Created
October 2, 2020 10:16
-
-
Save yeger00/e8a71ce644bcaeacacd78e6867f4f29a to your computer and use it in GitHub Desktop.
Set of Python utility functions
This file contains hidden or 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 to_type(o, new_type): | |
''' | |
Helper funciton that receives an object or a dict and convert it to a new given type. | |
:param object|dict o: The object to convert | |
:param Type new_type: The type to convert to. | |
''' | |
if new_type == type(o): | |
return o | |
else: | |
return new_type(**o | |
class MyEncoder(json.JSONEncoder): | |
""" | |
Encodes an object in JSON | |
""" | |
def default(self, o): # pylint: disable=E0202 | |
return o.__dict__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment