In [1]:
from cascadict import CascaDict
fruit_properties = CascaDict({'type':'fruit', 'taste':'sweet', 'color':"I don't have any color, I'm an abstract concept!"})
blueberry = fruit_properties.cascade({'name': 'blueberry', 'color':'blue'})
print(blueberry['color'])
print(blueberry['taste'])
Internally, it's not just copy and append, one can also access all the cascaded values:
In [2]:
print(blueberry.get_cascaded('color'))
Cascaded ancestors are referenced, not copied:
In [3]:
fruit_properties['taste'] = 'bittersweet'
blueberry['taste']
Out[3]:
CascaDicts can be nested, and any dict element put into CascaDict is also nested as CascaDict:
In [4]:
fruit_properties['classification'] = {'kingdom': 'Plantae',}
blueberry['classification'] = {'Order': 'Ericales', 'Family': 'Ericaceae', 'Genus': 'Vaccinium', 'Section': 'Cyanococcus'}
blueberry['classification']['kingdom']
Out[4]:
CascaDicts are of course iterable...
In [5]:
for key, value in blueberry.items():
print(key,value)
... and picklable
In [6]:
import pickle
blueberry_jam = pickle.loads(pickle.dumps(blueberry))
for key, value in blueberry_jam.items():
print(key,value)
If needed, CascaDict can be "flattened" into normal (nested) dict:
In [7]:
blueberry.copy_flat()
Out[7]:
Or only the top (final) level of CascaDict, without any ancestor properties, can be copied:
In [8]:
blueberry.copy_flat(level='skim')
Out[8]:
Combined with (e.g.) yaml, it makes any configuration processing a breeze:
In [9]:
import yaml
config = '''
defaults:
port: 5556
login_required: True
logging:
level: DEBUG
handler: stream
process_1:
max_runtime: 100
login_required: False
logging:
handler: file
process_2:
port: 6005
halt_on_error: True
logging:
level: ERROR
'''
raw_config = yaml.load(config)
defaults = CascaDict(raw_config.pop('defaults'))
properties = {} #no dict comprehension, remember Python 2.7 folk
for k,v in raw_config.items():
properties[k] = CascaDict(v, ancestor=defaults)
for k,v in properties.items():
print("{0}: {1}".format(k, v.copy_flat()))
This whole thing is just one small file, works in both Python 2.7 and 3.x and is released under MIT License. Now, cascade!
In [ ]: