Live data from Hacker News

DotDict: A simple Python library to make chained attributes possible

github.com

11–20 of 55 posts

Re: DotDict: A simple Python library to make chained attributes possible

#11

Does it lazy-create leaf dicts on access? It would be preferable if they were only created on assignment, and failed if it’s just a get. For example, if I call config.usors.adminn.naem I’d like it to fail loud and fast to point out the typos.

Yeah it does. If you do:

    x = dd()
    print(x.tyypo.a.b)
you'll get "DotDict()"

The problem is that an object can't know whether you're getting a node that you're later going to assign a leaf to... or whether you're getting the node because you want to get the node.

   x = dd()
   x.a.b.c = "Foo"
is equivalent to:

   x = dd()
   y = x.a.b
   y.c = "Foo"
So "x.a.b" can't raise an exception.

Now, it's python, so you can do unholy things to make pretty much anything work. But I think it would be ugly.

Re: DotDict: A simple Python library to make chained attributes possible

#13
post #5

This is more of a political statement than a proper library. More to demonstrate the flexibility of Python given I was able to so easily do this.

Could have been even easier: `argparse.Namespace` already has `__contains__()` ツ

That said, https://pypi.org/project/addict/ has been around a long time...

Re: DotDict: A simple Python library to make chained attributes possible

#14

Does it lazy-create leaf dicts on access? It would be preferable if they were only created on assignment, and failed if it’s just a get. For example, if I call config.usors.adminn.naem I’d like it to fail loud and fast to point out the typos.

Yeah it does. If you do: x = dd() print(x.tyypo.a.b) you'll get "DotDict()" The problem is that an object can't know whether you're getting a node that you're later going to assign a leaf to... or whether you're getting the node because you want to get the node. x = dd() x.a.b.c = "Foo" is equivalent to: x = dd() y = x.a.b y.c = "Foo" So "x.a.b" can't raise an exception. Now, it's python, so you can do unholy things…

They could add a __bool__ method to the class the returns false when that level of the namespace is empty. That would allow:

   if a.b.c:
       print("c:", a.b.c)

Re: DotDict: A simple Python library to make chained attributes possible

#15
post #5

This is more of a political statement than a proper library. More to demonstrate the flexibility of Python given I was able to so easily do this.

I enjoy how much complete control Python enables. It’s fun to turn it into what resembles a different language.

I want to make a library where you never ever call functions with fn() notation. But just utilize accessors and absolutely bastardize dicts.

Re: DotDict: A simple Python library to make chained attributes possible

#16

Earlier quoted context omitted.

Yeah it does. If you do: x = dd() print(x.tyypo.a.b) you'll get "DotDict()" The problem is that an object can't know whether you're getting a node that you're later going to assign a leaf to... or whether you're getting the node because you want to get the node. x = dd() x.a.b.c = "Foo" is equivalent to: x = dd() y = x.a.b y.c = "Foo" So "x.a.b" can't raise an exception. Now, it's python, so you can do unholy things…

They could add a __bool__ method to the class the returns false when that level of the namespace is empty. That would allow: if a.b.c: print("c:", a.b.c)

The __contains__ method should be used (and indeed is). What if a.b.c = False?

Re: DotDict: A simple Python library to make chained attributes possible

#17
this is cool!

i use a lot of defaultdict trees[1], and convert nested dicts to/from flat dicts with dots in their keys[2].

never underestimate the mighty dict.

1. https://github.com/nathants/py-util/blob/6844917fb51e9d24de1...

2. https://github.com/nathants/py-util/blob/6844917fb51e9d24de1...

Re: DotDict: A simple Python library to make chained attributes possible

#18
post #5

This is more of a political statement than a proper library. More to demonstrate the flexibility of Python given I was able to so easily do this.

I enjoy how much complete control Python enables. It’s fun to turn it into what resembles a different language. I want to make a library where you never ever call functions with fn() notation. But just utilize accessors and absolutely bastardize dicts.

My favorite example of this: https://old.reddit.com/r/learnprogramming/comments/kegalv/my...

Re: DotDict: A simple Python library to make chained attributes possible

#20
post #8
post #6

Earlier quoted context omitted.

Have I lost my marbles? It doesn't work? (temp5) ~/temp5 pip install . ERROR: Directory '.' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.

Run it from a clone of the repo to install it to the system / your user. I don't see a name to install this from pypi (I see several dotdict libraries on there but they don't link here, so don't know if this is there somewhere).

As per this comment, I added it to pypi. You can now just do ‘pip install attr-dot-dict’.
Post reply on HN