cdgriffith / Box

Python dictionaries with advanced dot notation access
https://github.com/cdgriffith/Box/wiki
MIT License
2.61k stars 106 forks source link

merge_update() not merging lists #181

Closed mdione-cloudian closed 3 years ago

mdione-cloudian commented 4 years ago

Does this make sense? I need it for myself, but maybe it's not box' responsibility.

13:10 $ ipython3 
Python 3.8.6 (default, Sep 25 2020, 09:36:53) 

In [1]: import box

In [5]: b1 = box.Box({'network-config': [{'use': 'frontend', 'prefix': 24}, {'use': 'backend'}]})

In [6]: b2 = box.Box({'network-config': [{'bogus_key': 'bogus_value'}]})

In [8]: b1.merge_update(b2)

In [9]: b1
Out[9]: <Box: {'network-config': [{'bogus_key': 'bogus_value'}]}>

In [10]: box.__version__
Out[10]: '5.2.0'

I was expecting:

In [9]: b1
Out[9]: <Box: {'network-config': [{'use': 'frontend', 'prefix': 24}, {'use': 'backend'}, {'bogus_key': 'bogus_value'}]}>

or maybe even:

In [9]: b1
Out[9]: <Box: {'network-config': [{'use': 'frontend', 'prefix': 24, 'bogus_key': 'bogus_value'}, {'use': 'backend'}]}>
cdgriffith commented 4 years ago

It was not designed that way, but I do think it would be good to include that as a possible option!

cdgriffith commented 3 years ago

Looking into to being able to give multiple options for how this happens:

        a = Box()
        a.merge_update({"lister": ["a"]})
        a.merge_update({"lister": ["a", "b", "c"]}, box_merge_lists="extend")
        assert a.lister == ["a", "a", "b", "c"]
        a.merge_update({"lister": ["a", "b", "c"]}, box_merge_lists="unique")
        assert a.lister == ["a", "a", "b", "c"]
        a.merge_update({"lister": ["a", "d", "b", "c"]}, box_merge_lists="unique")
        assert a.lister == ["a", "a", "b", "c", "d"]
        a.merge_update({"key1": {"new": 5}, "Key 2": {"add_key": 6}, "lister": ["a"]})
        assert a.lister == ["a"]
cdgriffith commented 3 years ago

Added in https://github.com/cdgriffith/Box/releases/tag/5.3.0