Skip to content

Instantly share code, notes, and snippets.

@yeger00
Created October 2, 2020 10:16
Show Gist options
  • Save yeger00/e8a71ce644bcaeacacd78e6867f4f29a to your computer and use it in GitHub Desktop.
Save yeger00/e8a71ce644bcaeacacd78e6867f4f29a to your computer and use it in GitHub Desktop.
Set of Python utility functions
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