sleep() function, perhaps.

Peter Hansen peter at engcorp.com
Tue Nov 25 13:59:58 EST 2003


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




More information about the Python-list mailing list