Skip to content

support chaining when setting a nested value with __setitem__ #6

@rnag

Description

@rnag

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 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions