1111import logging
1212logger = logging .getLogger ('dill._utils' )
1313
14- import inspect
15- from functools import partialmethod
16-
1714class AttrDict (dict ):
1815 """syntactic sugar for accessing dictionary items"""
19- _CAST = object () # singleton
20- def __init__ (self , * args , ** kwargs ):
21- data = args [0 ] if len (args ) == 2 and args [1 ] is self ._CAST else dict (* args , ** kwargs )
22- for key , val in tuple (data .items ()):
23- if isinstance (val , dict ) and not isinstance (val , AttrDict ):
24- data [key ] = AttrDict (val , self ._CAST )
25- super ().__setattr__ ('_data' , data )
2616 def _check_attr (self , name ):
2717 try :
2818 super ().__getattribute__ (name )
@@ -33,33 +23,17 @@ def _check_attr(self, name):
3323 def __getattr__ (self , key ):
3424 # This is called only if dict.__getattribute__(key) fails.
3525 try :
36- return self . _data [key ]
26+ return self [key ]
3727 except KeyError :
3828 raise AttributeError ("'AttrDict' object has no attribute %r" % key )
3929 def __setattr__ (self , key , value ):
4030 self ._check_attr (key )
41- if isinstance (value , dict ):
42- self ._data [key ] = AttrDict (value , self ._CAST )
43- else :
44- self ._data [key ] = value
31+ self [key ] = value
4532 def __delattr__ (self , key ):
4633 self ._check_attr (key )
47- del self ._data [key ]
48- def __proxy__ (self , method , * args , ** kwargs ):
49- return getattr (self ._data , method )(* args , ** kwargs )
34+ del self [key ]
5035 def __reduce__ (self ):
51- return AttrDict , (self ._data ,)
52- def copy (self ):
53- # Deep copy.
54- copy = AttrDict (self ._data )
55- for key , val in tuple (copy .items ()):
56- if isinstance (val , AttrDict ):
57- copy [key ] = val .copy ()
58- return copy
59-
60- for method , _ in inspect .getmembers (dict , inspect .ismethoddescriptor ):
61- if method not in vars (AttrDict ) and method not in {'__getattribute__' , '__reduce_ex__' }:
62- setattr (AttrDict , method , partialmethod (AttrDict .__proxy__ , method ))
36+ return type (self ), (dict (self ),)
6337
6438
6539### Namespace filtering
0 commit comments