Skip to content

Instantly share code, notes, and snippets.

@cesarkawakami
Last active December 13, 2015 22:08
Show Gist options
  • Save cesarkawakami/4982621 to your computer and use it in GitHub Desktop.
Save cesarkawakami/4982621 to your computer and use it in GitHub Desktop.
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