Open csm10495 opened 3 years ago
Currently no, but I am not opposed to that as a feature.
Found a 'cheap' way to do this:
In [1]: from box import Box
In [2]: box_data = Box(box_dots=True, default_box=True)
In [3]: box_data.a.b.c = 1
In [4]: box_data.a.b.c
Out[4]: 1
In [5]: v = 1234
In [6]: exec('box_data.c.a.b = v', locals())
In [7]: box_data.c.a.b
Out[7]: 1234
The exec mechanism can be used if you have a key as a string too:
In [8]: key = 'z.y.x'
In [9]: exec(f'box_data.{key} = v', locals())
In [10]: box_data.z.y.x
Out[10]: 1234
Optimally a better way wouldn't require something like an exec call.
With Box 7 going to have it so you can combine default_box and box_dots as you described and have it work with dots in strings as well. So:
a = Box(box_dots=True, default_box=True)
a['b.c'] = 3
# Box({'b': {'c': 3}})
Hey,
I noticed this line in the docs:
So if i do:
Is there a way to make it so that it recursively makes boxes as needed to make it to the final key?