Skip to content

Instantly share code, notes, and snippets.

@Averroes
Created April 10, 2015 14:14
Show Gist options
  • Select an option

  • Save Averroes/f0e38b5ceef15f6af636 to your computer and use it in GitHub Desktop.

Select an option

Save Averroes/f0e38b5ceef15f6af636 to your computer and use it in GitHub Desktop.
working with multiple mappings as a single mapping
# example.py
#
# Example of combining dicts into a chainmap
a = {'x': 1, 'z': 3 }
b = {'y': 2, 'z': 4 }
# (a) Simple example of combining
from collections import ChainMap
c = ChainMap(a,b)
print(c['x']) # Outputs 1 (from a)
print(c['y']) # Outputs 2 (from b)
print(c['z']) # Outputs 3 (from a)
# Output some common values
print('len(c):', len(c))
print('c.keys():', list(c.keys()))
print('c.values():', list(c.values()))
# Modify some values
c['z'] = 10
c['w'] = 40
del c['x']
print("a:", a)
# Example of stacking mappings (like scopes)
values = ChainMap()
values['x'] = 1
# Add a new mapping
values = values.new_child()
values['x'] = 2
# Add a new mapping
values = values.new_child()
values['x'] = 3
print(values)
print(values['x'])
# Discard last mapping
values = values.parents
print(values)
print(values['x'])
# Discard last mapping
values = values.parents
print(values)
print(values['x'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment