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.


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. = profile(
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

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.