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

support chaining when setting a nested value with __setitem__ #6

Open
rnag opened this issue Jun 23, 2022 · 0 comments
Open

support chaining when setting a nested value with __setitem__ #6

rnag opened this issue Jun 23, 2022 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@rnag
Copy link
Owner

rnag commented Jun 23, 2022

Is your feature request related to a problem? Please describe.

I want to support chaining when setting a nested value in a non-existent path. It should create all intermediate paths as needed.

Describe the solution you'd like

I want to add support for a "chaining" approach with __setitem__ as is inspired by this question, so for example:

from dotwiz import DotWiz

obj = DotWiz()
obj['a.b.c.d'] = 1

should then work, and essentially perform the equivalent of:

obj = ✫(a=✫(b=✫(c=✫(d=1))))

And maybe it makes sense to add a fallback method, set, so that can be used for cases where we don't actually desire that behavior. So in the example above,

obj.set('a.b.c', 2)

should result in the following value of obj:

✫(a.b.c=2)

Describe alternatives you've considered

Actually, it may also be worth it to implement a __missing__ method, as outlined in this question as well. I felt like this approach was rather clever and worthwhile to look into.

Additional context

See also this post on SO for more details.

Essentially, supporting the equivalent of dw.a.b.c.d = 1 I think is not a good idea, because it involves implementing a __getattr__ apparently, which will definitely hurt performance to read/access values, since in the default implementation we don't override the builtin object.__getattr__ method definition at all.

So I want to at the very least, support this slightly different format:

dw['a.b.c.d'] = 1

I think this is a good idea, relatively speaking, because we'll only need to update the __setitem__ method, and not touch other methods like __getitem__ for example -- at least not at this point, anyway 🙂

@rnag rnag added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Jun 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant