Created
October 25, 2018 13:40
-
-
Save IlyasYOY/12c761a23059670fc6985221e06646f6 to your computer and use it in GitHub Desktop.
Helpful generator to iterable converter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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