471,050 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,050 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 1157
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Eric | last post: by
20 posts views Thread by Matthew Thorley | last post: by
38 posts views Thread by jrlen balane | last post: by
73 posts views Thread by Claudio Grondi | last post: by
9 posts views Thread by vedrandekovic | last post: by
13 posts views Thread by Bryan | last post: by
3 posts views Thread by writser | last post: by
1 post views Thread by =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post: by
6 posts views Thread by skazhy | last post: by
reply views Thread by leo001 | last post: by

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.