[Tutor] Writing to the terminal?

Karim karim.liateni at free.fr
Sat Dec 18 08:25:53 CET 2010


I suppose we can do something like that (kind of pseudo code) to fully 
configure it:


def doSomeStuff(*args):
       ...

def progressBar( func, *args)
       import time, sys
       f = sys.stdout
       for i in range(20):
             func.__call__(args)
             f.write('=')
             f.flush()  # make the change visible immediately
       else:
             f.write('\n')

progressBar( doSomeStuff )

Cheers
Karim

On 12/18/2010 08:05 AM, Karim wrote:
>
> Hello Steven,
>
> I added the pipe char '|' to have a complete spinner!
> This would be set as a function for my wait routine installer.
>
> Thanks to share!
> Karim
>
>
> On 12/10/2010 09:51 PM, Steven D'Aprano wrote:
>> Modulok wrote:
>>> List,
>>>
>>> Forgive me if I don't describe this well, I'm new to it:
>>
>> [snip description of a progress bar]
>>
>> Here's one way:
>>
>> import time, sys
>> f = sys.stdout
>> for i in range(20):
>>     time.sleep(1)  # do some work
>>     f.write('=')
>>     f.flush()  # make the change visible immediately
>> else:
>>     f.write('\n')
>>
>>
>> You might be able to get the same effect with print, but it's 
>> probably easier using sys.stdout directly.
>>
>> Here's another way, which *must* use stdout and not print.
>>
>> for i in range(20):
>>     percentage = i/20.0
>>     spinner = '/-\\-'[i % 4]
>>     f.write("Progress: %5.2f%% %s %s>\r" %
>>             (percentage, spinner, '='*(i+1)))
>>     f.flush()
>>     time.sleep(1)
>> else:
>>     f.write('\n')
>>
>>
>>
>>
>>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list