class DictToObject: # example: instead of yml['my_key'] we can use yml.my_key
'''Class to convert a dictionary into an object. This will allow us to access the dictionary values as object attributes.'''
def __init__(self, data):
self._data = data
def __getattr__(self, name):
if name in self._data:
return self._data[name]
else:
raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")
def __repr__(self):
return str(self._data)
Json Serializer
import json
def exception_serializer_for_json(obj):
'''Custom JSON serializer to handle non-serializable objects like exceptions'''
if isinstance(obj, Exception):
return str(obj)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")
logger.info(f'{json.dumps(local_vars, indent=4, skipkeys=True, default=exception_serializer_for_json)}')