473,756 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5699
"Ryan Spencer" <je***@earthlin k.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.writ e(".")
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***@earthlin k.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.writ e, like so:
import time
for i in range(10): .... print '\b.',
.... time.sleep(1.5)
....
........... import sys
for i in range(10):

.... sys.stdout.writ e('.')
.... 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***@earthlin k.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.writ e, like so:
import time
for i in range(10): ... print '\b.',
... time.sleep(1.5)
...
.......... import sys
for i in range(10):

... sys.stdout.writ e('.')
... 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.writ e function.

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

..(pause).(paus e).(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***@earthlin k.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***@earthlin k.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.writ e, like so:
> import time for i in range(10):

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

... sys.stdout.writ e('.') ... 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.writ e 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.writ e('.')
.... sys.stdout.flus h()
.... 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.writ e("\r"+spinne r[pos])
sys.stdout.flus h()
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.Progre ss(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.Progre ss()
while some_condition_ holds:
one_more_comput ational_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_passe s = 10000
ticker = progress.Progre ss(title="(%d)" % number_of_passe s)
for i in xrange(number_o f_passes):
one_more_comput ational_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.i e 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.writ e("\r"+spinne r[pos])
sys.stdout.flus h()
time.sleep(.5)
pos+=1
pos%=4


And a quicky OO version for kicks (untested):

class Spinner:
CHARS = r"|/-\"
def __init__(self, stream=sys.stdo ut):
self.index = 0
self.stream = stream
def spin(self):
self.stream.wri te('\r' + self.CHARS[self.index])
self.stream.flu sh()
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.stdo ut):
self.index = 0
self.stream = stream
def spin(self):
self.stream.wri te('\r' + self.CHARS[self.index])
self.stream.flu sh()
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.stdo ut, chars="|/-\\"):
self.cycle = itertools.cycle (["\r" + c for c in chars])
self.stream = stream
def spin(self):
self.stream.wri te(self.cycle.n ext())
self.stream.flu sh()

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.stdo ut, chars="|/-\\"):
self.cycle = itertools.cycle (["\r" + c for c in chars])
self.stream = stream
def spin(self):
self.stream.wri te(self.cycle.n ext())
self.stream.flu sh()


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

-Peter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
18750
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 nanosleep(), depending on platform }
11
47114
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
8875
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
2008
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), After time elapsed, call a function via a callback 4. ability to kill the spawned thread.
5
3105
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 i used time.sleep in while, the function in while never executed. then i found something about Timer but couldnt realize any algorithm in my mind.
17
6431
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. Note, the Windows task manager shows 2 CPUs on the Performance tab with hyper-threading is turned on. Both Python 2.3.5 and 2.4.3 (downloaded from python.org) have this problem. The operating system is MS Windows XP Professional.
2
2585
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 driven model, etc.. Fine, not sleep, but at least a blocking function - that's whats missing in javascript, and I bet that's what's most people looking for sleep() had wanted.
7
1986
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 isn't very useful if you want to fetch information from an existing document in the window. One solution I came up with was quite simple; open the "new" window, giving you a reference to the object, then use window.history.back() to
0
1403
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 reproduces your problem is: <code> import signal
0
9275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10034
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.