Re: [Tutor] "Print" behaviour inside a loop?

2005-06-12 Thread Kent Johnson
Simon Gerber wrote:
> I got it working with
> 
> while ip_addr == "":
> sys.stdout.writelines("...")
> sys.stdout.flush()
> time.sleep(0.5)
> ip_addr = get_addr()
> if ip_addr != '':
>   ...

You should use sys.stdout.write(), not writelines(). writelines() expects a 
sequence of strings so what you wrote is roughly equivalent to
  sys.stdout.write(".")
  sys.stdout.write(".")
  sys.stdout.write(".")

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Print" behaviour inside a loop?

2005-06-12 Thread Simon Gerber

>Just use sys.stdout.writelines(' ... ')
>
>Example:
>  >>> import sys
>  >>> sys.stdout.writelines ('... '); sys.stdout.writelines ('... ')
>  ... ... >>>
>  
>
Thank you for the suggestion Lee, but I'm afraid that doesn't work 
either. Same problems as described before. Max Noel hit the nail right 
on the head, though. I got it working with

while ip_addr == "":
sys.stdout.writelines("...")
sys.stdout.flush()
time.sleep(0.5)
ip_addr = get_addr()
if ip_addr != '':
  ...

Script works like a charm now! Still rife with bugs and problems that 
would make it more or less useless for anyone except me - but a 
perfectly good, pptp config program already exists. I just made my own 
for education reasons.

Anyone curious can view the whole thing here:

http://members.optusnet.com.au/~sger/pytp

I'd welcome any constructive criticism. Also, I'm storing a lot of 
passwords in plain text files. I hope I got all the permissions stuff 
right, but again, if anyone happens to spot any obvious flaws please do 
share.

Regards,

-- 
"Come back to the workshop and dance cosmological models with me?"
 - Peer, "Permutation City", by Greg Egan. 

Simon Gerber
[EMAIL PROTECTED]

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Print" behaviour inside a loop?

2005-06-11 Thread Chinook
On Sat, 11 Jun 2005 19:42:27 -0400, Simon Gerber wrote
(in article <[EMAIL PROTECTED]>):

> Hullo,
> 
> Firstly, thanks to everyone who helped me find my own IP address. That 
> was a big help. That bit is working now, and working very nicely. I am 
> now stuck on something purely aesthetic - printing a few dots across the 
> screen to provide a bit of feedback while the VPN tunnel is being 
> established.
> 
> def vpn_connect(choice):
> import time
> ip_addr = ""
> tries = 0
> retries = 10
> print "Connecting to %s" % (choice),
> os.system("pon %s" % (choice))
> while ip_addr == "" and tries < retries:
> print "...", # This is the line causing problems
> time.sleep(0.5)
> ip_addr = get_addr()
> if ip_addr != '':
>   #create the route! 
>   pass
> else:
>   tries += 1
> sys.exit()
> 
> It works. The problem is, nothing is displayed on screen until after the 
> connection occurs - at which point we see:
> 
> "Connecting to Prodigi ... ... ... ... ... ... ... "
> 
> If I remove the comma at the end of the marked line, the ellipses  print 
> every .5 seconds as desired, except they print down the screen of course!
> 
> After googling around a little, I found a post that seemed to say  
> Python won't draw the results of 'print' statements until it hits a 
> newline. I tried using sys.stout.write('...'), but had the same problem 
> there, too. I've also found a few progress bar classes around the web, 
> but I not exactly want a progress bar. I just want to print '...' every 
> few seconds until connected.
> 
> Any hints? I looked up the python reference manual, but couldn't find 
> any way to force print statements to draw. Should I be looking into 
> threads, perhaps?
> 
> Regards,
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

Just use sys.stdout.writelines(' ... ')

Example:
  >>> import sys
  >>> sys.stdout.writelines ('... '); sys.stdout.writelines ('... ')
  ... ... >>>

That what your looking for?
Lee C



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "Print" behaviour inside a loop?

2005-06-11 Thread Max Noel

On Jun 12, 2005, at 00:42, Simon Gerber wrote:

> Any hints? I looked up the python reference manual, but couldn't find
> any way to force print statements to draw. Should I be looking into
> threads, perhaps?

 I/O in Python is buffered -- that is, it uses RAM whenever  
possible to delay the actual reading/writing operations, which in  
turn speeds them up. For example, when you read a character from a  
file, a larger part of the file is stored in a RAM buffer so as to  
speed up subsequent calls.
 In the same way, whenever you write to stdout (or to a file),  
everything is stored in a buffer until a newline is reached or the  
buffer is manually flushed. The latter is what you're looking for,  
it's done by calling the file object's (in your case, stdout --  
remember, on UNIX everything is a file) flush method.
 Here's a small example:

#!/usr/bin/env python
import time, sys

for i in xrange(20):
 sys.stdout.write(".")
 sys.stdout.flush()
 time.sleep(0.2)


-- Max
maxnoel_fr at yahoo dot fr -- ICQ #85274019
"Look at you hacker... A pathetic creature of meat and bone, panting  
and sweating as you run through my corridors... How can you challenge  
a perfect, immortal machine?"

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] "Print" behaviour inside a loop?

2005-06-11 Thread Simon Gerber
Hullo,

Firstly, thanks to everyone who helped me find my own IP address. That 
was a big help. That bit is working now, and working very nicely. I am 
now stuck on something purely aesthetic - printing a few dots across the 
screen to provide a bit of feedback while the VPN tunnel is being 
established.

def vpn_connect(choice):
import time
ip_addr = ""
tries = 0
retries = 10
print "Connecting to %s" % (choice),
os.system("pon %s" % (choice))
while ip_addr == "" and tries < retries:
print "...", # This is the line causing problems
time.sleep(0.5)
ip_addr = get_addr()
if ip_addr != '':
  #create the route! 
  pass
else:
  tries += 1
sys.exit()

It works. The problem is, nothing is displayed on screen until after the 
connection occurs - at which point we see:

"Connecting to Prodigi ... ... ... ... ... ... ... "

If I remove the comma at the end of the marked line, the ellipses  print 
every .5 seconds as desired, except they print down the screen of course!

After googling around a little, I found a post that seemed to say  
Python won't draw the results of 'print' statements until it hits a 
newline. I tried using sys.stout.write('...'), but had the same problem 
there, too. I've also found a few progress bar classes around the web, 
but I not exactly want a progress bar. I just want to print '...' every 
few seconds until connected.

Any hints? I looked up the python reference manual, but couldn't find 
any way to force print statements to draw. Should I be looking into 
threads, perhaps?

Regards,


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor