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

Access sub dictionary keys with box.get('key.subkey.subsubkey') #161

Closed
scott-createplay opened this issue Aug 1, 2020 · 4 comments
Closed

Comments

@scott-createplay
Copy link

scott-createplay commented Aug 1, 2020

Love the library. I was looking into subclassing box.Box to get this working, but wanted to make sure it wasn't already present in the library.

Imagine a situation like this:

my_data = box.Box({ 'level_1': {level_2:{level_3: my_value}}})

Currently to get to the level_3, I would need to do:

eval('my_data.level_1.leve_2.level_3)

Ideally, I would want to do:

my_val = my_data.get('level_1.level_2.level_3')

Of course I could just do:

my_val = my_data.level_1.level_2.level_3

However, I am encoding my dot access in config files as strings, so this would cause a syntax error as 'level_1' is not a variable.

is there a way to accomplish this with the current implementation of python-box?

Thanks in advance

@cdgriffith
Copy link
Owner

Actually there is almost exactly what you are looking for already, if you enable box_dots=True you can access variables via dot notation as dict keys.

my_data = Box({ 'level_1': {'level_2': {'level_3': 'my_value'}}}, box_dots=True)

my_val = my_data['level_1.level_2.level_3']

print(my_val)
# my_value

However, you did just point out something I forgot to add was the ability to do that via the get method as well, so I am keeping this open to track that effort.

@scott-createplay
Copy link
Author

Hey Chris,

Thanks for the heads up. This is great. One other aspect, that is related is the "in" check. IE-

myBox = box.Box({"this": {"is": {"my": "box"}}})

I would want to be able to check for the key's existence in the same way:

"this.is.my" in myBox
Currently returns False, but it would be nice to be able to check for nested keys.

Any thoughts / recommendations are welcomed!
Thanks
-s

cdgriffith added a commit that referenced this issue Jan 12, 2022
@cdgriffith
Copy link
Owner

cdgriffith commented Jan 21, 2022

Adding these features in 6.0, currently have a release candidate that can be installed and tested with:

pip install python-box[all]==6.0.0rc2 --upgrade 

6.0 is ushering in Cython speedups on supported platforms, so please let me know if you run into any issues!

cdgriffith added a commit that referenced this issue Mar 15, 2022
* Adding Cython support to greatly speed up normal Box operations on supported systems
* Adding #161 support for access box dots with `get` and checking with `in` (thanks to scott-createplay)
* Adding #183 support for all allowed character sets (thanks to Giulio Malventi)
* Adding #196 support for sliceable boxes (thanks to Dias)
* Adding #164 default_box_create_on_get toggle to disable setting box variable on get request (thanks to ipcoder)
* Changing #208 __repr__ to produce `eval`-able text (thanks to Jeff Robbins)
* Changing #215 support ruamel.yaml new syntax (thanks to Ivan Pepelnjak)
* Changing `update` and `merge_update` to not use a keyword that could cause issues in rare circumstances
* Changing internal `_safe_key` logic to be twice as fast
* Removing support for ruamel.yaml < 0.17
@cdgriffith
Copy link
Owner

Box 6 has been released with this added, thanks for opening the issue!

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

2 participants