Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intercept the first value of a generator and transparently yield from the rest

Update: I've started a thread on python-ideas to propose additional syntax or a stdlib function for this purpose (i.e. specifying the first value sent by yield from). So far 0 replies... :/


How do I intercept the first yielded value of a subgenerator but delegate the rest of the iteration to the latter using yield from?

For example, suppose we have an arbitrary bidirectional generator subgen, and we want to wrap this in another generator gen. The purpose of gen is to intercept the first yielded value of subgen and delegate the rest of the generation—including sent values, thrown exceptions, .close(), etc.—to the sub-generator.

The first thing that might come to mind could be this:

def gen():
    g = subgen()

    first = next(g)
    # do something with first...
    yield "intercepted"

    # delegate the rest
    yield from g

But this is wrong, because when the caller .sends something back to the generator after getting the first value, it will end up as the value of the yield "intercepted" expression, which is ignored, and instead g will receive None as the first .send value, as part of the semantics of yield from.

So we might think to do this:

def gen():
    g = subgen()

    first = next(g)
    # do something with first...
    received = yield "intercepted"
    g.send(received)

    # delegate the rest
    yield from g

But what we've done here is just moving the problem back by one step: as soon as we call g.send(received), the generator resumes its execution and doesn't stop until it reaches the next yield statement, whose value becomes the return value of the .send call. So we'd also have to intercept that and re-send it. And then send that, and that again, and so on... So this won't do.

Basically, what I'm asking for is a yield from with a way to customize what the first value sent to the generator is:

def gen():
    g = subgen()

    first = next(g)
    # do something with first...
    received = yield "intercepted"

    # delegate the rest
    yield from g start with received  # pseudocode; not valid Python

...but without having to re-implement all of the semantics of yield from myself. That is, the laborious and poorly maintainable solution would be:

def adaptor(generator, init_send_value=None):
    send = init_send_value
    try:
        while True:
            send = yield generator.send(send)
    except StopIteration as e:
        return e.value

which is basically a bad re-implementation of yield from (it's missing handling of throw, close, etc.). Ideally I would like something more elegant and less redundant.

like image 586
Anakhand Avatar asked Dec 19 '20 11:12

Anakhand


People also ask

How do I find the next value of a generator?

To get values from the generator object, call the next() method on the generator object or loop through the generator object.

Does yield return a generator?

The yield statement returns a generator object to the one who calls the function which contains yield, instead of simply returning a value.

What does the yield statement in a generator do?

The yield keyword pauses generator function execution and the value of the expression following the yield keyword is returned to the generator's caller. It can be thought of as a generator-based version of the return keyword.

How does a generator return?

Simply speaking, a generator is a function that returns an object (iterator) which we can iterate over (one value at a time).


1 Answers

If you're trying to implement this generator wrapper as a generator function using yield from, then your question basically boils down to whether it is possible to specify the first value sent to the "yielded from" generator. Which it is not.

If you look at the formal specification of the yield from expression in PEP 380, you can see why. The specification contains a (surprisingly complex) piece of sample code that behaves the same as a yield from expression. The first few lines are:

_i = iter(EXPR)
try:
    _y = next(_i)
except StopIteration as _e:
    _r = _e.value
else:
    ...

You can see that the first thing that is done to the iterator is to call next() on it, which is basically equivalent to .send(None). There is no way to skip that step and your generator will always receive another None whenever yield from is used.

The solution I've come up with is to implement the generator protocol using a class instead of a generator function:

class Intercept:
    def __init__(self, generator):
        self._generator = generator
        self._intercepted = False

    def __next__(self):
        return self.send(None)

    def send(self, value):
        yielded_value = self._generator.send(value)

        # Intercept the first value yielded by the wrapped generator and 
        # replace it with a different value.
        if not self._intercepted:
            self._intercepted = True

            print(f'Intercepted value: {yielded_value}')

            yielded_value = 'intercepted'

        return yielded_value

    def throw(self, type, *args):
        return self._generator.throw(type, *args)

    def close(self):
        self._generator.close()

__next__(), send(), throw(), close() are described in the Python Reference Manual.

The class wraps the generator passed to it when created will mimic its behavior. The only thing it changes is that the first value yielded by the generator is replaced by a different value before it is returned to the caller.

We can test the behavior with an example generator f() which yields two values and a function main() which sends values into the generator until the generator terminates:

def f():
    y = yield 'first'
    print(f'f(): {y}')

    y = yield 'second'
    print(f'f(): {y}')

def main():
    value_to_send = 0
    gen = f()

    try:
        x = gen.send(None)

        while True:
            print(f'main(): {x}')

            # Send incrementing integers to the generator.
            value_to_send += 1
            x = gen.send(value_to_send)
    except StopIteration:
        print('main(): StopIteration')    
      
main()

When ran, this example will produce the following output, showing which values arrive in the generator and which are returned by the generator:

main(): first
f(): 1
main(): second
f(): 2
main(): StopIteration

Wrapping the generator f() by changing the statement gen = f() to gen = Intercept(f()), produces the following output, showing that the first yielded value has been replaced:

Intercepted value: first
main(): intercepted
f(): 1
main(): second
f(): 2

As all other calls to any of the generator API are forwarded directly to the wrapped generator, it should behave equivalently to the wrapped generator itself.

like image 159
Feuermurmel Avatar answered Oct 25 '22 20:10

Feuermurmel