CDict is a Dict with lookup cascading into the next level Dict’s if the key is not found in the CDict itself.
(C) 2005,2008 Benedict Verhegghe Distributed under the GNU GPL version 3 or later
Classes defined in module mydict
A Python dictionary with default values and attribute syntax.
Dict is functionally nearly equivalent with the builtin Python dict, but provides the following extras:
There are a few caveats though:
Keys that are also attributes of the builtin dict type, can not be used with the attribute syntax to get values from the Dict. You should use the dictionary syntax to access these items. It is possible to set such keys as attributes. Thus the following will work:
C['get'] = 'foo'
C.get = 'foo'
print(C['get'])
but this will not:
print(C.get)
This is done so because we want all the dict attributes to be available with their normal binding. Thus,
print(C.get('get'))
will print foo
To avoid name clashes with user defines, many Python internal names start and end with ‘__’. The user should avoid such names. The Python dict has the following attributes not enclosed between ‘__’, so these are the ones to watch out for: ‘clear’, ‘copy’, ‘fromkeys’, ‘get’, ‘has_key’, ‘items’, ‘iteritems’, ‘iterkeys’, ‘itervalues’, ‘keys’, ‘pop’, ‘popitem’, ‘setdefault’, ‘update’, ‘values’.
Add a dictionary to the Dict object.
The data can be a dict or Dict type object.
Return the value for key or a default.
This is the equivalent of the dict get method, except that it returns only the default value if the key was not found in self, and there is no _default_ method or it raised a KeyError.
Replaces the setdefault function of a normal dictionary.
This is the same as the get method, except that it also sets the default value if get found a KeyError.
A cascading Dict: properties not in Dict are searched in all Dicts.
This is equivalent to the Dict class, except that if a key is not found and the CDict has items with values that are themselves instances of Dict or CDict, the key will be looked up in those Dicts as well.
As you expect, this will make the lookup cascade into all lower levels of CDict’s. The cascade will stop if you use a Dict. There is no way to guarantee in which order the (Cascading)Dict’s are visited, so if multiple Dicts on the same level hold the same key, you should know yourself what you are doing.
Add a dictionary to the Dict object.
The data can be a dict or Dict type object.
Return the value for key or a default.
This is the equivalent of the dict get method, except that it returns only the default value if the key was not found in self, and there is no _default_ method or it raised a KeyError.
Replaces the setdefault function of a normal dictionary.
This is the same as the get method, except that it also sets the default value if get found a KeyError.
Functions defined in module mydict
Format a dict in Python source representation.
Each (key,value) pair is formatted on a line of the form:
key = value
If all the keys are strings containing only characters that are allowed in Python variable names, the resulting text is a legal Python script to define the items in the dict. It can be stored on a file and executed.
This format is the storage format of the Config class.
Cascading lookup in a dictionary.
This is equivalent to the dict lookup, except that when the key is not found, a cascading lookup through lower level dict’s is started and the first matching key found is returned.
Always returns None.
Raise a KeyError.