traceback tool to increase efficiency.
Rich Harkins
rich at worldsinfinite.com
Mon Aug 12 13:43:24 EDT 2002
[ Original messages snipped in their entirety ]
On Monday 12 August 2002 01:17 pm, you (Hunter Peress) wrote (original
messages completely snipped):
> I will probably do the sys.settrace().
>
> But check this out, my test program:
> def yo(S):
> print "FROM USER:in yo [%s]"%S
> if S < 30:
> ho(S+1)
>
> def ho(S):
> print "FROM USER:in ho [%s]"%S
> if S < 30:
> MA(S+1)
>
> def MA(S):
> print "FROM USER:in ma [%s]"%S
> if S < 30:
> yo(S+1)
>
> yo(0)
>
> run it through profile.py, and I get:
>
> 34 function calls (6 primitive calls) in 0.000 CPU seconds
>
> Ordered by: standard name
>
> ncalls tottime percall cumtime percall filename:lineno(function)
> 1 0.000 0.000 0.000 0.000 <string>:1(?)
> 1 0.000 0.000 0.000 0.000
> profile:0(execfile('stackt.py'))
> 0 0.000 0.000 profile:0(profiler)
> 1 0.000 0.000 0.000 0.000 stackt.py:1(?)
> 11/1 0.000 0.000 0.000 0.000 stackt.py:1(yo)
> 10/1 0.000 0.000 0.000 0.000 stackt.py:11(MA)
> 10/1 0.000 0.000 0.000 0.000 stackt.py:6(ho)
>
>
> I'm looking for a simple spit out of which methods are called, not
> really these types of stats.
>
> The pydoc page for it is more of an API than a manpage....so would u
> have any idea if profiler can do what i need?
>
The profiler can tell where activity happened in each function at all and how
much time Python spent in those functions, not the order it happened in. If
you're looking for that then sys.settrace() is probably your best bet or use
the Python debugger and walk the code with that.
Here's a script that may help:
--- SNIP ---
import sys,getopt
# Defaults
showargs=0
outfile=sys.stderr
# Process - arguments.
while sys.argv[1][0] == '-':
arg=sys.argv.pop(1)[1:]
if arg[:1] == 'a':
showargs=1
elif arg[:1] == 'o':
filename=arg[1:]
if not filename:
filename=sys.argv.pop(1)
outfile=open(filename,'w')
else:
print >>sys.stderr,"Unsupport pytrace option: "+arg
# Fixup sys.argv
destfile=sys.argv[1]
sys.argv.pop(0)
def tracer(frame,event,arg):
if event == 'call':
if showargs:
outfile.write("%s(%s) in %s\n" % (
frame.f_code.co_name,
', '.join(['%s=%s' % (name,frame.f_locals[name])
for name in frame.f_locals]),
frame.f_code.co_filename,
))
else:
outfile.write("%s(...) in %s\n" %
(frame.f_code.co_name,frame.f_code.co_filename))
sys.settrace(tracer)
execfile(destfile)
--- SNIP ---
Save this file somewhere (such as /tmp/pytrace.py). To use it:
python /tmp/pytrace.py [options] YOURSCRIPT ARGS
options can be:
-a: show arguments to each call
-o outfile: output trace to outfile instead of stderr.
I just whipped this up so I can't vouch for it's operational capabilities but
it should give you a place to get started.
Let me know how it works...
Rich
PS: I copied this to the list in case someone else finds this script useful.
More information about the Python-list
mailing list