[Tutor] Generator function question?

John Clark clajo04 at mac.com
Sat May 7 14:52:51 CEST 2005


Okay, take two...

import sys

def neverEndingStatus():
     def statusGen():
          index = 0
          statusChars = ['|', '\\', '-', '/']
          while True:
               yield statusChars[index]
               index = (index + 1) % 4
     try: 
          neverEndingStatus.x
     except AttributeError:
          neverEndingStatus.x = statusGen()
     
     return neverEndingStatus.x.next()
     

def Test1():
    for w in range(1, 100):
        sys.stderr.write('\b'+neverEndingStatus())
        for z in range(1, 10000):
            z = z + 1

def Test2():
    for y in range(1, 10):
        sys.stderr.write('\b \nTesting'+str(y)+'\t')
        Test1()
    
Test2()

This borrows (read: shamelessly copies) the implementation of the generator
function that Max suggested, it removes the global variable, and it removes
the need for the client code to call .next() to get the next status
character.  However, this is my first foray into the world of function
attributes and local functions, so this may not be the best way to do this -
any suggestions / critiques?

Thanks,
-jdc


On 5/7/05 8:00 AM, "Max Noel" <maxnoel_fr at yahoo.fr> wrote:

> 
> On May 7, 2005, at 12:28, John Clark wrote:
> 
>> My question - the creation of the global variable x seems kludged -
>> when I
>> first started with this I thought I was going to be coding
>> something like
>> sys.stderr.write('\b'+neverEndingStatus()) but that just returns
>> '\' each
>> time and I now recognize why.  I am just thinking there has to be a
>> way to
>> encapsulate the generator function, the global variable and
>> the .next() call
>> so that it is much cleaner in the client code.
>> 
>> My guess is that I could create a class, override the __call__
>> method and
>> get to the point where my code is something like:
>> sys.stderr.write('\b'+statusClass()) but that seems to be heavy
>> (implementing a class) and intrusive (overriding the __call__
>> method seems a
>> bit heavy handed) - my reluctance may be just because I am less
>> familiar
>> with those particular constructs in Python, but it still feels like
>> I should
>> be able to do this more cleanly without that.
>> 
>> Also - I look at the code in the neverEndingStatus() function and I
>> just
>> _know_ there has to be a better way to write that - it's just not
>> coming to
>> me.  Any suggestions are embarrassingly appreciated...
>> 
>> Thanks,
>> -jdc
>> 
> 
>      This is (IMO) more elegant:
> 
> def neverEndingStatus():
>      index = 0
>      statusChars = ['|', '\\', '-', '/']
>      while True:
>          yield statusChars[index]
>          index = (index + 1) % 4
> 
> 
>      As for how to access it, use a for loop (for i in
> neverEndingStatus()). xrange, for example, is a generator function.
> 
> -- 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?"
> 




More information about the Tutor mailing list