[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

printing dots in simple program while waiting

john.m.roach

1/9/2008 4:49:00 PM

Ok, so this should be a really simple thing to do, but I haven't been
able to get it on the first few tries and couldn't find anything after
searching a bit.

what i want to do is print a 'waiting' statement while a script is
working-- the multithreading aspect isn't an issue, the printing on
the same line is. i want to print something like:

(1sec) working...
(2sec) working....
(3sec) working.....


where the 'working' line isn't being printed each second, but the dots
are being added with time.

something like:

import time
s = '.'
print 'working'
while True:
print s
time.sleep(1)


however, this doesn't work since it prints:

working
.
.
.

any suggestions?
9 Answers

Martin Marcher

1/9/2008 4:57:00 PM

0

John wrote:

> import time
> s = '.'
> print 'working', # Note the "," at the end of the line
> while True:
> print s
> time.sleep(1)

see my comment in the code above...

if that's what you mean

/martin

--
http://noneisyours.ma...
http://feeds.feedburner.com/N...

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Tim Chase

1/9/2008 5:06:00 PM

0

Martin Marcher wrote:
> John wrote:
>
>> import time
>> s = '.'
>> print 'working', # Note the "," at the end of the line
>> while True:
>> print s, #Note the "," at the end of this line too...
>> time.sleep(1)
>
> see my comment in the code above...

see my added comment in the code above...

Though this will produce spaces between the dots:

waiting . . . . . .

To eliminate the spaces, you need to write to a file-stream such
as sys.stdout:

from sys import stdout
stdout.write('working')
while True:
stdout.write('.')
# might need something like stdout.flush() here
time.sleep(1)
stdout.write('\n')

-tkc



john.m.roach

1/9/2008 5:08:00 PM

0

On Jan 9, 11:56 am, Martin Marcher <mar...@marcher.name> wrote:
> John wrote:
> > import time
> > s = '.'
> > print 'working', # Note the "," at the end of the line
> > while True:
> > print s
> > time.sleep(1)
>
> see my comment in the code above...
>
> if that's what you mean
>
> /martin
>
> --http://noneisyours.marcher.namehttp://feeds.feedburner.com/N...
>
> You are not free to read this message,
> by doing so, you have violated my licence
> and are required to urinate publicly. Thank you.

Thanks for the input Martin, but I already tried that. If you put a
comma on that line it successfully prints the first '.' on the same
line, but the rest below. Like:

working .
.
.
.



I want:

working......


I have tried the comma thing on the "print s" line ("print s,"), but
then it doesn't print anything at all...

Reedick, Andrew

1/9/2008 5:15:00 PM

0



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Martin Marcher
> Sent: Wednesday, January 09, 2008 11:57 AM
> To: python-list@python.org
> Subject: Re: printing dots in simple program while waiting
>
> John wrote:
>
> > import time
> > s = '.'
> > print 'working', # Note the "," at the end of the line
> > while True:
> > print s
> > time.sleep(1)
>
> see my comment in the code above...
>
> if that's what you mean
>


Bah. The trailing command may prevent the newline, but it appends a
space whether you want it or not.[1] Use sys.stdout.write('.') instead.

import sys

print "wussy nanny state, tax 'n spend my spaces, liberal comma:"
for i in range(1, 10):
print '.',
print
print "manly neo-con I know what's Right so keep your government out of
my strings! print:"
for i in range(1, 10):
sys.stdout.write('.')

[1] Which has to be _the_ most annoying feature of Python. *grrr*

*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625


Dennis Lee Bieber

1/9/2008 5:52:00 PM

0

On Wed, 9 Jan 2008 09:07:48 -0800 (PST), John <john.m.roach@gmail.com>
declaimed the following in comp.lang.python:


>
> I have tried the comma thing on the "print s" line ("print s,"), but
> then it doesn't print anything at all...

Likely it does -- but the console is buffering it until it sees a
new-line...

Try using sys.stdout.write() followed by sys.stdout.flush() calls
instead of print statements
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Santiago Romero

1/9/2008 5:55:00 PM

0

On 9 ene, 17:48, John <john.m.ro...@gmail.com> wrote:
> i want to print something like:
>
> (1sec) working...
> (2sec) working....
> (3sec) working.....
>
> where the 'working' line isn't being printed each second, but the dots
> are being added with time.
>
> something like:
>
> import time
> s = '.'
> print 'working'
> while True:
> print s
> time.sleep(1)
>
> however, this doesn't work since it prints:
>
> working
> .
> .

Change

> print s

to

> print s,

(With the ending ",", which sends NO linefeed to stdout)

Bye :)

