blah | ||
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
def dict_to_obj(our_dict): | |
""" | |
Function that takes in a dict and returns a custom object associated with the dict. | |
This function makes use of the "__module__" and "__class__" metadata in the dictionary | |
to know which object type to create. | |
""" | |
if "__class__" in our_dict: | |
# Pop ensures we remove metadata from the dict to leave only the instance arguments | |
class_name = our_dict.pop("__class__") | |
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
def convert_to_dict(obj): | |
""" | |
A function takes in a custom object and returns a dictionary representation of the object. | |
This dict representation includes meta data such as the object's module and class names. | |
""" | |
# Populate the dictionary with object meta data | |
obj_dict = { | |
"__class__": obj.__class__.__name__, | |
"__module__": obj.__module__ |