[Tutor] Question about profile.run() and decorators

Thane.Frivold at nokia.com Thane.Frivold at nokia.com
Tue Feb 27 20:21:38 CET 2007


Kent,

	Thank you. 

	That was just the kind of hint I needed. I will make sure to
look for objects inside imported packages in the future too; I missed
the Profile attribute when I first ran dir(profile) looking for
available functions.

	In case you archive the full email threads, I have included a
copy of the working program for reference for future neophytes like
myself...

	Cheers,

- Thane
  
=-=-=

import profile

def myProfileDecorator(function):
  def newFunction(obj, *args, **keywords):
    p = profile.Profile()
    p.runcall(function, *args, **keywords)
    p.print_stats()
  return newFunction

import random

class Foo:
  @myProfileDecorator
  def results(x):
    spin = [x*x for x in range(2000000)]
    print str(x) + " Done"

x = Foo()

print x.results("Almost")

=-=-=

>-----Original Message-----
>From: ext Kent Johnson [mailto:kent37 at tds.net] 
>Sent: Monday, February 26, 2007 7:26 PM
>To: Frivold Thane (Nokia-M/SanFrancisco)
>Cc: tutor at python.org
>Subject: Re: [Tutor] Question about profile.run() and decorators
>
>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.
>
>Kent
>
>> 
>> 	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")
>> 
>> =-=-=
>> 
>> 
>> 
>>  
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>> 
>
>


More information about the Tutor mailing list