473,772 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to write verbose scripts

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
Sep 2 '08 #1
11 2838
On Tue, Sep 2, 2008 at 12:55 PM, Steven D'Aprano
<st***@remove-this-cybersource.com .auwrote:
Is there a better way of doing this than the way I am going about it?
Would the logging module help, and just print the output to the stdout
(or a file) instead?
Sep 2 '08 #2
On Sep 2, 11:55 am, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
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?
I do something like this, although I don't know if it would
be an improvement.

def collatz(a,p):
""" 3x+1 sequencer, no loop detection

collatz(a,p)
a: starting value
p: print options (binary)
bit 0 print even numbers (turns off power division)
bit 1 print odd numbers
bit 2 print debug (if any)
returns: CollatzSequence Parameters [R1count, R2count]
"""
ONE = gmpy.mpz(1)
TWO = gmpy.mpz(2)
TWE = gmpy.mpz(3)
a = gmpy.mpz(a)
t = 0
u = 0
done = 0

if (p & 1)==1:
print_evens = True
else:
print_evens = False
if (p & 2)==2:
print_odds = True
else:
print_odds = False
if (p & 4)==4:
print_debug = True
else:
print_debug = False

while done==0:
f = gmpy.scan1(a,0) # locate LS 1-bit
if f>0: # it's even
if print_evens:
print a,
a = a >1 # no power division
u += 1
else:
a = a >f # power division
u += f
else:
if print_odds:
print a,
if a==1:
done = 1
seq_end = t + u
else:
a = a*TWE + ONE
t += 1
return [u,t]
>
--
Steven
Sep 2 '08 #3
Steven D'Aprano schrieb:
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?
I use the logging-module.

Regarding the expensive computations: maysomething like this help:

class DeferredToStrin g(object):

def __init__(self, func):
self.func = func

def __repr__(self):
return repr(self.func( ))

def __str__(self):
return str(self.func() )

dts = DeferredToStrin g

Because then you can do

logger.debug("S ome text for an: %r", dts(lambda: long_computatio n()))

Because AFAIK the string is only interpolated if the logging level is
actually put out.

Diez
Sep 2 '08 #4
On Sep 2, 5:55*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
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?
How about:

def print_(obj, level=0):
if _verbosity >= level:
if callable(obj):
obj = obj()
print obj

so that you could then use:

print_("precond ition", level=2)

and:

print_(calculat e_complicated_t hing, level=3)

or:

print_(lambda: calculate_compl icated_thing(ar gument), level=3)
Sep 2 '08 #5
On Sep 3, 3:52 am, Mensanator <mensana...@aol .comwrote:
On Sep 2, 11:55 am, Steven D'Aprano <st...@REMOVE-THIS-

if (p & 1)==1:
print_evens = True
else:
print_evens = False
if (p & 2)==2:
print_odds = True
else:
print_odds = False
if (p & 4)==4:
print_debug = True
else:
print_debug = False
No, no, no, you've taken "How to write verbose scripts" rather too
literally; try this:

print_evens = p & 1
print_odds = p & 2
print_debug = p & 4
Sep 2 '08 #6
On Sep 2, 4:14 pm, John Machin <sjmac...@lexic on.netwrote:
On Sep 3, 3:52 am, Mensanator <mensana...@aol .comwrote:
On Sep 2, 11:55 am, Steven D'Aprano <st...@REMOVE-THIS-
if (p & 1)==1:
print_evens = True
else:
print_evens = False
if (p & 2)==2:
print_odds = True
else:
print_odds = False
if (p & 4)==4:
print_debug = True
else:
print_debug = False

No, no, no, you've taken "How to write verbose scripts" rather too
literally; try this:

print_evens = p & 1
print_odds = p & 2
print_debug = p & 4
Thanks for that. Luckily, the code only does that once per call.
Sep 2 '08 #7
John Machin a écrit :
On Sep 3, 3:52 am, Mensanator <mensana...@aol .comwrote:
>On Sep 2, 11:55 am, Steven D'Aprano <st...@REMOVE-THIS-

