Package tulip :: Package transys :: Module mathset :: Class TypedDict
[frames] | no frames]

Class TypedDict


dict subclass where values can be constrained by key.

For each key, a domain can optionally be defined, which restricts the admissible values that can be paired with that key.

Example

>>> d = TypedDict()
>>> allowed_values = {'name': {'Maria', 'John'},
                      'age': range(122)}
>>> default_values = {'name': 'Maria',
                      'age': 30}
>>> d.set_types(allowed_types)
>>> d.update(default_values)
Instance Methods
new empty dictionary

__init__(self, *args, **kwargs)
x.__init__(...) initializes x; see help(type(x)) for signature
 
__setitem__(self, i, y)
Raise ValueError if value y not allowed for key i.
 
__str__(self)
str(x)
None
update(self, *args, **kwargs)
Update D from dict/iterable E and F.
D.get(k,d), also set D[k]=d if k not in D
setdefault(self, key, value=None)
 
set_types(self, allowed_values)
Restrict values the key can be paired with.
bool
is_consistent(self)
Check if typed keys have consistent values.

Inherited from dict: __cmp__, __contains__, __delitem__, __eq__, __ge__, __getattribute__, __getitem__, __gt__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __repr__, __sizeof__, clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, values, viewitems, viewkeys, viewvalues

Inherited from object: __delattr__, __format__, __reduce__, __reduce_ex__, __setattr__, __subclasshook__

Class Variables

Inherited from dict: __hash__

Properties

Inherited from object: __class__

Method Details

__init__(self, *args, **kwargs)
(Constructor)

 

x.__init__(...) initializes x; see help(type(x)) for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

__setitem__(self, i, y)
(Index assignment operator)

 

Raise ValueError if value y not allowed for key i.

Overrides: dict.__setitem__

__str__(self)
(Informal representation operator)

 

str(x)

Overrides: object.__str__
(inherited documentation)

update(self, *args, **kwargs)

 

Update D from dict/iterable E and F. If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

Returns: None
Overrides: dict.update
(inherited documentation)

setdefault(self, key, value=None)

 
Returns: D.get(k,d), also set D[k]=d if k not in D
Overrides: dict.setdefault
(inherited documentation)

set_types(self, allowed_values)

 

Restrict values the key can be paired with.

Parameters:
  • allowed_values - dict of the form:
           {key : values}
    
       C{values} must implement C{__contains__}
       to enable checking validity of values.
    
       If C{values} is C{None},
       then any value is allowed.
    

is_consistent(self)

 

Check if typed keys have consistent values.

Use case: changing the object that allowed_values points to can invalidate the assigned values.

Returns: bool