john.m.roach

1/9/2008 7:31:00 PM

0

On Jan 9, 12:14 pm, "Reedick, Andrew" <jr9...@ATT.COM> wrote:
> > -----Original Message-----
> > From: python-list-bounces+jr9445=att....@python.org [mailto:python-
> > list-bounces+jr9445=att....@python.org] On Behalf Of Martin Marcher
> > Sent: Wednesday, January 09, 2008 11:57 AM
> > To: python-l...@python.org
> > Subject: Re: printing dots in simple program while waiting
>
> > John wrote:
>
> > > import time
> > > s = '.'
> > > print 'working', # Note the "," at the end of the line
> > > while True:
> > > print s
> > > time.sleep(1)
>
> > see my comment in the code above...
>
> > if that's what you mean
>
> Bah. The trailing command may prevent the newline, but it appends a
> space whether you want it or not.[1] Use sys.stdout.write('.') instead.
>
> import sys
>
> print "wussy nanny state, tax 'n spend my spaces, liberal comma:"
> for i in range(1, 10):
> print '.',
> print
> print "manly neo-con I know what's Right so keep your government out of
> my strings! print:"
> for i in range(1, 10):
> sys.stdout.write('.')
>
> [1] Which has to be _the_ most annoying feature of Python. *grrr*
>
> *****
>
> The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625





Thanks for all of the help. This is what ended up working:



import time
import sys

s = '.'
sys.stdout.write( 'working' )
while True:
sys.stdout.write( s )
sys.stdout.flush()
time.sleep(0.5)



John Machin

1/9/2008 7:54:00 PM

0

On Jan 10, 6:30 am, John <john.m.ro...@gmail.com> wrote:
> On Jan 9, 12:14 pm, "Reedick, Andrew" <jr9...@ATT.COM> wrote:
>
>
>
> > > -----Original Message-----
> > > From: python-list-bounces+jr9445=att....@python.org [mailto:python-
> > > list-bounces+jr9445=att....@python.org] On Behalf Of Martin Marcher
> > > Sent: Wednesday, January 09, 2008 11:57 AM
> > > To: python-l...@python.org
> > > Subject: Re: printing dots in simple program while waiting
>
> > > John wrote:
>
> > > > import time
> > > > s = '.'
> > > > print 'working', # Note the "," at the end of the line
> > > > while True:
> > > > print s
> > > > time.sleep(1)
>
> > > see my comment in the code above...
>
> > > if that's what you mean
>
> > Bah. The trailing command may prevent the newline, but it appends a
> > space whether you want it or not.[1] Use sys.stdout.write('.') instead.
>
> > import sys
>
> > print "wussy nanny state, tax 'n spend my spaces, liberal comma:"
> > for i in range(1, 10):
> > print '.',
> > print
> > print "manly neo-con I know what's Right so keep your government out of
> > my strings! print:"
> > for i in range(1, 10):
> > sys.stdout.write('.')
>
> > [1] Which has to be _the_ most annoying feature of Python. *grrr*
>
> > *****
>
> > The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA625
>
> Thanks for all of the help. This is what ended up working:
>
> import time
> import sys
>
> s = '.'
> sys.stdout.write( 'working' )
> while True:
> sys.stdout.write( s )
> sys.stdout.flush()
> time.sleep(0.5)

For your next trick, write a "spinner" using |/-\ in succession :-)

Alex VanderWoude

1/10/2008 5:49:00 AM

0

John wrote:
> what i want to do is print a 'waiting' statement while a script is
> working-- the multithreading aspect isn't an issue, the printing on
> the same line is. i want to print something like:
>
> (1sec) working...
> (2sec) working....
> (3sec) working.....
>
>
> where the 'working' line isn't being printed each second, but the dots
> are being added with time.

When issuing output to stdout I have do something like this:

print "working", # Note trailing comma
while some_condition:
do_something()
print "\b.", # \b is backspace
print # Finish the line of dots

- Alex