473,404 Members | 2,178 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,404 software developers and data experts.

SImple python print question

Hey there,

I have a simple question about python print statement. Take the
following code snippet for example...

1 print "-#- executing: %s" % section,
2 tests[section] = test.testcase(name=config.get(section,'name'))
3 tests[section].runTest()
4 printStatus(tests[section])

Now the problem is that line 1 does not get printed until line 4. What
I thought would happen is that line 1 gets executed and the user sees
that the statement that the test case is executing. Then after the
test case executes a "PASS" or "FAIL" appears on the same line as the
"-#- executing: 0053" statement.

e.g.
-#- executing: 0053 FAIL

Some tests take a long time to finish thus the screen is blank until
the entire test finishes and the above statement is outputted.

Thanks for any help.

Amit
Jun 27 '08 #1
4 1227
am********@gmail.com wrote:
Hey there,

I have a simple question about python print statement. Take the
following code snippet for example...

1 print "-#- executing: %s" % section,
2 tests[section] = test.testcase(name=config.get(section,'name'))
3 tests[section].runTest()
4 printStatus(tests[section])

Now the problem is that line 1 does not get printed until line 4. What
I thought would happen is that line 1 gets executed and the user sees
that the statement that the test case is executing. Then after the
test case executes a "PASS" or "FAIL" appears on the same line as the
"-#- executing: 0053" statement.

e.g.
-#- executing: 0053 FAIL

Some tests take a long time to finish thus the screen is blank until
the entire test finishes and the above statement is outputted.

Thanks for any help.
'print' sends its output to sys.stdout, which is buffered, and may not be
displayed immediately (because it's held in the buffer). To force the output to
be displayed, use flush():

print "-#- executing: %s" % section,
sys.stdout.flush()
...tests here...

Hope this helps!

--Hans
Jun 27 '08 #2
On May 16, 6:38 pm, amit.ut...@gmail.com wrote:
Hey there,

I have a simple question about python print statement. Take the
following code snippet for example...

1 print "-#- executing: %s" % section,
2 tests[section] = test.testcase(name=config.get(section,'name'))
3 tests[section].runTest()
4 printStatus(tests[section])

Now the problem is that line 1 does not get printed until line 4. What
I thought would happen is that line 1 gets executed and the user sees
that the statement that the test case is executing. Then after the
test case executes a "PASS" or "FAIL" appears on the same line as the
"-#- executing: 0053" statement.

e.g.
-#- executing: 0053 FAIL

Some tests take a long time to finish thus the screen is blank until
the entire test finishes and the above statement is outputted.

Your standard output uses line-buffering, which means that the
underlying I/O code stores all the output in memory until it gets a
newline, only then does it send the output to the terminal (or
console, or whatever).

Workarounds to this are as follows:

1. Explicity flush the buffer after any print statements that end with
a comma:

print "whatever",
sys.stdout.flush()

2. Run Python in unbuffered mode, by using the -u switch:

python -u yourscript.py
Carl Banks
Jun 27 '08 #3
On May 16, 4:02 pm, Hans Nowak <zephyrfalcon!NO_SP...@gmail.com>
wrote:
amit.ut...@gmail.com wrote:
Hey there,
I have a simple question about python print statement. Take the
following code snippet for example...
1 print "-#- executing: %s" % section,
2 tests[section] = test.testcase(name=config.get(section,'name'))
3 tests[section].runTest()
4 printStatus(tests[section])
Now the problem is that line 1 does not get printed until line 4. What
I thought would happen is that line 1 gets executed and the user sees
that the statement that the test case is executing. Then after the
test case executes a "PASS" or "FAIL" appears on the same line as the
"-#- executing: 0053" statement.
e.g.
-#- executing: 0053 FAIL
Some tests take a long time to finish thus the screen is blank until
the entire test finishes and the above statement is outputted.
Thanks for any help.

'print' sends its output to sys.stdout, which is buffered, and may not be
displayed immediately (because it's held in the buffer). To force the output to
be displayed, use flush():

print "-#- executing: %s" % section,
sys.stdout.flush()
...tests here...

Hope this helps!

--Hans
Thanks a lot!

This worked beautifully!
Jun 27 '08 #4
On May 16, 4:03 pm, Carl Banks <pavlovevide...@gmail.comwrote:
On May 16, 6:38 pm, amit.ut...@gmail.com wrote:
Hey there,
I have a simple question about python print statement. Take the
following code snippet for example...
1 print "-#- executing: %s" % section,
2 tests[section] = test.testcase(name=config.get(section,'name'))
3 tests[section].runTest()
4 printStatus(tests[section])
Now the problem is that line 1 does not get printed until line 4. What
I thought would happen is that line 1 gets executed and the user sees
that the statement that the test case is executing. Then after the
test case executes a "PASS" or "FAIL" appears on the same line as the
"-#- executing: 0053" statement.
e.g.
-#- executing: 0053 FAIL
Some tests take a long time to finish thus the screen is blank until
the entire test finishes and the above statement is outputted.

Your standard output uses line-buffering, which means that the
underlying I/O code stores all the output in memory until it gets a
newline, only then does it send the output to the terminal (or
console, or whatever).

Workarounds to this are as follows:

1. Explicity flush the buffer after any print statements that end with
a comma:

print "whatever",
sys.stdout.flush()

2. Run Python in unbuffered mode, by using the -u switch:

python -u yourscript.py

Carl Banks
Thanks for the reply. This worked as expected. I did not know about
the -u switch, this is good stuff.

Amit
Jun 27 '08 #5

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

Similar topics

16
by: Eric | last post by:
I would want to obtain a list of factors (multiples of 2) given a prime number in python. For example 13=, 5=, 7=, 15= I would appreciate a fuction which would do this. Eric
20
by: Matthew Thorley | last post by:
My friend sent me an email asking this: > I'm attemtping to decide which scripting language I should master and > was wondering if it's possible to do > these unixy awkish commands in python:...
38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
9
by: vedrandekovic | last post by:
Hello, I have one question about string.I am trying to make an function to analyze line of some text, this is my example: "HELLO;HELLO2:WORLD:", if that function in this text find ";" and ":" (...
13
by: Bryan | last post by:
Hi, I just started with python, and have a for loop question In c++ (or a number of other languages) I can do this: for (int i=0, j=0; i < i_len, j< j_len; ++i, ++j) {} If I have this in...
3
by: writser | last post by:
hey all, For my study I'm writing a simple threaded webcrawler and I am trying to do this in python. But somehow, using threads causes IDLE to crash on Windows XP (with the latest python...
1
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner:...
6
by: skazhy | last post by:
hi, i am new to python, so i've a really simple question about dictionaries. if i have a dictionary and I make have an input after it (to input numbers) can i get the key of value that was in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.