collectionish.AncestorChainMap

class collectionish.AncestorChainMap(*maps)

Bases: collections.ChainMap

ChainMap variant that allows for the insertion of parents and ancestors.

Example

>>> from collectionish import AncestorChainMap
>>>
>>> child = AncestorChainMap({'b': 3, 'c': 3})
>>> child
AncestorChainMap({'b': 3, 'c': 3})

add oldest ancestor:

>>> child.add_ancestor({'a': 1, 'b': 1})
>>> child
AncestorChainMap({'b': 3, 'c': 3}, {'a': 1, 'b': 1})

add direct parent:

>>> child.add_parent({'a': 2, 'b': 2})
>>> child
AncestorChainMap({'b': 3, 'c': 3}, {'a': 2, 'b': 2}, {'a': 1, 'b': 1})

Methods:

__init__(*maps)

Initialize a ChainMap by setting maps to the given mappings. If no mappings are provided, a single empty dictionary is used.

add_ancestor(ancestor: Union[Dict[KT, VT], collections.ChainMap])

add a oldest ancestor to the current chain map

add_parent(parent: Union[Dict[KT, VT], collections.ChainMap])

add a parent to the current chain map

clear()

Clear maps[0], leaving maps[1:] intact.

copy()

New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]

classmethod fromkeys(iterable, *args)

Create a ChainMap with a single dict created from the iterable.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
new_child(m=None)

New ChainMap with a new map followed by all previous maps. If no map is provided, an empty dict is used.

pop(key, *args)

Remove key from maps[0] and return its value. Raise KeyError if key not in maps[0].

popitem()

Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from mapping/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, v in F.items(): D[k] = v

values() → an object providing a view on D's values

Attributes:

parents ].
add_ancestor(ancestor: Union[Dict[KT, VT], collections.ChainMap])

add a oldest ancestor to the current chain map

add_parent(parent: Union[Dict[KT, VT], collections.ChainMap])

add a parent to the current chain map

clear()

Clear maps[0], leaving maps[1:] intact.

copy()

New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]

classmethod fromkeys(iterable, *args)

Create a ChainMap with a single dict created from the iterable.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
new_child(m=None)

New ChainMap with a new map followed by all previous maps. If no map is provided, an empty dict is used.

parents

].

Type:New ChainMap from maps[1
pop(key, *args)

Remove key from maps[0] and return its value. Raise KeyError if key not in maps[0].

popitem()

Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from mapping/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, v in F.items(): D[k] = v

values() → an object providing a view on D's values