I find myself writing command line tools in Python where I wish to
include "verbose" output to stdout.
I start with a helper function:
def print_(obj, level=0):
if _verbosity >= level:
print obj
And then I end up with functions or methods looking like this:
def parrot(x)
print_("precond ition", level=2)
do_something()
print_("status is good...", level=1)
print_("parrot is squawking strongly now", level=2)
do_something_el se()
print_("squawk squawk squawk", level=3)
do_more()
print_("postcon dition", level=1)
return something
That often means that my functions end up with more message printing code
than actual code. The whole thing seems messy and hard to manage for all
but the smallest scripts.
Worst of all, sometimes the messages I wish to print may be expensive to
compute, and I don't want to waste time computing them if they aren't
going to be printed because the verbosity is too low. But nor do I wish
to fill my code with this:
if _verbosity >= 3:
x = calculate_compl icated_thing()
print_(x, level=3)
Is there a better way of doing this than the way I am going about it?
--
Steven
include "verbose" output to stdout.
I start with a helper function:
def print_(obj, level=0):
if _verbosity >= level:
print obj
And then I end up with functions or methods looking like this:
def parrot(x)
print_("precond ition", level=2)
do_something()
print_("status is good...", level=1)
print_("parrot is squawking strongly now", level=2)
do_something_el se()
print_("squawk squawk squawk", level=3)
do_more()
print_("postcon dition", level=1)
return something
That often means that my functions end up with more message printing code
than actual code. The whole thing seems messy and hard to manage for all
but the smallest scripts.
Worst of all, sometimes the messages I wish to print may be expensive to
compute, and I don't want to waste time computing them if they aren't
going to be printed because the verbosity is too low. But nor do I wish
to fill my code with this:
if _verbosity >= 3:
x = calculate_compl icated_thing()
print_(x, level=3)
Is there a better way of doing this than the way I am going about it?
--
Steven
Comment