Box icon indicating copy to clipboard operation
Box copied to clipboard

default_box does not work with box_dots

Open Rexbard opened this issue 3 years ago • 3 comments

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.

Rexbard avatar Mar 11 '21 23:03 Rexbard

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.

cdgriffith avatar Mar 31 '21 04:03 cdgriffith

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.

ipcoder avatar May 25 '21 21:05 ipcoder

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.

cdgriffith avatar Aug 14 '21 23:08 cdgriffith

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!

cdgriffith avatar Jan 28 '23 20:01 cdgriffith

Added in 7.0.0

cdgriffith avatar Feb 04 '23 04:02 cdgriffith