[Python] return and reuse a python generator

Discussão em 'Python' iniciado por Stack, Junho 21, 2025.

  1. Stack

    Stack Membro Participativo

    So I am working on a parser that at the bottom - uses the "re.finditer()" to parse things, and I can make some stuff work that way. But I need to use this in a very different paradigm and I am confused

    What I have now is effectively this:

    class Foo():
    def parse_file( self, filename ):
    with open(filename,"rt") as f:
    text = f.read()
    yield re.finditer( patterns, text )


    I need to change this to something like this, but I can't seem to figure out how to do this. The "Yeild()" call - returns the token - not a generator.

    I need to do this - because in the middle of the "generator" operation my DSL (domain specific language) parser needs to deal with things like the C language: #if/#else/#endif and #include

    class Foo2():
    def parse_file( self, filename ):
    with open(filename,"rt") as f:
    text = f.read()
    self.generator = re.finditer( patterns, text )

    def next_token( self ):
    if self.generator is None:
    return None
    try:
    return next(self.generator)
    except StopItteration:
    self.generator = None
    return None

    Continue reading...

Compartilhe esta Página