print()

Dave Angel davea at ieee.org
Sat Oct 17 10:38:55 EDT 2009


mattia wrote:
> Il Fri, 16 Oct 2009 22:40:34 -0700, Dennis Lee Bieber ha scritto:
>
>   
>> On Fri, 16 Oct 2009 23:39:38 -0400, Dave Angel <davea at ieee.org>
>> declaimed the following in gmane.comp.python.general:
>>
>>
>>     
>>> You're presumably testing this in the interpreter, which prints extra
>>> stuff.  In particular, it prints the result value of any expressions
>>> entered at the interpreter prompt.  So if you type
>>>
>>> sys.stdout.write("hello")
>>>
>>> then after the write() method is done, the return value of the method
>>> (5) will get printed by the interpreter.
>>>
>>>       
>> 	I was about to respond that way myself, but before doing so I 
>>     
> wanted
>   
>> to produce an example in the interpreter window... But no effect?
>>
>> C:\Documents and Settings\Dennis Lee Bieber>python ActivePython 2.5.2.2
>> (ActiveState Software Inc.) based on Python 2.5.2 (r252:60911, Mar 27
>> 2008, 17:57:18) [MSC v.1310 32 bit (Intel)] on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>     
>>>>> import sys
>>>>> sys.stdout.write("hello")
>>>>>           
>> hello>>>
>>
>>
>> PythonWin 2.5.2 (r252:60911, Mar 27 2008, 17:57:18) [MSC v.1310 32 bit
>> (Intel)] on win32.
>> Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin'
>> for further copyright information.
>>     
>>>>> import sys
>>>>> sys.stdout.write("This is a test")
>>>>>           
>> This is a test
>>     
>>>>> print sys.stdout.write("Hello")
>>>>>           
>> HelloNone
>>     
>>>>>           
>> 	No count shows up... neither PythonWin or Windows command line/
>>     
> shell
>
> Indeed I'm using py3. But now everythong is fine. Everything I just 
> wanted to know was just to run this simple script (I've also sent the msg 
> 'putchar(8)' to the newsgroup):
>
> import time
> import sys
>
> val = ("|", "/", "-", "\\", "|", "/", "-", "\\")
> for i in range(100+1):
>     print("=", end="")
>     # print("| ", end="")
>     print(val[i%len(val)], " ", sep="", end="")
>     print(i, "%", sep="", end="")
>     sys.stdout.flush()
>     time.sleep(0.1)
>     if i > 9:    
>         print("\x08"*5, " "*5, "\x08"*5, sep="", end="")
>     else:
>         print("\x08"*4, " "*4, "\x08"*4, sep="", end="")
> print(" 100%\nDownload complete!")  
>
>   
Seems to me you're spending too much energy defeating the things that 
print() is automatically doing for you.  The whole point of write() is 
that it doesn't do anything but ship your string to the file/device.  So 
if you want control, do your own formatting.

Consider:

import time, sys, itertools

val = ("|", "/", "-", "\\", "|", "/", "-", "\\")
sys.stdout.write("     ")
pattern = "\x08"*8 + "   {0}{1:02d}%"
for percentage, string in enumerate(itertools.cycle(val)):
    if percentage>99 : break
    paddednum = pattern.format(string, percentage)
    sys.stdout.write(paddednum)
    sys.stdout.flush()
    time.sleep(0.1)
print("\x08\x08\x08\x08 100%\nDownload complete!")


Note the use of cycle() which effectively repeats a list indefinitely.  
And enumerate, which makes an index for you automatically when you're 
iterating through a list.  And  str.format() that builds our string, 
including using 0 padding so the percentages are always two digits.


DaveA



More information about the Python-list mailing list