472,958 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 28939
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.