Re:DotDict: A simple Python library to make chained attribut...
by Izkata on Wednesday August 02, 2023 @05:53PM
(#36978185)
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).
Re:DotDict: A simple Python library to make chained attribut...
by b33j0r on Wednesday August 02, 2023 @10:16PM
(#36980556)
Anthony’s video 100%.
For anyone who doesn’t watch… the point being made is that this has been done so many times that yet another implementation was pulled from the stdlib in 3.12 (AttrDict).
It’s true. I’ve implemented this several times myself, and thought it was a new/good idea every time until I remembered how many abstractions it breaks if I use it anywhere.
I can’t promise I won’t do it again in a few weeks! `NaturalDictLite` is probably coming around in the accompanying rotation of bad name ideas :p
Re:DotDict: A simple Python library to make chained attribut...
by necovek on Wednesday August 02, 2023 @07:02PM
(#36979046)
Wow, great job by this "beginner" just "learning a language"! Demonstrates knowledge of so many of Python intricacies that it's worth starting a Non-obfuscated Obfuscated Python Code Contest :)
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 dharmab on Wednesday August 02, 2023 @07:42PM
(#36979438)
Be aware that pickle is unsafe (as documented near the top of https://docs.python.org/3/library/pickle.html), so prefer other serialization formats except between trusted processes
Re:DotDict: A simple Python library to make chained attribut...
by claytonjy on Wednesday August 02, 2023 @07:59PM
(#36979553)
I can't remember the last time I _wanted_ to pickle something. Parquet and JSON pretty much cover it for me. I guess pickling made more sense before tools like Pydantic were good and popular?
If I want all the python-specific nuance on disk, I know I'm in a bad place!
Re:DotDict: A simple Python library to make chained attribut...
by jwestbury on Thursday August 03, 2023 @05:00AM
(#36983210)
In addition to the already-mentioned suggestions, you can also use marshmallow schemas to serialise complex objects, and there's even a package to autogen marshmallow schemas from dataclasses: https://pypi.org/project/marshmallow-dataclass/
Re:DotDict: A simple Python library to make chained attribut...
by Fatal1ty on Thursday August 03, 2023 @06:15PM
(#36992785)
Yes, there are many tools for converting dataclasses to dict, json or something else, mashumaro (https://github.com/Fatal1ty/mashumaro) is one of them.
Re:DotDict: A simple Python library to make chained attribut...
by rivepica on Thursday August 03, 2023 @01:15AM
(#36981669)
100%. I don’t understand the obsession with bending dicts into something they are inherently bad at when actual alternatives in dataclasses and Pydantic models right there.
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).
For anyone who doesn’t watch… the point being made is that this has been done so many times that yet another implementation was pulled from the stdlib in 3.12 (AttrDict).
It’s true. I’ve implemented this several times myself, and thought it was a new/good idea every time until I remembered how many abstractions it breaks if I use it anywhere.
I can’t promise I won’t do it again in a few weeks! `NaturalDictLite` is probably coming around in the accompanying rotation of bad name ideas :p
I want to make a library where you never ever call functions with fn() notation. But just utilize accessors and absolutely bastardize dicts.
That said, https://pypi.org/project/addict/ has been around a long time...
Yes, addict seems to be exactly what I was going for.
For example, if I call config.usors.adminn.naem I’d like it to fail loud and fast to point out the typos.
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.
is equivalent to: 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.
https://pypi.org/project/python-box/
https://pypi.org/project/munch/
https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#defaul...
https://glom.readthedocs.io
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...
https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#defaul...
Then if I need to send or receive it, it's an easy conversion to a Pydantic BaseModel.
It's really the way to go, takes a minute or two to define and saves so much headaches in the long run.
Or for JSON, dataclasses_json
If I want all the python-specific nuance on disk, I know I'm in a bad place!
https://pypi.org/project/jsane/
Its syntax is a bit uglier, though.
config = dd()
config.users.foo = 'bar' config.users = ‘baz’
Is config.users turned into a string and config.users.foo is lost? Or is the equal symbol mapped to something cleverer than assignment?
At least with Pydantic, there's the discipline to define the model and validation that a model instance is probably correct.
It allows references to another data structures.