In [1]:
import collections

Accessing Values

The ChainMap manages a sequence of dictionaries, and searches through them in the order they are given, to find values associated with keys.


In [5]:
a= {'a':'A','c':'C'}
b= {'b':'B','c':'D'}

m=collections.ChainMap(a,b)

print('Indivisual Values')
print('a={}'.format(m['a']))
print('b={}'.format(m['b']))
print('c={}'.format(m['c']))
print()

print('Keys={}'.format(list(m.keys())))
print('Value={}'.format(list(m.values())))

print()

print("Print Items")

for k,v in m.items():
    print('{}={}'.format(k,v))
    
print('"d" in m:{}'.format(('d' in m)))


Indivisual Values
a=A
b=B
c=C

Keys=['a', 'b', 'c']
Value=['A', 'B', 'C']

Print Items
a=A
b=B
c=C
"d" in m:False

Note The child mapping are searched in the order they are passed to the constructor, so the value reported for the key 'c' comes from the "a" dictionary.

Reordering

the ChainMap stores the list of mappings over which it searchs in a list in its maps attribute. This list is mutable. so it is possible to add new mappings directly or change the order of the elements to control lookup and update behavior


In [6]:
import collections

a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}

m = collections.ChainMap(a, b)

print(m.maps)
print('c = {}\n'.format(m['c']))

# reverse the list
m.maps = list(reversed(m.maps))

print(m.maps)
print('c = {}'.format(m['c']))


[{'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'}]
c = C

[{'b': 'B', 'c': 'D'}, {'a': 'A', 'c': 'C'}]
c = D

Updating Values

A ChainMap does not cache the values in the child mappings, thus, if their contents are modified, the results are reflected when ChainMap is accessed.


In [7]:
import collections

a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}

m = collections.ChainMap(a, b)
print('Before: {}'.format(m['c']))
a['c'] = 'E'
print('After : {}'.format(m['c']))


Before: C
After : E

ChainMap Provides a convenience method for creating a new instance with one extra mapping at the front of maps list to make it easy to avoid modifying the existing underlying data structures.


In [9]:
import collections

a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}

m1 = collections.ChainMap(a, b)
m2 = m1.new_child()

print('m1 before:', m1)
print('m2 before:', m2)

m2['c'] = 'E'

print('m1 after:', m1)
print('m2 after:', m2)


m1 before: ChainMap({'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'})
m2 before: ChainMap({}, {'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'})
m1 after: ChainMap({'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'})
m2 after: ChainMap({'c': 'E'}, {'a': 'A', 'c': 'C'}, {'b': 'B', 'c': 'D'})

for situation where the new context is known or buit in advance, it is also possible to pass a mapping to `new_child()'


In [10]:
import collections

a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
c = {'c': 'E'}

m1 = collections.ChainMap(a, b)
m2 = m1.new_child(c)

print('m1["c"] = {}'.format(m1['c']))
print('m2["c"] = {}'.format(m2['c']))


m1["c"] = C
m2["c"] = E

we can also skip the first map in the search. parents Property returning a new ChainMap containing all of the maps in the current instance except the first one.


In [14]:
import collections

a = {'a': 'A', 'c': 'C'}
b = {'b': 'B', 'c': 'D'}
c = {'e': 'E', 'f': 'F'}


m1 = collections.ChainMap(a, b,c)
m1.parents


Out[14]:
ChainMap({'b': 'B', 'c': 'D'}, {'e': 'E', 'f': 'F'})

In [ ]: