dalcde / cloudpickle-generators

cloudpickle support for generators, including partially consumed generators.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cloudpickle-generators

cloudpickle support for generators, including partially consumed generators.

Usage

To use cloudpickle-generators, you must simply call register. After calling register, you may use cloudpickle like normal.

>>> import cloudpickle
>>> import cloudpickle_generators

# register the generator support
>>> cloudpickle_generators.register()

# define a simple generator function that has both intermediate state (the
# loop) and arguments
>>> def f(a, b):
...     for n in range(a):
...         yield b + b[-1] * n

>>> gen = f(4, 'ay')

# advance the generator half way though before serializing
>>> next(gen)
ay
>>> next(gen)
ayy

# do a serialization round trip
>>> new_gen = cloudpickle.loads(cloudpickle.dumps(gen))
>>> new_gen is gen
False

# advance the newly created generator instance
>>> next(new_gen)
ayyy
>>> next(new_gen)
ayyyy
>>> next(new_gen)
Traceback (most recent call last):
   ...
StopIteration

# the old instance is unaffected by the new one!
>>> next(gen)
ayyy
>>> next(gen)
ayyyy
>>> next(gen)
Traceback (most recent call last):
   ...
StopIteration

About

cloudpickle support for generators, including partially consumed generators.

License:BSD 3-Clause "New" or "Revised" License


Languages

Language:Python 59.9%Language:C 40.1%