Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Stopping a fucntion from printing its output on screen

Reply
Thread Tools

Stopping a fucntion from printing its output on screen

 
 
sophie_newbie
Guest
Posts: n/a
 
      10-17-2007
Hi, in my program i need to call a couple of functions that do some
stuff but they always print their output on screen. But I don't want
them to print anything on the screen. Is there any way I can disable
it from doing this, like redirect the output to somewhere else? But
later on in the program i then need to print other stuff so i'd need
to re-enable printing too. Any ideas?

 
Reply With Quote
 
 
 
 
Jeremy Sanders
Guest
Posts: n/a
 
      10-17-2007
sophie_newbie wrote:

> Hi, in my program i need to call a couple of functions that do some
> stuff but they always print their output on screen. But I don't want
> them to print anything on the screen. Is there any way I can disable
> it from doing this, like redirect the output to somewhere else? But
> later on in the program i then need to print other stuff so i'd need
> to re-enable printing too. Any ideas?


If they are python functions, this hack should work...

import sys

class NullWriter(object):
def write(self, arg):
pass

def testfunc():
print "this is a test"

nullwrite = NullWriter()
oldstdout = sys.stdout
sys.stdout = nullwrite # disable output
testfunc()
sys.stdout = oldstdout # enable output
testfunc()


--
Jeremy Sanders
http://www.jeremysanders.net/
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      10-17-2007
On Wed, 17 Oct 2007 07:57:04 -0700, sophie_newbie wrote:

> Hi, in my program i need to call a couple of functions that do some
> stuff but they always print their output on screen. But I don't want
> them to print anything on the screen. Is there any way I can disable it
> from doing this, like redirect the output to somewhere else? But later
> on in the program i then need to print other stuff so i'd need to
> re-enable printing too. Any ideas?



If it is your program, then just change your program to not print to the
screen! Instead of writing a function like this:


def parrot():
# This is bad practice!
do_lots_of_calculations()
print "This is a parrot"


write it like this:

def parrot():
# This is good practice
do_lots_of_calculations()
return "This is a parrot"


What's the difference? In the first version, the function parrot()
decides that its result is always printed. In the second version, YOU
decide:


result = parrot()
# now pass the result to something else
do_more_calculations(result)
# or print it
print result


Otherwise, you can do something like this:

import cStringIO
import sys
capture_output = cStringIO.StringIO()
sys.stdout = capture_output
# call the function that always prints
parrot()
# now restore stdout
sys.stdout = sys.__stdout__


but that's a little risky, and I recommend against it unless you have no
other choice.


--
Steven
 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      10-17-2007
On Oct 17, 4:01 pm, Jeremy Sanders <jeremy
(E-Mail Removed)> wrote:
> sophie_newbie wrote:
> > Hi, in my program i need to call a couple of functions that do some
> > stuff but they always print their output on screen. But I don't want
> > them to print anything on the screen. Is there any way I can disable
> > it from doing this, like redirect the output to somewhere else? But
> > later on in the program i then need to print other stuff so i'd need
> > to re-enable printing too. Any ideas?

>
> If they are python functions, this hack should work...
>
> import sys
>
> class NullWriter(object):
> def write(self, arg):
> pass
>
> def testfunc():
> print "this is a test"
>
> nullwrite = NullWriter()
> oldstdout = sys.stdout
> sys.stdout = nullwrite # disable output
> testfunc()
> sys.stdout = oldstdout # enable output
> testfunc()
>

You might want to guarantee that the output is re-enabled even if
testfunc() raises an exception:

nullwrite = NullWriter()
oldstdout = sys.stdout
sys.stdout = nullwrite # disable output
try:
testfunc()
finally:
sys.stdout = oldstdout # enable output

 
Reply With Quote
 
Paul Hankin
Guest
Posts: n/a
 
      10-18-2007
On Oct 17, 3:57 pm, sophie_newbie <(E-Mail Removed)> wrote:
> Hi, in my program i need to call a couple of functions that do some
> stuff but they always print their output on screen. But I don't want
> them to print anything on the screen. Is there any way I can disable
> it from doing this, like redirect the output to somewhere else? But
> later on in the program i then need to print other stuff so i'd need
> to re-enable printing too. Any ideas?


Yes, in your functions that you may or may not want to print stuff,
declare them with a stream parameter that defaults to stdout.

For example:

import sys

def f(i, out = sys.stdout)
# Do something...
print >>out, "i is %d" % i

Then usually, you call
f(10)

But when you want to elide the output, use Jeremy's nullwriter:
class NullWriter(object):
def write(self, arg):
pass
nullwriter = NullWriter()

f(10, out = nullwriter)

Having the output stream explicit like this is much better style than
abusing sys.stdout, and it won't go wrong when errors occur. It's the
same idea as avoiding global variables.

--
Paul Hankin

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
How can i return an iterator from a fucntion? TOMERDR C++ 6 05-22-2006 09:21 PM
Now() Fucntion and CurrentCulture ra294 ASP .Net 5 11-26-2004 08:51 AM



Advertisments