django.utils.datastrctures DotExpandedDict var
Ben parantez tarafından sözlüğü ayrıştırır bunun çatal, yazdım ..
class BrExpandedDict(dict):
"""
A special dictionary constructor that takes a dictionary in which the keys
may contain brackets to specify inner dictionaries. It's confusing, but this
example should make sense.
>>> d = BrExpandedDict({'person[1][firstname]': ['Simon'], \
'person[1][lastname]': ['Willison'], \
'person[2][firstname]': ['Adrian'], \
'person[2][lastname]': ['Holovaty']})
>>> d
{'person': {'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}}
>>> d['person']
{'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}}
>>> d['person']['1']
{'lastname': ['Willison'], 'firstname': ['Simon']}
"""
def __init__(self, key_to_list_mapping):
for k, v in key_to_list_mapping.items():
current = self
k = k.replace(']', '')
bits = k.split('[')
for bit in bits[:-1]:
current = current.setdefault(bit, {})
# Now assign value to current position
try:
current[bits[-1]] = v
except TypeError: # Special-case if current isn't a dict.
current = {bits[-1]: v}
if __name__ == "__main__":
import doctest
doctest.testmod()