[Tutor] Question about profile.run() and decorators
kent37 at tds.net
Tue Feb 27 04:25:58 CET 2007
Thane.Frivold at nokia.com wrote:
> Is there a way to construct a string version (suitable to pass
> into profile.run()) from what is available inside a decorator function?
> I realize that what I am trying to do could probably be done otherwise,
> but this arose out of questions and problems possed in a Python class I
> just completed, and I am still trying to find my way around the
> language. My 'best' attempt is shown below.
Take a look at profile.Profile.runcall() (look at the source for
profile, it is not in the docs). This function takes an actual function
object as its parameter rather than a string describing the function call.
> Also, I have limited myself to a function with only 1 parameter,
> but it seems to get even worse if you have 2 or more arguments, since
> repr() takes only a single argument.
> BTW, I am using ActiveState Python 2.4.3 on Windows XP.
> Any and all suggestions or solutions welcomed.
> Thank you.
> - Thane
> import profile
> def myProfileDecorator(function):
> def newFunction(obj, *args):
> # This attempt does not seem to give an object expression that can
> be used
> #expression = function.__name__ + '(' + repr(obj) + ',' +
> repr(*args) + ')'
> # This attempt generates a NameError exception
> expression = function.__name__ + '(' + repr(*args) + ')'
> print 'About to call: profile.run(', expression, ')'
> return newFunction
> import random
> class Foo:
> def results(x):
> print str(x) + " Done"
> x = Foo()
> print x.results("Almost")
> Tutor maillist - Tutor at python.org
More information about the Tutor