Last active
December 13, 2015 22:08
-
-
Save cesarkawakami/4982621 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def _mturk_flatten(obj): | |
if isinstance(obj, basestring): | |
return {"": obj} | |
elif isinstance(obj, collections.Mapping): | |
iterable = obj.items() | |
elif isinstance(obj, collections.Iterable): | |
iterable = enumerate(obj) | |
else: | |
return {"": obj} | |
rv = {} | |
for key, value in iterable: | |
rv.update({"{}.{}".format(key, inner_key): inner_value | |
for inner_key, inner_value in mturk_flatten(value).items()}) | |
return rv | |
def mturk_flatten(obj): | |
return {key[:-1]: value for key, value in _mturk_flatten(obj).items()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment