467,122 Members | 1,258 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,122 developers. It's quick & easy.

sleep() function, perhaps.


Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".

[code]
import time

sleep(.5)
print "."
sleep(.5)
print "."
[end code]

But, it would (with those .5 second intervals)
print out much like the following.

..
(pause)
..
(pause)

I would rather those periods be on a single line, not printing on a new
line each time.

Any suggestions?

Thank you in advance,
~Ryan

Jul 18 '05 #1
  • viewed: 5358
Share:
12 Replies
"Ryan Spencer" <je***@earthlink.net> schrieb im Newsbeitrag
news:pa****************************@earthlink.net. ..
|
| Hello Everyone,
|
| I want to have a row of periods, separated by small, say, .5 second
| intervals between each other. Thus, for example, making it have the
| appearance of a progress "bar".
|
| [code]
| import time
|
| sleep(.5)
| print "."
| sleep(.5)
| print "."
| [end code]
|
| But, it would (with those .5 second intervals)
| print out much like the following.

|
| .
| (pause)
| .
| (pause)
|
| I would rather those periods be on a single line, not printing on a new
| line each time.

import time,sys

while 1:
sys.stdout.write(".")
time.sleep(0.5)

You could also use:

print ".",

(note the trailing comma)

but that will leave you with an additional space after the dot

HTH,

Vincent Wehren
| Any suggestions?
|
| Thank you in advance,
| ~Ryan
|
Jul 18 '05 #2
On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <je***@earthlink.net>
wrote:

Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".

[code]
import time

sleep(.5)
print "."
sleep(.5)
print "."
[end code]

But, it would (with those .5 second intervals)
print out much like the following.

.
(pause)
.
(pause)

I would rather those periods be on a single line, not printing on a new
line each time.

Any suggestions?


Try print with added comma or sys.stdout.write, like so:
import time
for i in range(10): .... print '\b.',
.... time.sleep(1.5)
....
........... import sys
for i in range(10):

.... sys.stdout.write('.')
.... time.sleep(0.5)
....
...........
--
Christopher
Jul 18 '05 #3
On Tue, 25 Nov 2003 06:14:20 +0000, Christopher Koppler wrote:
On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <je***@earthlink.net>
wrote:

Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".

[code]
import time

sleep(.5)
print "."
sleep(.5)
print "."
[end code]

But, it would (with those .5 second intervals)
print out much like the following.

.
(pause)
.
(pause)

I would rather those periods be on a single line, not printing on a new
line each time.

Any suggestions?


Try print with added comma or sys.stdout.write, like so:
import time
for i in range(10): ... print '\b.',
... time.sleep(1.5)
...
.......... import sys
for i in range(10):

... sys.stdout.write('.')
... time.sleep(0.5)
...
..........

Heya', Thanks,

