News for hackers. Stuff that matters.
Re:DotDict: A simple Python library to make chained attribut... by gorgoiler on Wednesday August 02, 2023 @05:48PM (#36978115)
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.

Re:DotDict: A simple Python library to make chained attribut... by heisenzombie on Wednesday August 02, 2023 @06:09PM (#36978431)
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 attribut... by samwillis on Wednesday August 02, 2023 @06:20PM (#36978546)
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 attribut... by rozab on Wednesday August 02, 2023 @06:25PM (#36978605)
The __contains__ method should be used (and indeed is). What if a.b.c = False?
Re:DotDict: A simple Python library to make chained attribut... by kzrdude on Wednesday August 02, 2023 @06:04PM (#36978335)
I don't think it's possible in Python to detect the difference between access or write (when it's nested attributes).
Re:DotDict: A simple Python library to make chained attribut... by Izkata on Wednesday August 02, 2023 @05:57PM (#36978238)
Code looks like it's on access, but I didn't test it.