views:

43

answers:

4

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

+1  A: 

You need to fix various imprecisions (missing self, saying you're using class methods when there's no classmethod in sight, failing to inherit from object, ...) then make profile happy by giving it a string as it wants -- and the name of the instance must be made globally visible so that profile can actually use that string. For example:

import profile
import time

class Class(object):

  def doSomething(self):
      time.sleep(0.1)

  def callMethod(self):
      global _o
      _o = self
      profile.run('_o.doSomething()')

o = Class()
o.callMethod()
Alex Martelli
A: 

HI Alex,

when I tried this, I get the following :

file : buildobject.py

import profile class BuildObject:

def __init__(self, id):
    self.id = id
    self.setObjectAttributes();

def setObjectAttributes(self):

    global _o
    _o = self
    profile.run('_o.dbCalls()')    

def dbCalls(self):
    print "hello world!"

file: ../views.py build_object_one = BuildObject(b1)

now when I run this I get this error:

Traceback (most recent call last):

File "views.py", line 134, in

processQuery(sys.argv[1], sys.argv[2])

File "views.py", line 105, in processQuery

build_object_one = BuildObject(b1)

File "/perforce/hhimanshu-dev-webapps-main/webapp/webapps/changelog/classes/buildobject.py", line 26, in init self.setObjectAttributes();

File "/perforce/hhimanshu-dev-webapps-main/webapp/webapps/changelog/classes/buildobject.py", line 86, in setObjectAttributes profile.run('_o.dbCalls()')

File "/build/toolchain/lin32/python-2.6.1/lib/python2.6/profile.py", line 70, in run prof = prof.run(statement)

File "/build/toolchain/lin32/python-2.6.1/lib/python2.6/profile.py", line 456, in run return self.runctx(cmd, dict, dict)

File "/build/toolchain/lin32/python-2.6.1/lib/python2.6/profile.py", line 462, in runctx exec cmd in globals, locals

File "", line 1, in

NameError: name '_o' is not defined

learner
A: 

HI Alex,

when I tried this, I get the following :

file : buildobject.py

import profile class BuildObject:

def init(self, id): self.id = id self.setObjectAttributes();

def setObjectAttributes(self):

global _o
_o = self
profile.run('_o.dbCalls()')    

def dbCalls(self): print "hello world!" file: ../views.py build_object_one = BuildObject(b1)

now when I run this I get this error:

Traceback (most recent call last):

File "views.py", line 134, in

processQuery(sys.argv[1], sys.argv[2]) File "views.py", line 105, in processQuery

build_object_one = BuildObject(b1) File "/perforce/hhimanshu-dev-webapps-main/webapp/webapps/changelog/classes/buildobject.py", line 26, in init self.setObjectAttributes();

File "/perforce/hhimanshu-dev-webapps-main/webapp/webapps/changelog/classes/buildobject.py", line 86, in setObjectAttributes profile.run('_o.dbCalls()')

File "/build/toolchain/lin32/python-2.6.1/lib/python2.6/profile.py", line 70, in run prof = prof.run(statement)

File "/build/toolchain/lin32/python-2.6.1/lib/python2.6/profile.py", line 456, in run return self.runctx(cmd, dict, dict)

File "/build/toolchain/lin32/python-2.6.1/lib/python2.6/profile.py", line 462, in runctx exec cmd in globals, locals

File "", line 1, in

NameError: name '_o' is not defined

learner
A: 

Fixed!!!

Instead of profile, I used cProfile module that as per the python docs has much lesser overhead

Ref : http://docs.python.org/library/profile.html#introduction-to-the-profilers

with cProfiler, one can actually pass the local and global params using the runctx module so for the same problem, I did the following:

import cProfile
cProfile.runctx('self.doSomething()',globals(),locals())

and it worked :)

also, if you have more params to pass you can like

import cProfile
cProfile.runctx('self.doSomething(x,y,z)',globals(),locals())

Thanks for all help

learner