Box
Box copied to clipboard
default_box does not work with box_dots
This behaviour is noted in the Wiki, but I don't agree it is as it should be.
Using default_box, one can create and modify a new dotted chain as expected:
>>> b=Box(default_box=True)
>>> b.a.b.c = 1
>>> print(b)
<Box: {'a': {'b': {'c': 1}}}>
>>> b["a.b.c"] = 2
>>> print(b)
<Box: {'a': {'b': {'c': 2}}}>
However, when trying to create a new value using box_dots, it does not work as I expect (again the demonstrated behavior is noted in the Wiki).
>>> b=Box(default_box=True, box_dots=True)
>>> b["a.b.c"] = 1
>>> print(b)
<Box: {'a.b.c': 1}>
By declaring box_dots=True the programmer has explicitly stated they want to interpret the periods as field separators. It seems that the above code should return the same as the first code block.
Since I require this behavior, I have to split the index string and create empty boxes, then perform the b["a.b.c"]=1
for it to work as intended.
To be honest it would probably be a lot easier programmatically to say that everything with dots is always changed for box dots.
Such a breaking change would require a major version bump (which I am not against), but also not something I have the cycles to work on myself.
Is this a related problem?
>>> b = Box(box_dots=True)
>>> b['x'] = {'y':10}
<Box: {'x': {'y': 10}}>
>>> b.setdefault('x.y', 20)
<Box: {'x': {'y': 20}}>
That is setdefault
overwrites already defined value.
Sorry I missed this earlier @ipcoder, that is actually an unrelated issue, addressing in 5.4.0
Basically the issue is that I was doing a check of key in self
, but in
uses __iter__
so it doesn't see the sub box dots keys, and doesn't think it exists, so it overwrites it.
Adding this feature in Box 7! Please test and give feedback if possible pip install python-box[all]~=7.0.0rc0
box_dots now means all periods should be split!
Added in 7.0.0