python module import question

So I have a big project with many modules in it, and I want to run some profiling on it. I have a profile module that basically just provides a decorator that I can add to a function to profile it when it's called.

The problem is, I'll have to import that module into the dozens of modules that I have. This is fine I guess, but I can't push the code with the profiling modules imported or the decorator on the functions to version control. This means every time I import/export, I have to add/remove all my profiling code.

Is there any kind of system to manage this adding/removing of profiling code without manually importing/deleting the modules in every module of my project? We use mercurial, but I can't really mess with mercurial settings or make a branch.

Answers


You could create your profiling module such that it imports your other modules and annotates their functions:

# these are the modules you want to profile
import foo
import huh

# This is a profiling function
# yours would do something smarter
def profile(f):
    def gotcha(*args, **kwds):
        print "before"
        result = f(*args, **kwds)
        print "after"
        return result
    return gotcha

# these are the functions in those modules that you
# want to profile.  Each one is patched here instead
# of decorated there.
foo.bar = profile(foo.bar)
huh.baz = profile(huh.baz)
huh.hmm = profile(huh.hmm)

This way you don't have to modify those modules, but if you choose to import this profiling module anywhere at runtime, it will "patch" all the other modules as you'd like

You should be able to decorate class methods similarly.


Need Your Help

VB datetimepicker format as 09-SEP-2012

visual-studio-2010 datetimepicker datetime-format

I am trying to get datetimepicker to return the date in the following format: 08-SEP-2012.

How can I keep field data from being escaped and preserve html and image styles in CodeIgniter?

php codeigniter escaping codeigniter-2 codeigniter-form-helper

I’m using CKEditor and KCFinder to build a relatively simple CMS. Currently, things are working pretty well. I noticed an issue, though, when I started integrating images into the mix. The links fo...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.