[Tutor] Question about profile.run() and decorators
Thane.Frivold at nokia.com
Thane.Frivold at nokia.com
Tue Feb 27 02:49:09 CET 2007
To whom it may concern,
I was directed to this forum... I searched for 'decorator
profile' in the Python tutorial archives, and had no hits, so I hope
this is not a lame question.
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.
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.
Cheers,
- 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, ')'
profile.run(expression)
return newFunction
import random
class Foo:
@myProfileDecorator
def results(x):
print str(x) + " Done"
x = Foo()
print x.results("Almost")
=-=-=
More information about the Tutor
mailing list