Last active
May 12, 2022 11:13
-
-
Save travishen/0920b5ddb86e161a01b52d478e0047a3 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
class FrozenJSON: | |
"""A read-only facade for nevigating a JSON-like object""" | |
def __init__(self, mapping): | |
self.__data = dict(mapping) | |
def __getattr__(self, name): | |
if hasattr(self.__data, name): | |
return getattr(self.__data, name) | |
else: | |
return FrozenJSON(self.__data[name]) | |
def __new__(cls, arg): | |
if isinstance(arg, abc.Mapping): | |
return cls(arg) | |
elif isinstance(arg, abc.MutableSequence): | |
return [cls.build(item) for item in arg] | |
else: | |
return arg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment