@@ -236,87 +236,6 @@ def update(d1, d2):
236236 # if it is any kind of object
237237 d1 = deepcopy (d2 )
238238
239- class SuperDict (dict ):
240- """
241- A dictionary accessible with the dot notation
242-
243- a['foo'] <=> a.foo
244-
245- except for standard methods
246- """
247-
248- def __init__ (self , * args , ** kwargs ):
249- # Call the superclass's __init__ method
250- super ().__init__ (* args , ** kwargs )
251- self .__post_init__ ()
252-
253- def __post_init__ (self ):
254- "Recursively transform sub-dictionary"
255- for key , value in self .items ():
256- if isinstance (value , dict ):
257- self [key ] = SuperDict (value )
258-
259- def __getattr__ (self , name :str ):
260- "Allow dot notation on reading"
261- ERR_MSG = "Cannot find attribute '%s'" % name
262- # if name.startswith('_'):
263- # raise AttributeError(ERR_MSG)
264- try :
265- return self [name ]
266- except KeyError :
267- raise AttributeError (ERR_MSG )
268-
269- def __setattr__ (self , name , value ):
270- "Allow dot notation on writing"
271- # ERR_MSG = "Cannot assign an attribute starting with _ ('%s')" % name
272- # if name.startswith('_'):
273- # raise AttributeError(ERR_MSG)
274- self [name ] = value
275-
276- @property
277- def _attributes (self ):
278- "Make a list of the valid attributes"
279- return list (self .keys ())
280-
281- def _codewords (self ):
282- "Make a list of the codewords"
283- return
284-
285- def __dir__ (self ):
286- "List all attributes (for autocompletion, etc.)"
287- return super ().__dir__ () + self ._attributes
288-
289-
290-
291- # -------------------------------------
292- # Output
293- # -------------------------------------
294-
295- def to_json (self ):
296- "Convert to json"
297- return json .dumps (self , cls = CustomEncoder )
298-
299- def to_hjson (self ):
300- """
301- Convert to hjson
302- """
303- python_dict = json .loads (self .to_json ())
304- return hjson .dumps (python_dict )
305-
306-
307- def __str__ (self ):
308- "Print a superdict"
309- return self .to_hjson ()
310- return self .to_yaml ()
311- # r = [f"{self.__class__.__name__}:"]
312- # r.extend([f" - {key}: {value}" for key, value in self.items()])
313- # return("\n".join(r))
314-
315- def __rich__ (self ):
316- "Print a superdict (for rich)"
317- r = [f"[bold red]{ self .__class__ .__name__ } :[/]" ]
318- r .append (self .to_hjson ())
319- return ("\n " .join (r ))
320239
321240
322241
0 commit comments