Created
March 2, 2018 03:55
-
-
Save kbni/2af872cb87f26d754a22aa04f70f08fc to your computer and use it in GitHub Desktop.
AutoMuncher for munch
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 munch import munchify, Munch | |
def automunchify(data): | |
return munchify(data, AutoMunch) | |
class AutoMunch(Munch): | |
""" For convenience, we'll use this instad of the standard Munch so if we add a dict | |
into the Munch that dict itself will be munchified. | |
See Since my PR was unworthy: https://github.com/Infinidat/munch/pull/27 | |
""" | |
def __setattr__(self, k, v): | |
""" Works the same as Munch.__setattr__ but if you supply | |
a dictionary as value it will convert it to another Munch. | |
""" | |
if isinstance(v, (AutoMunch, Munch)): | |
v = automunchify(v.toDict()) | |
elif isinstance(v, dict): | |
v = automunchify(v) | |
elif isinstance(v, list): | |
v = [automunchify(li) for li in v] | |
super(AutoMunch, self).__setattr__(k, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment