Skip to content

Instantly share code, notes, and snippets.

@Wiper-R
Last active July 11, 2020 04:10
Show Gist options
  • Save Wiper-R/a26672a806204c1dbbfd9da806862e4f to your computer and use it in GitHub Desktop.
Save Wiper-R/a26672a806204c1dbbfd9da806862e4f to your computer and use it in GitHub Desktop.
dict_ = {"User" :{"Name" : "SQWiperYT", "ID" : 10, "Other" : {"ID" : 1}}}
import json
class OurObject:
def __init__(self, /, **kwargs):
self.__dict__.update(kwargs)
def __repr__(self):
keys = sorted(self.__dict__)
items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
return "{}({})".format(type(self).__name__, ", ".join(items))
def __eq__(self, other):
return self.__dict__ == other.__dict__
x = json.dumps(dict_)
y = json.loads(x, object_hook=lambda d: OurObject(**d))
y.User
>>> OurObject(ID=10, Name='SQWiperYT', Other=OurObject(ID=1))
y.User.ID
>>> 10
y.User.Name
>>> 'SQWiperYT'
y.User.Other.ID
>>> 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment