473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stopping a fucntion from printing its output on screen

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?

Oct 17 '07 #1
4 29257
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/
Oct 17 '07 #2
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
Oct 17 '07 #3
On Oct 17, 4:01 pm, Jeremy Sanders <jeremy
+complangpyt...@jeremysanders.netwrote:
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

Oct 17 '07 #4
On Oct 17, 3:57 pm, sophie_newbie <paulgeele...@gmail.comwrote:
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

Oct 18 '07 #5

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

Similar topics

8
by: Tinus | last post by:
Hello all, Because you have been so helpfull the last couple of times, I thought after testing and wasting more than 20 pages (and google-ling for 3 days :-( ). I would ask you again for your...
7
by: DazedAndConfused | last post by:
I have a 8.5 x 11 landscape document with about 1/4 inch of space on the left and right where there is no print. The document displays perfect in print preview, but when I print it, about 1/2 inch...
0
by: Vincent | last post by:
Dear all, I have implemented a class to export the content of RichTextBox to image in WYSISYG mode so that line breaks on the screen are the same as exported. C# Code: public struct...
3
by: Birky | last post by:
I have a report which I am opening from a command button within a form. I am using the “DoCmd.OpenReport stDocName” command to open the report but it is automatically printing instead of popping the...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
1
by: mehdi | last post by:
Hi, Consider a printing scenario where I have to draw the entire page on a 827x1169 (.01 inch) size. Thereafter, the entire bitmap has to be resized to fill a given Bounds rectangle (keeping the...
5
by: Vani Perumal | last post by:
Hai All, I am working on Fingerprints. I reproduced fingerprints in C. I want to print it and also for matching I want to store the output as a JPEG file. For printing I tried PrintScreen, I...
10
by: sophie_newbie | last post by:
Hi, I'm trying to write a piece of code that spawns a thread and prints dots every half second until the thread spawned is finished. Code is something like this: import threading class...
7
by: Iain Wilson | last post by:
I am pulling my hair out trying to print various objects from a .net web page My apologies for cross posting but I need an answer and my previous post has attracted no interest. ASP.Net 2.0...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.