473,671 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 29516
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(obje ct):
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_calc ulations()
print "This is a parrot"
write it like this:

def parrot():
# This is good practice
do_lots_of_calc ulations()
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_calcula tions(result)
# or print it
print result
Otherwise, you can do something like this:

import cStringIO
import sys
capture_output = cStringIO.Strin gIO()
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(obje ct):
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...@g mail.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(obje ct):
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
9614
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 help. The problem is this: If I print a rectangle which begins at (0,0) and the margins are also set to 0 (l:0, t:0, r:0, b:0) then it prints fine (ok, not quite because 0,0 is inside the none printable area but I corrected for that by checking...
7
2474
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 on the right is not printed (leaving about 3/4 inch empty margin on the right side of the page). The left side starts about 1/4 inch in and prints just like I expect it to. I tried setting the margins to 0 and OriginAtMargins = True. The print...
0
11141
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 STRUCT_RECT
3
7488
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 report to the screen. How can I stop it from printing and just have the output sent to the screen? Any help would be greatly appreciated. Thanks Birky
8
5887
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 clients (Internet Explorer). My flash content was originally brought in via the “flash satay” method, but I have since used some server-side magic do deliver one <objecttag
1
2110
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 aspect ratio fixed). To do so, I've just wrote the following code in the PrintPage event: Graphics g = e.Graphics; Rectangle bounds = TheDestinationRectangle; Rectangle rc = new Rectangle(0, 0, (Int32)(827 * g.DpiX / 100), (Int32)
5
3978
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 toggled from the full screen mode then I tried PrintScreen. But nothing was worked out. Please tell me how to take printout. Also how to store the output as a JPEG file. This is very much needed for my research. So please post the answer if you know. ...
10
2108
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 MyThread ( threading.Thread ): def run ( self ): myLongCommand()...
7
2561
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 using C# This is a simple web page on our intranet (written as a test) and all I am trying to do is print a document to a named printer. If I use a StreamReader then I get an output to the printer. This
0
8471
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
8907
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
8817
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
8593
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
5687
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.