[Tutor] Sharpening the print statement (was:Readlines(longish!))

Alan Gauld alan.gauld@blueyonder.co.uk
Fri Jun 27 15:30:05 2003


> > print statements ... can obfuscate as much as they reveal
> > if overused.
>
> shows the intent of having a easy to change debug 'mode'.

> mode = 2 # execution mode, see readme. .config?
>          # 0 = print nothing, return password, script entry
> ....
>          # 5 = print everything, including list and object states
>
> def read(filename,key,command):
>    '''
>    if (mode >= 3): print "Debug: read(%s, %s, %s)" % (filename, key,
> command)
>    if (mode >= 2): print readmessage
>    if command == "!cli":
>       if not filename: filename = getfile(filename)

Unfortunately I think this tends to reveal the danger of over
using print statements! I have seen this done in C code and it
has the same effect. The best solution I've seen, and even
then I'm still not keen, is as follows:

- create a dictionary of error messages and debug level
  keyed by codeword.
- create a function that reads the dictionary and the
  current debug level.
- call the function with a list of codewords when needed

The above snippet then becomes:

def read(fn, ky, cmd):
    msgLog((params,readmessage))
    if cmd == "!cli":
       ....etc...

This removes all the spurious indentation involved in the if
tests and so maintains the structure of the code and hence
most of its readability while still providing switched error
reporting. But it does take more work to set up and maintain...

> found a easier way to make a sweeping debug mode ...

However fundamentally I'd recommend just using the debugger
for that kind of heaby duty printing, its designed to do it
and is much more controllable. The debugger is the best
"sweeping debug mode" IMHO.

Alan G.