473,387 Members | 1,542 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,387 software developers and data experts.

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
12 5668
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
11
by: Yeounkun, Oh | last post by:
Hello. Sleep (x) function make a process sleep during x seconds. but, how to sleep during milliseconds... Pls. help me. Thank you. Regards.
6
by: Angus Comber | last post by:
Hello I am testing and think I have a timing issue. On Windows I used sleep function to wait a while. Is there a C Runtime equivalent? I need to port some code to Linux. Angus
10
by: Alfonso Morra | last post by:
Hi, I need help witht he sleep function as follows. I need to be write som code to do the ff: 1. creates a new thread 2. (in the new thread), Sleep for x milliseconds 3. (in the new thread),...
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
17
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
2
by: fltcpt | last post by:
After reading the many posts on the newsgroup, I still disagree with the people who claim you will never need a sleep(), or that a sleep() is a bad idea, or that sleep() does not fit in the event...
7
by: multicherry | last post by:
Hi, Having searched for a way to fetch a window object by name, all I came across were answers along the line of... "All you have to do is say windowObj = window.open("blah", "name");" which...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.