Verbosity Check Style

G

Guest

I would like to get people's views on the best way to
implement verbosity (or any command-line option) into python
scripts.

The three styles I'm wrestling with are:

def func():
#do stuff
if VERBOSE:
print #something useful to know if wanted

or...

if VERBOSE:
def func():
#do stuff
print #something useful to know if wanted
else:
def func():
#do stuff

or...

def vfunc():
#do stuff
print #something useful to know if wanted
def func():
#do stuff
if VERBOSE:
func=vfunc

The former has the benefit of readability. The latter two
examples the benefit of being faster.

Is there any other great pythonic ways to do this? I'd like to
sample the smorgusborg of styles this list usually offers.

Thanks in advance,
Brian

PS. I'd like to extend an extra thanks to all the large
brain's in jars here. I'm just finishing a summer internship
at a Pharmaceutical Company customizing/optimizing
scripts/programs to do very high end computational chemistry.
(Why I'm a bit obsessed with speed) I have absolutely no
chemistry background and before I started only a year doing
python for small data processing challenges. Through being
able to read this list fairly constantly I've been able to
make such a big splash here (things that used to take days,
now take minutes) in my small amount of time that they'd like
to give me a laptop to keep me on as a contractor during the
school year. Thank you all so very much for giving a 19 year
old a bright future and a way to pay for college.
 
C

Christopher T King

I would like to get people's views on the best way to
implement verbosity (or any command-line option) into python
scripts.

I'd suggest one of the following:

1) Use a dedicated verbose() function. This will likely be the fastest
and cleanest method:

if VERBOSE:
def verbose(t):
print t
else:
def verbose(t):
pass

<some code>
verbose("I'm doing something here")
<more code>

2) Use a dedicated verbose file handle. This is as fast as and possibly
(depending on your taste) cleaner than the above method:

import sys
# Pythonic version of /dev/null
class devnull(object):
def write(self,d): pass

if VERBOSE:
verbose = sys.stdout
else:
verbose = devnull()

<some code>
print >>verbose, "I'm doing something here"
<more code>

Hope this helps.
 
D

Dan Sommers

I would like to get people's views on the best way to implement
verbosity (or any command-line option) into python scripts.

Use the logging module (new in 2.3). I think that verbosity equates to
the INFO level.

Regards,
Dan
 
F

Fernando Perez

I would like to get people's views on the best way to implement
verbosity (or any command-line option) into python scripts.
[/QUOTE]

Command-line options are handled by the optparse module, part of python's
standard library as of python 2.3. Python 2.2 had the older, somewhat less
flexible getopt module (which is still there in 2.3, of course).

Best,

f
 
M

Mark Bottjer

Dan said:
Use the logging module (new in 2.3). I think that verbosity equates to
the INFO level.

Not always. Many programs have multiple levels of informative verbosity,
where each level exposes "more" information. Of course, the additional
information can be viewed as DEBUG, but there can be multiple levels of
that, too.

Even if you want more than one level of verbosity, you can still use the
logging module (or any other facility, for that matter). I use a Verbose
class, which I pass an instance of to anything in my code which wants to
log something. A simplified version would be:

class Verbose:
import sys as __sys
def __init__(self, verbosity = 0, log = __sys.stderr):
self.__vebosity = verbosity
self.__log = log
def __call__(self, verbosity, msg):
if verbosity >= self.__verbosity:
print >>self.__log, '*' * verbosity, msg

if __name__ == '__main__':
# Test
import sys
verbose = Verbose(4, sys.stdout)
verbose(1, "Level one message.")
verbose(3, "Level three message.")
verbose(5, "Level five message.")

Which gives the following output when imported:

* Level one message.
*** Level three message.

The nice thing here is that, once you've got it in a class, you can
subclass it. This allows you to add helper methods to make logging
certain types of information easier, or to support different types of
logger. You can even use it to switch at runtime between using the
logger module, dumping straight to a stream, or both.

I find it to be quite flexible.

-- Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top