RE: Python profiler usage with objects

2010-06-30 Thread Ben Kaplan


 -Original Message-
 From: python-list-bounces+bsk16=case@python.org [mailto:python-list-
 bounces+bsk16=case@python.org] On Behalf Of rik
 Sent: Tuesday, June 29, 2010 10:52 PM
 To: python-list@python.org
 Subject: Re: Python profiler usage with objects
 
 harit harit.himanshu at gmail.com writes:
 
 
  Hi,
 
  I have a specific question regarding the usage of profiler. I am new
  to python programming I am trying to profile a function which I want
  to invoke as a class method, something like this
 
  import profile
 
  class Class:
 
  def doSomething():
 
  do here ..
 
  def callMethod():
 
  **self.doSomething()**
  instead of this I want to use
 
  **profile.run(self.doSomething())**
  but the profile.run expects the string inside it and I get error
 
  TypeError: exec: arg 1 must be a string, file, or code object
 
  Can somebody please help?
 
  Thank you
 
 
 
 Harit,
 
 i am OLD to python, and have used its profiler in the past.
 but i'm getting your same error:
 
  TypeError: exec: arg 1 must be a string, file, or code object
 
 on both Ubuntu with Python 2.6 and OSX with 2.4.  with both cProfile and
 profile?!  whether or not i specify a file for profile output!?!
 
 anybody else having trouble profiling?
 
  - rik
 

Let's take this code as an example:

def foo() :
return None

import profile
profile.run(foo())

What does the profile.run call do?

First thin it does is evaluate foo(), which returns None. So you're calling
profile.run(None)

There's  nothing special about profile.run- you have to hand it something to
execute, not something already executed. Try calling
Profile.run(doSomething) # no parenthesis for doSomething.
 
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python profiler usage with objects

2010-06-30 Thread rik
Ben Kaplan bsk16 at case.edu writes:
 
 
 Let's take this code as an example:
 
 def foo() :
 return None
 
 import profile
 profile.run(foo())
 
 What does the profile.run call do?
 
 First thin it does is evaluate foo(), which returns None. So you're calling
 profile.run(None)
 
 There's  nothing special about profile.run- you have to hand it something to
 execute, not something already executed. Try calling
 Profile.run(doSomething) # no parenthesis for doSomething.
  
  --
  http://mail.python.org/mailman/listinfo/python-list
 
 

hi Ben,  right: i have a top-level function main() that runs just
fine.  but i give it as an argument to cProfile.run() or profile.run(),
it executes as expected, but then:

   File /var/folders/lu/luGJNSGwE0mO84R+YbcKpU+++TI/-Tmp-/python-439Ffi.py,
line 544, in ?
 profile.run(main(seedFile),'/Data/tmp/fetchProfile_100629.profile')
   File
/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/profile.py,
line 72, in run
 prof = prof.run(statement)
   File
/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/profile.py,
line 448, in run
 return self.runctx(cmd, dict, dict)
   File
/Library/Frameworks/Python.framework/Versions/2.4//lib/python2.4/profile.py,
line 454, in runctx
 exec cmd in globals, locals
 TypeError: exec: arg 1 must be a string, file, or code object

this example is from python2.4 on OSX, but the same code generates
the same error on python2.6 on Ubuntu?!  





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python profiler usage with objects

2010-06-30 Thread rik
Ben Kaplan bsk16 at case.edu writes:

 First thin it does is evaluate foo(), which returns None. So you're calling
 profile.run(None)
 
 There's  nothing special about profile.run- you have to hand it something to
 execute, not something already executed. Try calling
 Profile.run(doSomething) # no parenthesis for doSomething.

i'm reading your message more carefully, and so tried calling
profile.run(main), without any parens (and absorbing the
argument into the function's body for this test).  same error!?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python profiler usage with objects

2010-06-30 Thread Rami Chowdhury
On 2010-06-30 06:39, rik wrote:
 Ben Kaplan bsk16 at case.edu writes:
  
  
  Let's take this code as an example:
  
  def foo() :
  return None
  
  import profile
  profile.run(foo())
  
  What does the profile.run call do?
  
  First thin it does is evaluate foo(), which returns None. So you're calling
  profile.run(None)
  
  There's  nothing special about profile.run- you have to hand it something to
  execute, not something already executed. Try calling
  Profile.run(doSomething) # no parenthesis for doSomething.
   
   --
   http://mail.python.org/mailman/listinfo/python-list
  
  
 
 hi Ben,  right: i have a top-level function main() that runs just
 fine.  but i give it as an argument to cProfile.run() or profile.run(),
 it executes as expected, but then:
 
File /var/folders/lu/luGJNSGwE0mO84R+YbcKpU+++TI/-Tmp-/python-439Ffi.py,
 line 544, in ?
  profile.run(main(seedFile),'/Data/tmp/fetchProfile_100629.profile')
 

Looks like the same problem. As Ben pointed out, you need to pass profile.run() 
an executable
(e.g. the main() function itself) and not something that's already been
executed. Since you've already called main(seedFile), it's the return value
that is being passed to profile.run(), and that's why it's complaining...

Perhaps you meant:
profile.run('main(seedFile)', '/Data/tmp/whatever')

?


 -- 
 http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python profiler usage with objects

2010-06-30 Thread rik
Ben Kaplan bsk16 at case.edu writes:

 There's  nothing special about profile.run- you have to hand it something to
 execute, not something already executed. Try calling
 Profile.run(doSomething) # no parenthesis for doSomething.

your hint and REREADING THE DOCUMENTATION made me realize it
was the QUOTE MARKS on the function call i was missing:

profile.run('main(seedFile)','profileOutFile')

Harit, perhaps this was your problem, too?

Ben, thanks for your help.


-- 
http://mail.python.org/mailman/listinfo/python-list


Python profiler usage with objects

2010-06-29 Thread harit
Hi,

I have a specific question regarding the usage of profiler. I am new
to python programming I am trying to profile a function which I want
to invoke as a class method, something like this

import profile

class Class:

def doSomething():

do here ..

def callMethod():

**self.doSomething()**
instead of this I want to use

**profile.run(self.doSomething())**
but the profile.run expects the string inside it and I get error

TypeError: exec: arg 1 must be a string, file, or code object

Can somebody please help?

Thank you
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python profiler usage with objects

2010-06-29 Thread rik
harit harit.himanshu at gmail.com writes:

 
 Hi,
 
 I have a specific question regarding the usage of profiler. I am new
 to python programming I am trying to profile a function which I want
 to invoke as a class method, something like this
 
 import profile
 
 class Class:
 
 def doSomething():
 
 do here ..
 
 def callMethod():
 
 **self.doSomething()**
 instead of this I want to use
 
 **profile.run(self.doSomething())**
 but the profile.run expects the string inside it and I get error
 
 TypeError: exec: arg 1 must be a string, file, or code object
 
 Can somebody please help?
 
 Thank you
 


Harit,

i am OLD to python, and have used its profiler in the past.
but i'm getting your same error:

 TypeError: exec: arg 1 must be a string, file, or code object

on both Ubuntu with Python 2.6 and OSX with 2.4.  with both
cProfile and profile?!  whether or not i specify a file for
profile output!?!

anybody else having trouble profiling?

 - rik


-- 
http://mail.python.org/mailman/listinfo/python-list