sleep() function, perhaps.

R

Ryan Spencer

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
 
V

vincent wehren

|
| 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
|
 
C

Christopher Koppler

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?[/QUOTE]

Try print with added comma or sys.stdout.write, like so:
....     print '\b.',
....     time.sleep(1.5)
....
...............     sys.stdout.write('.')
....     time.sleep(0.5)
....
...........
 
R

Ryan Spencer

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?[/QUOTE]

Try print with added comma or sys.stdout.write, like so:
...     print '\b.',
...     time.sleep(1.5)
...
.............     sys.stdout.write('.')
...     time.sleep(0.5)
...
..........[/QUOTE]


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
 
I

Isaac To

>> On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <[email protected]>
>> 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?[/QUOTE]
    >>  Try print with added comma or sys.stdout.write, like so:
    >> [QUOTE]
>>>>> import time for i in range(10):[/QUOTE]
    >> ...  print '\b.', ...  time.sleep(1.5) ...  ..........[QUOTE]
>>>>> import sys for i in range(10):[/QUOTE]
    >> ...  sys.stdout.write('.')  ...  time.sleep(0.5) ...  ..........[/QUOTE][/QUOTE]


    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:
....   sys.stdout.write('.')
....   sys.stdout.flush()
....   time.sleep(.5)
.... 
...........>>> 

Regards,
Isaac.
 
S

Skip Montanaro

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
 
P

Padraig

Ryan said:
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
 
P

Peter Hansen

Ryan said:
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
 
P

Peter Otten

Peter said:
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
 
P

Peter Hansen

Peter said:
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
 
R

Ryan Spencer

Without a newline character, the normal sys.stdout writes to a buffer
and won't try to flush it out. Try this:

... 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
 
R

Ryan Spencer

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
 
P

Padraig

Peter said:
Ryan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top