Actually though, None of those suggestions give me the desired result
I was looking for. I used both with the for loops, even the one with the
while loop, and for the first suggested it prints all of them out on new
lines (as opposed to all on the same line as I'd been hoping for) and the
second posts on one full line, yet, the periods still don't have pauses
between themselves. Perhaps something else is amiss?

As well, the trailing commas gives the exact same result as doing the
sys.stdout.write function.

Is the code that you suggested giving you a result such as...

..(pause).(pause).(pause).

I raised everything up to a 1.5 second interval to exaggerate the results,
and I'm afraid I still don't notice the pauses.

Perchance I simply need to remove whatever is terminating the line?
Does the time.sleep() function itself terminate a line? It would seem
if I could bypass that, it would allow the pauses and keep the periods
on one line.

Thank you for your advice though, It's highly appreciated.

~Ryan
Jul 18 '05 #4
>>>>> "Ryan" == Ryan Spencer <je***@earthlink.net> writes:

Ryan> On Tue, 25 Nov 2003 06:14:20 +0000, Christopher Koppler wrote:
On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <je***@earthlink.net>
wrote:
Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".

[code] import time

sleep(.5) print "." sleep(.5) print "." [end code]

But, it would (with those .5 second intervals) print out much like
the following.

. (pause) . (pause)

I would rather those periods be on a single line, not printing on a
new line each time.

Any suggestions?

Try print with added comma or sys.stdout.write, like so:
> import time for i in range(10):

... print '\b.', ... time.sleep(1.5) ... ..........
> import sys for i in range(10):

... sys.stdout.write('.') ... time.sleep(0.5) ... ..........

Ryan> Heya', Thanks,

Ryan> Actually though, None of those suggestions give me the desired
Ryan> result I was looking for. I used both with the for loops, even the
Ryan> one with the while loop, and for the first suggested it prints all
Ryan> of them out on new lines (as opposed to all on the same line as
Ryan> I'd been hoping for) and the second posts on one full line, yet,
Ryan> the periods still don't have pauses between themselves. Perhaps
Ryan> something else is amiss?

Ryan> As well, the trailing commas gives the exact same result as doing
Ryan> the sys.stdout.write function.

Ryan> Is the code that you suggested giving you a result such as...

Ryan> .(pause).(pause).(pause).

Ryan> I raised everything up to a 1.5 second interval to exaggerate the
Ryan> results, and I'm afraid I still don't notice the pauses.

Ryan> Perchance I simply need to remove whatever is terminating the
Ryan> line? Does the time.sleep() function itself terminate a line? It
Ryan> would seem if I could bypass that, it would allow the pauses and
Ryan> keep the periods on one line.

Ryan> Thank you for your advice though, It's highly appreciated.

Without a newline character, the normal sys.stdout writes to a buffer and
won't try to flush it out. Try this:
for i in range(10):

.... sys.stdout.write('.')
.... sys.stdout.flush()
.... time.sleep(.5)
....
...........>>>

Regards,
Isaac.
Jul 18 '05 #5
Ryan Spencer wrote:
Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".


You've got the answer for dots, here's a spinner in case it's useful:

import sys, time

spinner="|/-\\"
pos=0

while 1:
sys.stdout.write("\r"+spinner[pos])
sys.stdout.flush()
time.sleep(.5)
pos+=1
pos%=4

Jul 18 '05 #6

Ryan> I want to have a row of periods, separated by small, say, .5
Ryan> second intervals between each other. Thus, for example, making it
Ryan> have the appearance of a progress "bar".

You might find my progress module at

http://www.musi-cal.com/~skip/python/progress.py

a good starting point. Something like

import progress, time
ticker = progress.Progress(major=1, minor=1, majormark='.')
while True:
ticker.tick()
time.sleep(0.5)

running in a separate thread should do what you want.

In many situations you want to actually measure progress of some
computation. If you can wedge in a call to ticker.tick() on each pass of
your main computation loop:

import progress, time
ticker = progress.Progress()
while some_condition_holds:
one_more_computational_step()
ticker.tick()

you can watch your computation progress.

This is particularly helpful if you know how many passes you need to make
around the loop:

import progress, time
number_of_passes = 10000
ticker = progress.Progress(title="(%d)" % number_of_passes)
for i in xrange(number_of_passes):
one_more_computational_step()
ticker.tick()

The title displayed tells you how many loops to expect and the dots and
numbers measure your progress:

(10000): .........1.........2.........3.........4

and when you delete the ticker or it goes out-of-scope, it displays the
total number of ticks (which might be lower if the loop was exited
prematurely).

There are more bells and whistles. Check the Progress class docstring for
full details.

Skip

Jul 18 '05 #7
Pa*****@Linux.ie wrote:

Ryan Spencer wrote:
Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".


You've got the answer for dots, here's a spinner in case it's useful:

import sys, time

spinner="|/-\\"
pos=0

while 1:
sys.stdout.write("\r"+spinner[pos])
sys.stdout.flush()
time.sleep(.5)
pos+=1
pos%=4


And a quicky OO version for kicks (untested):

class Spinner:
CHARS = r"|/-\"
def __init__(self, stream=sys.stdout):
self.index = 0
self.stream = stream
def spin(self):
self.stream.write('\r' + self.CHARS[self.index])
self.stream.flush()
self.index = (self.index + 1) % len(CHARS)

spin = Spinner()
while 1:
spin.spin()
time.sleep(0.5)

-Peter
Jul 18 '05 #8
Peter Hansen wrote:
And a quicky OO version for kicks (untested):

class Spinner:
CHARS = r"|/-\"
Python chokes when it encounters an odd number of backslashes at the end of
a raw string.
def __init__(self, stream=sys.stdout):
self.index = 0
self.stream = stream
def spin(self):
self.stream.write('\r' + self.CHARS[self.index])
self.stream.flush()
self.index = (self.index + 1) % len(CHARS)


Whenever you have to calculate an index, look into the itertools module
first; so here's my variant (2.3 only):

class Spinner:
def __init__(self, stream=sys.stdout, chars="|/-\\"):
self.cycle = itertools.cycle(["\r" + c for c in chars])
self.stream = stream
def spin(self):
self.stream.write(self.cycle.next())
self.stream.flush()

Peter
Jul 18 '05 #9
Peter Otten wrote:

Peter Hansen wrote:
And a quicky OO version for kicks (untested):

class Spinner:
CHARS = r"|/-\"
Python chokes when it encounters an odd number of backslashes at the end of
a raw string.


Good point. In that case, I'd prefer to re-order rather than
using the \\ form, as I (personally) find that somewhat unreadable
for short strings where each character is treated separately (as
is often the case with regex patterns, for example, or the above).
Whenever you have to calculate an index, look into the itertools module
first; so here's my variant (2.3 only):

class Spinner:
def __init__(self, stream=sys.stdout, chars="|/-\\"):
self.cycle = itertools.cycle(["\r" + c for c in chars])
self.stream = stream
def spin(self):
self.stream.write(self.cycle.next())
self.stream.flush()


Nice... someday I'll have to find time to look at that itertools module,
I guess. :-)

-Peter
Jul 18 '05 #10
On Tue, 25 Nov 2003 16:30:14 +0800, Isaac To wrote:

Without a newline character, the normal sys.stdout writes to a buffer
and won't try to flush it out. Try this:
for i in range(10):

... sys.stdout.write('.')
... sys.stdout.flush()
... time.sleep(.5)
...
..........>>>

Regards,
Isaac.


Hey!

Wow! Thanks for all the advice guys. To admit, I'm rather new to python,
but I am understanding thus far and working through all the great advice
you've all given. The spinner is actually very cool, I must say. Also, I
thank you Skip for your progress module, I'll play around with that in a
bit for actually working in coherence with the programs actual progress.

But flushing stdout's buffer does the trick, Which I'm really jazzed to
see. I'm still am heading back through all the other stuff that everyone
else has mentioned though, just to improve my knowledge of course.

I must say though, I previously posted this question on an on line message
board and received no feedback in a matter of weeks, and two days on this
newsgroup and BAM! Haha

Well, Thanks again guys, I'm sure I'll be back with other questions later
;)

