Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stacking generators
#1
So I have a class where an algorithm in a method of the base class needs to iterate things that will depend on the derived class:

class BaseClass(object):
    def __init__(self,string):
        self.string=string

    def iterateChars(self):
        print ','.join([c for c in self.generator()])
This is easily implemented when the derived class has a simple generator:

class DerivedWithSingleGenerator(BaseClass):
    def __init__(self,string):
        super(DerivedWithSingleGenerator,self).__init__(string)
        
    def generator(self):
        for c in '+'.join([c for c in self.string]):
            yield c
Now, things get complicated because another derived class may require two rather different generators (and I don't really want to define different classes just for this). The best I can come up with is to have the generator expected by the parent class select one of the possible generators and iterate it:

       
class DerivedWithComplexGenerator(BaseClass):
    def __init__(self,string,uc):
        super(DerivedWithComplexGenerator,self).__init__(string)
        self.uc=uc
        
    # one of the possible generators
    def lcGenerator(self):
        for c in self.string.lower():
            yield c
            
    # the other possible generator
    def ucGenerator(self):
        for c in self.string.upper():
            yield c
        
    # So this generator ends up iterating another generator 
    def generator(self):
        gen=self.ucGenerator() if self.uc else self.lcGenerator()
        for c in gen:
            yield c
Some test code:

sg=DerivedWithSingleGenerator('aBcdEFg')
sg.iterateChars()

cgu=DerivedWithComplexGenerator('aBcdEFg',True)
cgu.iterateChars()

cgl=DerivedWithComplexGenerator('aBcdEFg',False)
cgl.iterateChars()
The code above is a bit artificial but demonstrates the problem. IRL I'm iterating strokes in Gimp paths, and in some cases re-splitting the strokes...

So, the question: can you think of a better way? Can there be  some unforeseen problem when a generator iterates another? I also thought about a "factory" method in the derived class to return the generator to use but it makes the simple case more complex just to support the more complex case.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#2
Why not just return the appropriate generator:

def generator(self):
    if self.uc:
        return self.ucGenerator()
    else:
        return self.lcGenerator()
Then you're not generating over a generator, you're just providing the correct generator to the original iteration.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-18-2016, 08:21 PM)ichabod801 Wrote: Why not just return the appropriate generator:

def generator(self):
    if self.uc:
        return self.ucGenerator()
    else:
        return self.lcGenerator()
Then you're not generating over a generator, you're just providing the correct generator to the original iteration.

** Slaps forehead ** Doh... In my "factory" method I was returning self.ucGenerator and not self.ucGenerator(). These parens make a huge difference...

ichabod801.virtual_beers+=1
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Stacking Problem in a game. HelloTobi22 2 960 Aug-05-2022, 09:48 AM
Last Post: HelloTobi22
  Image Stacking Help Rhys_SSD 2 2,614 Feb-25-2019, 10:49 AM
Last Post: Rhys_SSD
  2 iterators or generators Skaperen 1 28,588 Jan-04-2018, 07:41 AM
Last Post: Gribouillis
  Using Generators and their expressions BeerLover 3 4,487 Mar-18-2017, 11:06 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020