if (p & 1)==1:
print_evens = True
else:
print_evens = False
if (p & 2)==2:
print_odds = True
else:
print_odds = False
if (p & 4)==4:
print_debug = True
else:
print_debug = False

No, no, no, you've taken "How to write verbose scripts" rather too
literally;
KEYBOARD !

Sep 3 '08 #8
On 2 Sep., 18:55, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
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
You can save some code if you use function decorators for logging
input and output values of
functions.
So write lots of functions containing one statement and your problem
is solved ;-)

Greetings, Uwe

Sep 3 '08 #9
On Tue, 02 Sep 2008 13:07:33 -0400, Joe Riopel wrote:
On Tue, Sep 2, 2008 at 12:55 PM, Steven D'Aprano
<st***@remove-this-cybersource.com .auwrote:
>Is there a better way of doing this than the way I am going about it?

Would the logging module help, and just print the output to the stdout
(or a file) instead?
Thank you to everyone who answered.

As I feared, it seems that there's no really simple way of dealing with
arbitrary messages at arbitrary parts of my code.

I'm currently playing around with a few ideas, one of which is a
decorator to print information about function calls. However, what I'd
like to avoid is having to explicitly call the decorator on every method.
I guess that there's metaclass trickery I can play. Does anyone have any
pointers on how I can apply a decorator to every method in a class
automatically?

--
Steven
Sep 4 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11158
by: Pascal | last post by:
Hello, I've a function like this: def myfunction(data, verbose = False): dothis... if verbose: print 'I do this' dothisother if verbose: print 'I do this other' ...
1
2811
by: Jeremy | last post by:
I am using regular expressions and I would like to use both re.IGNORECASE and re.VERBOSE options. I want to do something like the following (which doesn't work): matsearch = r'''^\ {0,4}(\d+) ''' MatSearch = re.compile(matsearch, re.VERBOSE, re.IGNORECASE) Does anyone have any suggestions? Thanks, Jeremy
13
9650
by: Stumped and Confused | last post by:
Hello, I really, really, need some help here - I've spent hours trying to find a solution. In a nutshell, I'm trying to have a user input a value in form's textfield. The value should then be assigned to a variable and output using document.write. (Note, there is no submit button or other form elements. Basically
12
3347
by: Radek Maciaszek | last post by:
Hi It's very interesting problem. I couldn't even find any inforamtion about it on the google. I think that the best way of explain will be this simple example: <html> <body> <script language="JavaScript" type="text/javascript" src="write.js"></script>
0
1423
by: Adam H. Pendleton | last post by:
When running a "VACUUM VERBOSE" query using PQsendQuery, PQconsumeInput, and PQisBusy, all the "INFO:" output from the "VACUUM VERBOSE" command is printed on the terminal, and is not available through the PGresult structure. The resultset shows now rows, etc. but I would have expected to be able to get the "INFO:" output of the VERBOSE command. Is there any way to do this? Did I screw something up to make PG do this? Any help would be...
9
2681
by: Eric Lilja | last post by:
Hello, I'm a novice C++ programmer and now I have the task of converting a number of C++ functions to C. Some of the functions I'm converting takes a parameter of type reference-to-std::string that stores verbose error information if an error occurs in the function. How should I implement that in C? A pointer to a fixed-size array of chars runs the risk of overflowing. Having the function take a char** and allocate memory as
0
3584
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this OleDbParameterCollection" whenI try to write a new record. Delete and Modify work fine its just the add record function causes the error.
2
3506
by: raghunadhs | last post by:
Hi All, i have some simple scripts( which may have some insert statements, update statements etc). these scripts were written in Sql server 2000. now i want to use the same scripts in Access. (since the data is same in both Access, Sql server databases). can i write the scripts in Access-2003? if so please help me. Actually i tried with simple examples. it is working fine for insert, update statements. but it is not working for "If" state...
0
771
by: Hendrik van Rooyen | last post by:
Steven D'Aprano <stev...bersource.com.auwrote: Not sure if its "better", but I would keep the messages in a table or dict and have different tables or dicts for different levels of verbosity, and write a displayer that knows about the verbosity - This approach has the advantage that you can write in English and have different verbosities in different languages too, if the displayer knows about the language. Verbosity level and...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.