lenML/ChatTTS-Forge
301
1class JsonObject:2 def __init__(self, initial_dict=None):3 """4 Initialize the JsonObject with an optional initial dictionary.5 6 :param initial_dict: A dictionary to initialize the JsonObject.7 """8 # If no initial dictionary is provided, use an empty dictionary9 self._dict_obj = initial_dict if initial_dict is not None else {}10 11 if self._dict_obj is self:12 raise ValueError("JsonObject cannot be initialized with itself")13 14 def __getattr__(self, name):15 """16 Get an attribute value. If the attribute does not exist,17 look it up in the internal dictionary.18 19 :param name: The name of the attribute.20 :return: The value of the attribute.21 :raises AttributeError: If the attribute is not found in the dictionary.22 """23 try:24 return self._dict_obj[name]25 except KeyError:26 return None27 28 def __setattr__(self, name, value):29 """30 Set an attribute value. If the attribute name is '_dict_obj',31 set it directly as an instance attribute. Otherwise,32 store it in the internal dictionary.33 34 :param name: The name of the attribute.35 :param value: The value to set.36 """37 if name == "_dict_obj":38 super().__setattr__(name, value)39 else:40 self._dict_obj[name] = value41 42 def __delattr__(self, name):43 """44 Delete an attribute. If the attribute does not exist,45 look it up in the internal dictionary and remove it.46 47 :param name: The name of the attribute.48 :raises AttributeError: If the attribute is not found in the dictionary.49 """50 try:51 del self._dict_obj[name]52 except KeyError:53 return54 55 def __getitem__(self, key):56 """57 Get an item value from the internal dictionary.58 59 :param key: The key of the item.60 :return: The value of the item.61 :raises KeyError: If the key is not found in the dictionary.62 """63 if key not in self._dict_obj:64 return None65 return self._dict_obj[key]66 67 def __setitem__(self, key, value):68 """69 Set an item value in the internal dictionary.70 71 :param key: The key of the item.72 :param value: The value to set.73 """74 self._dict_obj[key] = value75 76 def __delitem__(self, key):77 """78 Delete an item from the internal dictionary.79 80 :param key: The key of the item.81 :raises KeyError: If the key is not found in the dictionary.82 """83 del self._dict_obj[key]84 85 def to_dict(self):86 """87 Convert the JsonObject back to a regular dictionary.88 89 :return: The internal dictionary.90 """91 return self._dict_obj92 93 def has_key(self, key):94 """95 Check if the key exists in the internal dictionary.96 97 :param key: The key to check.98 :return: True if the key exists, False otherwise.99 """100 return key in self._dict_obj101 102 def keys(self):103 """104 Get a list of keys in the internal dictionary.105 106 :return: A list of keys.107 """108 return self._dict_obj.keys()109 110 def values(self):111 """112 Get a list of values in the internal dictionary.113 114 :return: A list of values.115 """116 return self._dict_obj.values()117 118 def clone(self):119 """120 Clone the JsonObject.121 122 :return: A new JsonObject with the same internal dictionary.123 """124 return JsonObject(self._dict_obj.copy())125 126 def merge(self, other):127 """128 Merge the internal dictionary with another dictionary.129 130 :param other: The other dictionary to merge.131 """132 self._dict_obj.update(other)133 