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 12 5598
"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
|
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
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
>>>>> "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.
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
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 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
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
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
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
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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.
|
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
|
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),...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Matthew3360 |
last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
|
by: Matthew3360 |
last post by:
Hi,
I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
| | |