Skip to content

Instantly share code, notes, and snippets.

@IlyasYOY
Created October 25, 2018 13:40
Show Gist options
  • Save IlyasYOY/12c761a23059670fc6985221e06646f6 to your computer and use it in GitHub Desktop.
Save IlyasYOY/12c761a23059670fc6985221e06646f6 to your computer and use it in GitHub Desktop.
Helpful generator to iterable converter.
from typing import Generator
def iterize(generator: Generator):
"""
This function creates an iterable object from the generator you pass here,
it helps when function expects an iterable, but you have a generator.
:param generator: generator that you want to wrap.
:return: an iterable object.
"""
return type('_wrappedGenerator', (), {
'__iter__': lambda self: generator
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment