Created
December 20, 2017 12:38
-
-
Save reutsharabani/7c6e4850853d62e029b5e2677c5fd414 to your computer and use it in GitHub Desktop.
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 itertools import chain | |
import inspect | |
# very naive deastruct implementation (not native...) | |
def _destruct(selector): | |
def _(value): | |
if isinstance(selector, dict): | |
yield from chain.from_iterable(_destruct(selector[k])(value[k]) for k in selector) | |
return | |
if isinstance(selector, list): | |
yield from chain.from_iterable(_destruct(a)(b) for a, b in zip(selector, value)) | |
return | |
yield selector, value | |
return _ | |
def destruct(selector): | |
def _(value): | |
new_locals = dict(_destruct(selector)(value)) | |
# print('updating locals: %s' % new_locals) | |
inspect.currentframe().f_back.f_locals.update(new_locals) | |
return _ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample usage:
Now
my_list
is mapped to the list locally: