473,767 Members | 2,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

easy verbose in functions

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'
...

How can I do to exclude eachtime the `if verbose` statement?

I tried `print verbose and 'I do this'` but it's printing `False`.
So I tried with `verbose = None` in the definition, but it's printing None!

Thanks.
Jul 18 '05 #1
4 11153
Pascal wrote:
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'
...

How can I do to exclude eachtime the `if verbose` statement?


How about defining a function like:

def myprint(arg):
if verbose:
print arg

and using that?

--
Timo Virkkala
Jul 18 '05 #2
pa***********@f ree.fr (Pascal) wrote in
news:e5******** *************** ***@posting.goo gle.com:
def myfunction(data , verbose = False):
dothis...
if verbose: print 'I do this'
dothisother
if verbose: print 'I do this other'
...

How can I do to exclude eachtime the `if verbose` statement?


If it offends you that much:

def log_verbose(s):
print s
def log_dummy(s):
pass
def myfunction(data , verbose = False):
if verbose:
log = log_verbose
else:
log = log_dummy
dothis...
log('I do this')
dothisother
log('I do this other')
...

or even pass the log function around as the parameter.

A more general solution might be to use Python's logging facility. That
gives you the opportunity to classify your log messages by different levels
(debug, info, warning, error, critical) and also by the section of program
in a hierarchical arrangement. This gives you the ability to, say, turn on
debug logging for a class, or a module by editing a config file.
Jul 18 '05 #3
Hello Pascal,
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'
...

How can I do to exclude eachtime the `if verbose` statement?

I tried `print verbose and 'I do this'` but it's printing `False`.
So I tried with `verbose = None` in the definition, but it's printing None!

Try the logging package?

HTH.
Miki
Jul 18 '05 #4
Hello Pascal,

Pascal wrote:
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'
...

How can I do to exclude eachtime the `if verbose` statement?

I tried `print verbose and 'I do this'` but it's printing `False`.
So I tried with `verbose = None` in the definition, but it's printing None!


I'm not sure exactly what you are doing because I can't
duplicate your results.

But I am guessing that what you are doing is passing a
string "False" instead of the boolean constant False.
The constant False is only defined in Python 2.2 and
higher.

You can put something like this at the beginning of
your program to make sure that False and True are
defined no matter what version of Python you are using:

try:
if False:
pass
except:
False = 0
True = not False
Notice the difference between a string and a boolean value:
print False; print "False" 0
False type(False); type("False")

<type 'int'>
<type 'str'>

Likewise None is not the same as "None".
The way I would handle verbose printing in functions is
to factor out the "verbosity" from the function logic:

def verboseprint(s, verbose):
if verbose: print s

def myfunction(arg, verbose=False):
dothis
verboseprint("D o this", verbose)
dothat
verboseprint("D o that", verbose)
That way, if there is a bug in your handling of verbose
printing, you only need to fix it in one place. If you
decide to change verbose printing to (say) using a log
file, you only need to change one place.

The statement:

print verbose and "Do this"
will not work. If verbose is True, then the term
(verbose and "Do this") will evaluate to "Do this", and
Python will print what you expect, namely "Do this".

But if verbose is False, then the term (verbose and "Do
this") will evaluate to False, and Python will then
print False, which is not what you want.

--
Steven D'Aprano
Jul 18 '05 #5

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

Similar topics

3
2350
by: Michael Ahlers | last post by:
Obviously if you're looping or using a template, choosing output based on the current iteration is easy. For example, if you're walking a set of elements and you want index % 2 == 0 produce one thing and another when that is not true. My question is, what if you are not looping through tags but want to achieve a similar effect without having to be excessively verbose? Is there any way that I can simplify the following block? <!-- ......
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
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
2680
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
2
1429
by: Frank Lopez | last post by:
Question: Is there any way to use the Visual Studio .NET environment to easily identify C and C++ functions that are not called? If not, does anyone have any recommendations on some other software that I can quickly get my hands on to do this? Right now, I only need to do it once.
3
4070
by: mast2as | last post by:
In the same vein as the topic that I started on exception handling ;-) .... If I have read (not all of them though) the documents that you guys pointed me to, the try/throw/catch mechanism should really be kept for exception handling (basically errors whevere they are critical or not). Now, while I am developing this application, so far I used std::cout quite a lot to check that the code was doing the right thing (writing out to the...
12
3557
by: Les Caudle | last post by:
I've got a .NET 2.0 app that works quite well on all of my test boxes. However, at the client's site, it crashes with 'has encounted a problem' basic dialog. No useful info. I've yet to see a .NET app crash like this, without the verbose dialog that will tell me the line # of the problem (the app was compiled in debug mode). What steps can I take to track this down?
84
7218
by: braver | last post by:
Is there any trick to get rid of having to type the annoying, character-eating "self." prefix everywhere in a class? Sometimes I avoid OO just not to deal with its verbosity. In fact, I try to use Ruby anywhere speed is not crucial especially for @ prefix is better- looking than self. But things grow -- is there any metaprogramming tricks or whatnot we can throw on the self? Cheers,
11
2838
by: Steven D'Aprano | last post by:
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
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
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
10168
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
10009
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
9959
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
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.