This library is a simple way to work with complex python dictionaries. The resources in your dictionary can be obtained through accesses indexed in the structure, we can then imagine these accesses as an operating system path. For example the resource /home/usr/user/Documents/file.txt
.
dict_ = {
"simple_key": "simple_value",
"array": [1, 2, 3, 4],
"dict": {
"foo": "bar",
"vect": ["a", 1],
"sub": {
"foo": "bar"
}
},
"complex": [
"value",
245,
{
"sub_vect": [{"foo": "bar"}]
}
],
"56": 34
}
For example, the access:
dict_["dict"]["sub"]["foo"]
# Returns the value "bar"
Think of this access as a path, we get /dict/sub/foo
. What about array indices? Is this a path? No, but we can use it anyway because arrays also support indexed access.
dict_["complex"][2]["sub_vect"][0]["foo"]
# Returns the value "bar"
We can translate this for complex/2/sub_vect/0/foo
"56": 34
) we need to use a especial character to identify the number with dictionary key and don't like a array index. We use in this case '
, for example /'56'
.
from rdict import Rdict as rd
rd_ = rd(dict_, sep="/", use_copy=True)
rd_.get("complex/2/sub_vect/0/foo")
# Returns the value "bar"
With default value, if the path don't exists the function returns default value.
rd_.get("this/path/do/not/exists", default="bar")
# Returns the default value "bar"
If you need an exception will be raised, use flag the raise_error
. The type of exception is the same one thrown if you try to access the indexes normally in the original dictionary.
rd_.get("this/path/do/not/exists", default="bar", raise_error=True)
# Raise "KeyError: 'this'"