python module import question -
so have big project many modules in it, , want run profiling on it. have profile module provides decorator can add function profile when it's called.
the problem is, i'll have import module dozens of modules have. fine guess, can't push code profiling modules imported or decorator on functions version control. means every time import/export, have add/remove profiling code.
is there kind of system manage adding/removing of profiling code without manually importing/deleting modules in every module of project? use mercurial, can't mess mercurial settings or make branch.
you create profiling module such imports other modules , annotates functions:
# these modules want profile import foo import huh # profiling function # yours smarter def profile(f): def gotcha(*args, **kwds): print "before" result = f(*args, **kwds) print "after" return result return gotcha # these functions in modules # want profile. each 1 patched here instead # of decorated there. foo.bar = profile(foo.bar) huh.baz = profile(huh.baz) huh.hmm = profile(huh.hmm)
this way don't have modify modules, if choose import profiling module anywhere @ runtime, "patch" other modules you'd like
you should able decorate class methods similarly.
Comments
Post a Comment