Merge-able dict.
Note
JSON Pointer: http://tools.ietf.org/html/rfc6901
Parse and decode given encoded JSON Pointer expression, convert ~1 to / and ~0 to ~.
>>> jsnp_unescape("/a~1b")
'/a/b'
>>> jsnp_unescape("~1aaa~1~0bbb")
'/aaa/~bbb'
Parse path expression and return list of path items.
Parameters: |
|
---|---|
Returns: | A list of keys to fetch object[s] later. |
>>> parse_path('')
[]
>>> parse_path('/') # JSON Pointer spec expects this behavior.
['']
>>> parse_path('/a') == parse_path('.a') == ['a']
True
>>> parse_path('a') == parse_path('a.') == ['a']
True
>>> parse_path('/a/b/c') == parse_path('a.b.c') == ['a', 'b', 'c']
True
>>> parse_path('abc')
['abc']
getter for nested dicts.
Parameters: |
|
---|---|
Returns: | A tuple of (result_object, error_message) |
>>> d = {'a': {'b': {'c': 0, 'd': [1, 2]}}, '': 3}
>>> get(d, '/') # key becomes '' (empty string).
(3, '')
>>> get(d, "/a/b/c")
(0, '')
>>> sorted(get(d, "a.b")[0].items())
[('c', 0), ('d', [1, 2])]
>>> (get(d, "a.b.d"), get(d, "/a/b/d/1"))
(([1, 2], ''), (2, ''))
>>> get(d, "a.b.key_not_exist")
(None, "'...'")
>>> get(d, "/a/b/d/2")
(None, 'list index out of range')
>>> get(d, "/a/b/d/-")
(None, 'list indices must be integers...')
Make a nested dict iteratively.
Parameters: |
|
---|
>>> mk_nested_dic("a.b.c", 1)
{'a': {'b': {'c': 1}}}
>>> mk_nested_dic("/a/b/c", 1)
{'a': {'b': {'c': 1}}}
setter for nested dicts.
Parameters: |
|
---|
>>> d = MergeableDict.create(dict(a=1, b=dict(c=2, )))
>>> set_(d, 'a.b.d', 3)
>>> d['a']['b']['d']
3
Parameters: | obj – Any object may be an instance of MergeableDict or dict. |
---|
>>> is_dict_like("a string")
False
>>> is_dict_like({})
True
>>> is_dict_like(create_from({}))
True
Convert a MergeableDict instances to a dict object.
Borrowed basic idea and implementation from bunch.unbunchify. (bunch is distributed under MIT license same as this module.)
Parameters: | mdict – A MergeableDict instance |
---|---|
Returns: | A dict |
Try creating a MergeableDict instance[s] from a dict or any other objects.
Parameters: | dic – A dict instance |
---|
Bases: dict
Dict based object supports ‘merge’ operation.
Create an instance from any object.
Create an object from MergeableDict instances
Merge strategy
Update members recursively based on given strategy.
Parameters: |
|
---|
Update and replace self w/ other if both has same keys.
Parameters: | other – object of which type is same as self’s. |
---|
>>> md0 = MergeableDict.create(dict(a=1, b=[1, 3], c="abc"))
>>> md1 = MergeableDict.create(dict(a=2, b=[0, 1], c="xyz"))
>>> md0.update_w_replace(md1)
>>> all(md0[k] == md1[k] for k in ("a", "b", "c"))
True
Update self w/ other but never replace self w/ other.
Parameters: | other – object of which type is same as self’s. |
---|
>>> md0 = md1 = MergeableDict.create(dict(a=1, b=[1, 3], c="abc"))
>>> md2 = MergeableDict.create(dict(a=2, b=[0, 1], c="xyz", d=None))
>>> md0.update_wo_replace(md2)
>>> all(md0[k] != md2[k] for k in ("a", "b", "c"))
True
>>> all(md0[k] == md1[k] for k in ("a", "b", "c"))
True
>>> md0["d"] == md2["d"]
True
Merge members recursively. Behavior of merge will be vary depends on types of original and new values.
Parameters: |
|
---|
>>> md0 = md1 = MergeableDict.create(dict(a=1, b=dict(c=2, d=3),
... e=[1, 2, 2]))
>>> md2 = MergeableDict.create(dict(a=2, b=dict(d=4, f=5),
... e=[2, 3, 4]))
>>> md0.update_w_merge(md2, False)
>>> md0["a"] == md2["a"]
True
>>> md0["b"]["d"] == md2["b"]["d"] and md0["b"]["f"] == md2["b"]["f"]
True
>>> md0["e"] == md2["e"]
True
>>> md3 = MergeableDict.create(dict(aaa=[1, 2, 3], ))
>>> md4 = MergeableDict.create(dict(aaa=[4, 4, 5], ))
>>> md3.update_w_merge(md4, True)
>>> md3["aaa"] == [1, 2, 3, 4, 4, 5]
True