Skip to content

Instantly share code, notes, and snippets.

@travishen
Last active May 12, 2022 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travishen/0920b5ddb86e161a01b52d478e0047a3 to your computer and use it in GitHub Desktop.
Save travishen/0920b5ddb86e161a01b52d478e0047a3 to your computer and use it in GitHub Desktop.
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