[Tutor] Conventions for code snippets for debugging and testing?

Ricardo Aráoz ricaraoz at gmail.com
Fri Apr 11 16:22:39 CEST 2008


Robert Kirkpatrick wrote:
> Hi All,
> 
> Just wondering if there are any basic conventions for including code
> snippets that are for testing / debugging only?
> 
> For example, you could set a boolean variable called DEBUG, then have
> snippets of code like:
> 
> if DEBUG:
>     do stuff
> else:
>     do otherstuff
> 
> The use case I'm dealing with right now is to query the SVN commits for a
> weekly period and report on each user for that time period.  If I'm testing
> though, I only want to cycle through a few users, not all of them.
> 
> I'm thinking there has to be something slicker than that but maybe not...
> 
> Thoughts?
> 
> Rob
> 

Check the logging standard module. I use it this way (from the manual) :

<code>

import logging

logging.basicConfig(level=logging.DEBUG,
                     format='%(asctime)s %(levelname)s %(message)s',
                     filename='/tmp/myapp.log',
                     filemode='w')
logging.debug('A debug message')
logging.info('Some information')
logging.warning('A shot across the bows')

</code>

I put the logging.debug('dbg messg') at the appropriate places. When I'm 
done then I replaces the logging.basicConfig(level=logging.CRITICAL or 
just 100 so as not to log anything and that's it.
You have 6 preset levels, but you can define as many levels you want.

Level		Numeric value
CRITICAL	50
ERROR		40
WARNING 	30
INFO 		20
DEBUG 		10
NOTSET 		0


Once you have your logging set you use a tail program to watch your log 
and see what's going on.

HTH




More information about the Tutor mailing list