Forked from kkirsche/python-convert-dictionary-to-object
Created
August 24, 2018 11:18
-
-
Save pasystem/5091d7699cc7bb6cf9e02bb4d7b409fe to your computer and use it in GitHub Desktop.
Convert a dictionary to an object (recursive) for python 3.
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
class DictionaryUtility: | |
""" | |
Utility methods for dealing with dictionaries. | |
""" | |
@staticmethod | |
def to_object(item): | |
""" | |
Convert a dictionary to an object (recursive). | |
""" | |
def convert(item): | |
if isinstance(item, dict): | |
return type('jo', (), {k: convert(v) for k, v in item.items()}) | |
if isinstance(item, list): | |
def yield_convert(item): | |
for index, value in enumerate(item): | |
yield convert(value) | |
return list(yield_convert(item)) | |
else: | |
return item | |
return convert(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment