Dictlib is a lightweight add-on for dictionaries, featuring:
- Dictionary union done properly:
union()
(not immutably safe), union_copy()
(immutably safe) "String.dot"
notation for retrieval from classic dictionaries, with a string key: dig()
, dig_get()
, dug()
. For efficiencies sake, it isn't an object. If you want dot notation more commonly used in your code, use Dict()
instead.Object.key
Dictionary keys as object attributes (easy classes): Dict()
(useful for rapid prototyping,
just define your class as a Dict, either way:- balancing features with performance: we could do more (such as supporting dictionary['this.key'] inline dot notation), but I wanted to
keep it near native performance, and having an external function like
dig()
is similar to Ruby's method, so you can use it as needed,
and if you really want dot notation, use an inline method that is efficient at runtime like Dict()
NewClass = Dict
class NewClass(Dict):
pass
If this doesn't work for you, consider other dictionary helper libraries:
- Scalpl
- a more indepth tool that does similar to
dictlib.dig()
and dictlib.dug()
- does not include keys as object attributes --
Dict()
- Addict
- similar to
addict.Dict()
and dictlib.Dict()
- As time allows I'll add a better comparison
- Box
- similar to
addict.Dict()
and dictlib.Dict()
- As time allows I'll add a better comparison
union() and union_copy()
from dictlib import union, union_copy
dict1 = union(dict1, dict2)
dict3 = union_copy(dict1, dict2)
Deep union of dict2 into dict1, where dictionary values are recursively merged.
Non-dictionary elements are replaced, with preference given to dict2.
This alters dict1, which is the returned result, but it will have references to
both dictionaries. If you do not want this, use union_copy(), which is less
efficient but data-safe.
dig() and dig_get()
Recursively pull from a dictionary, using dot notation. dig_get behaves like dict.get()
, but with dot-notated keys.
from dictlib import dig, dig_get
dict1 = {"a":{"b":{"c":1},"d":[{"e":1},{"f":2}]}}
dig(dict1, "a.b.c")
dig(dict1, "a.d[1].f")
dig(dict1, "a.b.z")
dig_get(dict1, "a.b.z")
dig_get(dict1, "a.b.z", 2)
dug()
Inverse of dig()
, dug()
puts an item into a nested dictionary, using dot notation.
This does not behave immutably, as it alters the origin dictionary.
from dictlib import dug
dict1 = {"a":{"b":{"c":1}}}
dug(dict1, "a.b.c", 200)
dug(dict1, "a.b.z.e", True)
Note: dug() does not support pushing to lists within a dictionary, it assumes
all values are dictionaries in your dot notation string. If you attempt to use
a list index, it still behaves as if it were a dictionary, which may give you
unexpected results:
dict1 = {"a":{"b":{"c":1}}}
dug(dict1, "a.b.d[0].e", True)
(PR's to finish this feature correctly are appreciated)
Dict()
A bit of sugar to represent a dictionary in object form where keys are set as
attributes on the object. Features:
- it tokenizes your keys if they are not python safe (
"this-key"
is .this_key
). Example:
d = Dict({"this key": "value"})
d["this-key"]
d.this_key
- Recursive -- it will walk the full depth of the dictionary
This is not python zen because it provides an alternate way to use dictionaries,
and it has some challenges with names that collide with builtin methods, but it
is very
But I'm okay with this, because it is handy bit of sugar.
Limitations:
- raises error if there is a name conflict with reserved words
- reserves the key prefix \f$\f for internal use (raises error)
- because of namespace conflict problems, you must be cautious on what keys are input
- Two keys exist for each non-tokenized name, such as
ugly var!
,
which is tokenized to ugly_var_
. However, they do not point to the same
data value! While both exist, if exporting to original object only the
value of the tokenized name is used (see examples)
from dictlib import Dict
Dict(key1=1, a=2)
test_dict = {"a":{"b":1,"ugly var!":2}, "c":3}
test_obj = Dict(**test_dict)
test_obj.keys()
'a' in test_obj
test_obj.get('c')
test_obj['c']
test_obj.c
test_obj.c = 4
test_obj.c
test_obj.a.b
test_obj.a.ugly_var_
test_obj.a['ugly var!']
test_obj.a.ugly_var_ = 0xdeadbeef
test_obj.a.ugly_var_
test_obj.a['ugly var!']
test_obj = Dict(test_dict)
test_obj
import json
json.dumps(test_obj)
json.dumps(test_obj.__export__())
json.dumps(test_obj.__original__())
test_obj.__original__()
Note: Dict()
was previously Obj()
, which has been deprecated but is still supported.
dictlib.original() and dictlib.export()
Walk dict1
which may be mixed dict()/Dict() and export any Dict()'s to dict(),
using the Dict.__original__()
or Dict.__export__()
method, respectively.
(useful for data conversions, such as with dict->yaml)
import json
export(Dict({"ugly first": 1, "second": {"tres": Dict({"nachos":2})}}))
json.dumps(Dict({"ugly first": 1, "second": {"tres": Dict({"nachos":2})}}))
json.dumps(export(Dict({"ugly first": 1, "second": {"tres": Dict({"nachos":2})}})))