[python-win32] python print statements

King Simon-NFHD78 simon.king at motorola.com
Thu Jan 8 20:03:51 CET 2009


I don't think anyone's mentioned the python logging package, which is
good for general purpose logging. However, since you want to do this
without changing all your print statements:

In Python 2.x, you can probably reassign sys.stdout. As long as you
aren't outputting anything else to stdout, this should probably work
(lightly tested):

---------------------------------------

import sys

class DummyStdout(object):
    def write(self, text):
        pass

print "sys.stdout is not redirected"
sys.stdout = DummyStdout()
print "This should not appear"
print "Nor should this"
# Restore the original stdout again
sys.stdout = sys.__stdout__
print "This should appear again now"

----------------------------------------

Instead of the DummyStdout class, you could also try:

sys.stdout = open('NUL', 'w')

I believe NUL is the Windows equivalent of /dev/null, but I'm not an
expert.

In Python 3.0, 'print' is a normal function instead of a keyword, so I
assume you can replace it like this (untested):

----------------------------------------

import __builtin__

def my_print(*args, **kwargs):
    pass

__builtin__.print = my_print

----------------------------------------

Hope that helps,

Simon

> -----Original Message-----
> From: python-win32-bounces+simon.king=motorola.com at python.org 
> [mailto:python-win32-bounces+simon.king=motorola.com at python.or
> g] On Behalf Of Nalli Dinesh
> Sent: 08 January 2009 18:13
> To: Vernon Cole; Jim.Vickroy at noaa.gov
> Cc: python-win32 at python.org
> Subject: Re: [python-win32] python print statements
> 
> Thanks Vernon, Jim, Micheal.
>  
> I kind of knew the different ways of modelling any python 
> application as you guys have described in your email. I 
> appreciate your inputs though. But I am looking for a 
> different solution.
>  
> Here is what I am looking at -
>  
> I do not want to remodel my application at this stage. I have 
> print statements all over the place. I looking at a solution 
> where, without touching the print statements at all, I want 
> to tell the python interpreter to not execute print 
> statements inside my application, when the application is 
> running. Like, is there a way to tell the Python interpreter 
> do not bother to execute print lines in my application. Or is 
> there a way to just define a python builtin variable which 
> dictates executing print statements or not.
>  
> Hope I am able to describe clearly what solution I am looking for.
>  
> To my understanding, I do not think any language supports 
> stuff like this. If I am right about it, then we all know to 
> what level of growth all the s/w languages must grow too!!!!
> 
> 
> On Thu, Jan 8, 2009 at 8:02 AM, Vernon Cole 
> <vernondcole at gmail.com> wrote:
> 
> 
> 	How about --
> 	 if debug: print x
> 	or, in a more complex setting, have a "verbose" 
> attribute in each module and --
> 	 if self.verbose > 2: print x   # so you can have 
> levels of debug printouts
> 	??
> 	--
> 	Vernon Cole
> 	
> 
> 	On Wed, Jan 7, 2009 at 11:30 PM, Michel Claveau 
> <mc at mclaveau.com> wrote:
> 	> Hi!
> 	>
> 	> 1) Define your print function. Example:
> 	>   def mprint(*par):
> 	>       for i in par:
> 	>           print i,
> 	>       print
> 	>
> 	> 2) in your code, replace all  'print'  by  'mprint'
> 	>
> 	> 3) when you want cancel the print, modify only the 
> mprint function.
> 	>
> 	> @-salutations
> 	> --
> 	> Michel Claveau
> 	> _______________________________________________
> 	> python-win32 mailing list
> 	> python-win32 at python.org
> 	> http://mail.python.org/mailman/listinfo/python-win32
> 	>
> 	_______________________________________________
> 	python-win32 mailing list
> 	python-win32 at python.org
> 	http://mail.python.org/mailman/listinfo/python-win32
> 	
> 
> 
> 


More information about the python-win32 mailing list