Spaces:
Sleeping
Sleeping
File size: 3,318 Bytes
02e90e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
class JsonObject:
def __init__(self, initial_dict=None):
"""
Initialize the JsonObject with an optional initial dictionary.
:param initial_dict: A dictionary to initialize the JsonObject.
"""
# If no initial dictionary is provided, use an empty dictionary
self._dict_obj = initial_dict if initial_dict is not None else {}
def __getattr__(self, name):
"""
Get an attribute value. If the attribute does not exist,
look it up in the internal dictionary.
:param name: The name of the attribute.
:return: The value of the attribute.
:raises AttributeError: If the attribute is not found in the dictionary.
"""
try:
return self._dict_obj[name]
except KeyError:
return None
def __setattr__(self, name, value):
"""
Set an attribute value. If the attribute name is '_dict_obj',
set it directly as an instance attribute. Otherwise,
store it in the internal dictionary.
:param name: The name of the attribute.
:param value: The value to set.
"""
if name == "_dict_obj":
super().__setattr__(name, value)
else:
self._dict_obj[name] = value
def __delattr__(self, name):
"""
Delete an attribute. If the attribute does not exist,
look it up in the internal dictionary and remove it.
:param name: The name of the attribute.
:raises AttributeError: If the attribute is not found in the dictionary.
"""
try:
del self._dict_obj[name]
except KeyError:
return
def __getitem__(self, key):
"""
Get an item value from the internal dictionary.
:param key: The key of the item.
:return: The value of the item.
:raises KeyError: If the key is not found in the dictionary.
"""
if key not in self._dict_obj:
return None
return self._dict_obj[key]
def __setitem__(self, key, value):
"""
Set an item value in the internal dictionary.
:param key: The key of the item.
:param value: The value to set.
"""
self._dict_obj[key] = value
def __delitem__(self, key):
"""
Delete an item from the internal dictionary.
:param key: The key of the item.
:raises KeyError: If the key is not found in the dictionary.
"""
del self._dict_obj[key]
def to_dict(self):
"""
Convert the JsonObject back to a regular dictionary.
:return: The internal dictionary.
"""
return self._dict_obj
def has_key(self, key):
"""
Check if the key exists in the internal dictionary.
:param key: The key to check.
:return: True if the key exists, False otherwise.
"""
return key in self._dict_obj
def keys(self):
"""
Get a list of keys in the internal dictionary.
:return: A list of keys.
"""
return self._dict_obj.keys()
def values(self):
"""
Get a list of values in the internal dictionary.
:return: A list of values.
"""
return self._dict_obj.values()
|