scallop dome pyformex logo

Previous topic

68. olist — Some convenient shortcuts for common list operations.

Next topic

70. odict — Specialized dictionary type structures.

[FSF Associate Member]

Valid XHTML 1.0 Transitional

69. mydict

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

class mydict.Dict(data={}, default=None)

A Python dictionary with default values and attribute syntax.

Dict is functionally nearly equivalent with the builtin Python dict, but provides the following extras:

  • Items can be accessed with attribute syntax as well as dictionary syntax. Thus, if C is a Dict, C['foo'] and C.foo are equivalent. This works as well for accessing values as for setting values. In the following, the terms key or attribute therefore have the same meaning.
  • Lookup of a nonexisting key/attribute does not automatically raise an error, but calls a _default_ lookup method which can be set by the user. The default is to raise a KeyError, but an alternative is to return None or some other default value.

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’.

update(data={}, **kargs)

Add a dictionary to the Dict object.

The data can be a dict or Dict type object.

get(key, default)

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.

setdefault(key, default)

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.

class mydict.CDict(data={}, default=<function returnNone at 0x4eaecf8>)

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.

update(data={}, **kargs)

Add a dictionary to the Dict object.

The data can be a dict or Dict type object.

get(key, default)

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.

setdefault(key, default)

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

mydict.formatDict(d)

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.

mydict.cascade(d, key)

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.

mydict.returnNone(key)

Always returns None.

mydict.raiseKeyError(key)

Raise a KeyError.