开发者

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 m开发者_高级运维eans 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.
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.


Why not just create a branch with all the profiling changes, and then merge the branch when you want to test ... and revert everything afterwards.

git makes this somewhat easier with it's "git stash", but using branches shouldn't be significantly harder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