hbutils.collection.stacked¶
StackedMapping¶
-
class
hbutils.collection.stacked.
StackedMapping
(*mps: collections.abc.Mapping)[source]¶ - Overview:
Stacked mapping data structure.
Multiple mapping-liked object (such as
dict
) can be stacked together and accessed like one mapping object.
Note
StackedMapping
is readonly. The__setitem__
and__delitem__
’s behaviour cannot be defined, because which dict to write or delete can not be determined.- Examples::
>>> from hbutils.collection import StackedMapping >>> >>> d1 = {'a': 1, 'b': 2} >>> d2 = {'b': 3, 'c': 4, 'd': 5} >>> d3 = {'c': 6, 'e': 7} >>> s = StackedMapping(d1, d2, d3) ## stack together, d3 > d2 > d1 >>> >>> for key, value in s.items(): # __iter__ ... print(key, value) a 1 b 3 c 6 d 5 e 7 >>> s['a'] # __getitem__, from d1['a'] 1 >>> s['b'] # from d2['b'], d2 > d1 3 >>> s['c'] # from d3['c'], d3 > d2 6 >>> s['d'] # from d2['d'] 5 >>> s['e'] # from d3['e'] 7 >>> 'c' in s # __contains__ True >>> 'f' in s # 'f' not found in neither d1, d2 nor d3 False >>> s == {'a': 1, 'b': 3, 'c': 6, 'd': 5, 'e': 7} # __iter__ True >>> len(s) # __len__ 5 >>> >>> d2['c'] = 11 ## update original dicts >>> del d2['b'] >>> del d3['c'] >>> del d1['b'] >>> >>> s['a'] # __getitem__, from d1['a'] 1 >>> s['b'] # 'b' nor found in neither d1, d2 nor d3 KeyError: 'b' >>> s['c'] # from d2['c'] 11 >>> s['d'] # from d2['d'] 5 >>> s['e'] # from d3['e'] 7 >>> len(s) # 'b' is no longer here 4
-
__init__
(*mps: collections.abc.Mapping)[source]¶ Constructor of
StackedMapping
.Later mappings will be stacked on top of the earlier ones.
- Parameters:
mps – Multiple mapping objects.