[BangPypers] Python Maintainence code: Debugging, Tracing and Profiling help
Jeffrey Jose
jeffjosejeff at gmail.com
Sun Jan 24 09:05:13 CET 2010
Hi Anand,
I can talk a lil bit about your 2 queries. Debugging and seeing the flow of
the program.
*A. Debugging*
Surprisingly both of them come under the same banner. One of the reasons you
debug is to see how the code progresses. For debugging I highly recommend
Python Debugger. It would look a lot odd the first time you invoke it, but
trust me with a few neat tricks you'd be using it all the time.
There's just one thing you need to do to summon the Python Debugger. Insert
this one line in your code.
import pdb; pdb.set_trace()
The next time you run the code, it would drop into the debugger (you just
set a trace point). When you have the debugger prompt, you have access to
all the variables, stacktrace etc. You need to know a few commands to get
around.
c -> Continue (till the end of the program or till the next breakpoint)
n -> Execute next step
l -> See where you are
s -> Step into a function.
I accept this is not a natural way of doing things.. That's where we come to
the 'with-a-few-tricks-you'd-be-doing-this-all-the-time'
And the trick is this. Invoke Ipython from pdb. The good thing is you get
tab-competition (which pdb lacks). You can try out all kinds of things.
Introspect, import other modules, try and see the next line and see what its
output 'would be'. Its sweet. I highly recommend you take a look at this.
Starting ipython from
pdb<http://libreamoi.com/index.php/starting-ipython-from-pdb/>-
http://libreamoi.com/index.php/starting-ipython-from-pdb/
Hit Ctrl-D to exit from Ipython to go to pdb again and hit 'c' to continue
the program. While you're at pdb, you can still go into ipython again.
Remember Ipython is a regular prompt, so you cant do debugger-y things like
Step Into, Continue etc. For that you'll have to come 'out' of Ipython to
pdb. You can go back to Ipython anytime.
*B. Seeing Your Program Flow*
If I understand you correctly, I've seen this problem hit me lot of times.
If you're working on others code, and all of a sudden you wanna
understand/fix them - you'd be sitting there thinking - Damn, *how* does
this code work. I can see a lot of functions and I get what it does. But
when does this one get called. I see this main call from main() .. but is
there another place where it gets called ?. If so what are the arguments.
What does it return on this one specific call etc.
While its technically possible to do next-next-next using Python Debugger to
step through the program and sometimes that's all can you do.
But there's a much better way than inserting
print "Called me!"
print "Done with me!"
etc.
The idea here is to use sys.settrace() to insert to a utility function which
gets called before any important events. Events include,
# execution of any line (that means all the time)
# calling a function
# return from a function
# encountering an exception
This means your utility function can go .. hmm .. is this a function call ?
.. if so gimme the name of the caller and name of the function and let me
print it.
When you run the code you'd get a huge output with prints which shows the
flow of the program.
Again, refer this page for a better understanding.
Tracing Your Program As It Runs -
http://blog.doughellmann.com/2009/11/pymotw-sys-part-5-tracing-your-program.html
One caution : Do Not Abuse sys.settrace() Use sys.settrace() for debugging
only. You might be tempted to do crazy things like .. during every function
call .. I'll authorize the user or something. That better be done using
different methods. Just saying :)
Unfortunately, the place where I work doesnt care much about performance (or
unnecessary optimization) for the python code. So I cant help you there.
I'll let others chip in for those stuff.
HTH
Jeff
On Sun, Jan 24, 2010 at 4:34 AM, <learningpython at aol.com> wrote:
>
>
>
> Hi Experts,
>
> I am back to python after a while of just formal introduction. Thistime i
> have to manage a huge files with numerous functions, i am debugging a issue
> right now and would require to understand the flow ofthe code.
>
> Please can you help me on what can i do to have better understanding ofthe
> profiling of classes, defs invoked each time and how to read them.
>
> The ones i plan to do is to insert lot of prints in every class, defs ex:
> print self.__class__.__name__ in every def, to see the flow which is tedious
> and time consuming as the size of files and project is huge.
>
> Any suggestions please and advice please.
>
> PS: how to post to comp.lang.python newsgroup please??
>
>
> Cheers
> Anand
>
>
>
>
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
More information about the BangPypers
mailing list