~Ryan
Jul 18 '05 #11
Hello Once Again,

Well, Here's the beta, I'll say, for my "bogus" program. Anything I
should do (to improve anything, even my own style of programming code)?

[code]

#Python Excercise number one.
#Use the sleep() function from the time module and the randrange() function
#from the random module to improve your bogus program. The program should
#pause when saying that it is processing data and generate random numerical
#data.

import time, sys, whrandom

#Random numerical data (rnd = Random Numerical Data)
rnd = whrandom.randrange(5, 150)

print "\nRunning system administration clean-up, please wait...\n"

#Progress bar
for i in range(10):
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(.5)

print ".\n"

print "System administration clean-up complete,", rnd,"MB of space freed from temporary information files.\n"
print "Executing system tune-up procedure, please wait...\n"
time.sleep(5)

#passcode = raw_input("Please enter your root password: ")

#rootpass = open("/home/ryan/main/programming/testfile", "w")
#rootpass.writelines(passcode)
#rootpass.close()

print "Thank you. System tune-up will now continue..."
time.sleep(5)
print "\nSystem tune-up complete. Your system is now running better."
print "Thank you for your patience.\n"

[end code]

I commented out the password part, I was playing around with writing to
files and decided to add that in for the heck of it.

How does it look?

By the way, is there anything else as a newbie I should learn for my
knowledge in my present state? I'm just truly trying to find help. Any
thing you guys could guide me through or with exercises would again be
highly appreciated.

The only background I've had is programming in some C, but small stuff.

Thanks all, Your a tremendous help!

~Ryan
Jul 18 '05 #12
Peter Hansen wrote:
Pa*****@Linux.ie wrote:
Ryan Spencer wrote:
Hello Everyone,

I want to have a row of periods, separated by small, say, .5 second
intervals between each other. Thus, for example, making it have the
appearance of a progress "bar".


You've got the answer for dots, here's a spinner in case it's useful:

import sys, time

spinner="|/-\\"
pos=0

while 1:
sys.stdout.write("\r"+spinner[pos])
sys.stdout.flush()
time.sleep(.5)
pos+=1
pos%=4

And a quicky OO version for kicks (untested):


I thought you mean't an " .o0O" version initially
(like cdparanoia)

:-)

Pádraig.

Jul 18 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Alo Sarv | last post: by
11 posts views Thread by Yeounkun, Oh | last post: by
10 posts views Thread by Alfonso Morra | last post: by
5 posts views Thread by Sinan Nalkaya | last post: by
reply views Thread by Tim Golden | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.