ch02
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
# chainmap.py
|
||||
|
||||
|
||||
>>> from collections import ChainMap
|
||||
>>> default_connection = {'host': 'localhost', 'port': 4567}
|
||||
>>> connection = {'port': 5678}
|
||||
>>> conn = ChainMap(connection, default_connection) # map creation
|
||||
>>> conn['port'] # port is found in the first dictionary
|
||||
5678
|
||||
>>> conn['host'] # host is fetched from the second dictionary
|
||||
'localhost'
|
||||
>>> conn.maps # we can see the mapping objects
|
||||
[{'port': 5678}, {'host': 'localhost', 'port': 4567}]
|
||||
>>> conn['host'] = 'packtpub.com' # let's add host
|
||||
>>> conn.maps
|
||||
[{'port': 5678, 'host': 'packtpub.com'},
|
||||
{'host': 'localhost', 'port': 4567}]
|
||||
>>> del conn['port'] # let's remove the port information
|
||||
>>> conn.maps
|
||||
[{'host': 'packtpub.com'}, {'host': 'localhost', 'port': 4567}]
|
||||
>>> conn['port'] # now port is fetched from the second dictionary
|
||||
4567
|
||||
>>> dict(conn) # easy to merge and convert to regular dictionary
|
||||
{'host': 'packtpub.com', 'port': 4567}
|
||||
Reference in New Issue
Block a user