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

Create converter for nested dictionaries / objects #9

Open
PFython opened this issue Dec 2, 2020 · 2 comments
Open

Create converter for nested dictionaries / objects #9

PFython opened this issue Dec 2, 2020 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed wontfix This will not be worked on

Comments

@PFython
Copy link
Owner

PFython commented Dec 2, 2020

Create a new utility method (or enhance init?) to convert nested dictionaries into nested CleverDicts, so that deeper levels can be accesses easily with "." notation. For example:

>>> d = CleverDict({"level1": {"level2A": "A", "level2B": "B"}})
>>> d.level1.level2B
'B'
>>> d
CleverDict({'level1': CleverDict({'level2A': 'A', 'level2B': 'B'}, _aliases={}, _vars={})}, _aliases={}, _vars={})

I'd also like to extend this functionality to attributes, which might be trickier. For example:

>>> ac1 = AnotherClass()
>>> ac2 = AnotherClass()
>>> ac1.level2 = "Level Two"
>>> ac2.level1 = "Level One"
>>> d = CleverDict(ac1, ac2)
>>> d.ac1.level2
"Level Two"
>>> d.ac2.level1
"Level One"

And the absolute ideal would be to handle any mixture of dictionaries and objects/attributes. For example:

>>> d = CleverDict({"First Item":  {"level1": {"level2A": "A", "level2B": "B"}}},
                     ac1, {"Second Item": {"obj": ac2}}}
>>> d.First_Item.level1.level2A
"A"
>>> d.ac1.level2
"Level Two"
>>> d.Second_Item.obj.level1
"Level One"
@PFython PFython added the enhancement New feature or request label Dec 2, 2020
@PFython PFython added the help wanted Extra attention is needed label Jan 20, 2021
@PFython PFython added the wontfix This will not be worked on label Jan 30, 2021
@PFython
Copy link
Owner Author

PFython commented Jan 30, 2021

Labelled as "wontfix" after Version 1.8.0 simply because the main authors don't currently have the time for non essential enhancements. If there's a huge uptake in people using CleverDict and asking for this feature that may change, or if you're reading this why not Fork, develop a solution, and contribute a Pull Request yourself?

@PFython
Copy link
Owner Author

PFython commented May 26, 2022

Revisiting this after some time and while writing tests for my new Whatsapp class... I think the solution may be to set an option and if True, every time a new value is set, check to see if its value (or recursively, any of the values contained therein) is a Dictionary; if it is a dictionary, convert it to a cleverdict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

1 participant