Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to initialize with a flattened dict? #170

Closed
dragonpaw opened this issue Oct 17, 2020 · 4 comments
Closed

Be able to initialize with a flattened dict? #170

dragonpaw opened this issue Oct 17, 2020 · 4 comments

Comments

@dragonpaw
Copy link

dragonpaw commented Oct 17, 2020

I'm in a situation where I need to deal with some deeply nested structures, but I want to initialize them with dictionaries of dotted keys. So I'd ideally like to be able to do something like:

d = Box({'service.name': 'django', 'service.version': 2.2})
print(d.service.name) 
# Prints 'django'
print(d.service.keys())
# prints "dict_keys(['name', 'version'])"
print(d.to_dict())
# prints {'service': {'name': 'django', 'version': 2.2}}

Now, I can do this if I initialize the Box, then write to it as d.service.name = 'django', but it'd be nice to be able to unflatten the incoming dict somehow.

@dragonpaw dragonpaw changed the title Be able to initalize with a flattened dict? Be able to initialize with a flattened dict? Oct 17, 2020
@trallnag
Copy link

Here is the algorithm I used to unflatten a dict https://stackoverflow.com/a/55545369/7391331

@cdgriffith
Copy link
Owner

If this is added, I think it would be good to also have a method to pack in into a flattened dict.

@cdgriffith
Copy link
Owner

Adding this feature in Box 7! Please test and give feedback if possible pip install python-box[all]~=7.0.0rc0

Specifically will require combo of default_box and box_dots, or which is same as using new DDBox class

cdgriffith added a commit that referenced this issue Feb 4, 2023
* Adding #169 default functions with the box_instance and key parameter (thanks to Коптев Роман Викторович)
* Adding #170 Be able to initialize with a flattened dict - by using DDBox (thanks to Ash A.)
* Adding #192 box_dots treats all keys with periods in them as separate keys (thanks to Rexbard)
* Adding #211 support for properties and setters in subclasses (thanks to Serge Lu and David Aronchick)
* Adding #226 namespace to track changes to the box (thanks to Jacob Hayes)
* Adding #236 iPython detection to prevent adding attribute lookup words (thanks to Nishikant Parmar)
* Adding #238 allow ``|`` and ``+`` for frozen boxes (thanks to Peter B)
* Adding new DDBox class (Default Dots Box) that is a subclass of SBox
* Fixing #235 how ``|`` and ``+`` updates were performed for right operations (thanks to aviveh21)
* Fixing #234 typos (thanks to Martin Schorfmann)
* Fixing no implicit optionals with type hinting
@cdgriffith
Copy link
Owner

Added in 7.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants