DotDict: A simple Python library to make chained attributes possible
github.com
github.com
https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#defaul...
from box import Box
movie_box = Box({ "Robin Hood: Men in Tights": { "imdb stars": 6.7, "length": 104 } })
movie_box.Robin_Hood_Men_in_Tights.imdb_stars
from munch import Munch
b = Munch()
b.hello = 'world'
b.foo = Munch(lol=True)
b.foo.lol b.foo.lol = True >>> from box import Box
>>> empty_box = Box(default_box=True)
>>> empty_box.a.b.c.d.e.f.g # <Box: {}>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!
For example, if I call config.usors.adminn.naem I’d like it to fail loud and fast to point out the typos.
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.
if a.b.c:
print("c:", a.b.c)That said, https://pypi.org/project/addict/ has been around a long time...
Yes, addict seems to be exactly what I was going for.
I want to make a library where you never ever call functions with fn() notation. But just utilize accessors and absolutely bastardize dicts.
my %x;
$x{foo}[1]{bar} = 42; # %x is ( foo => [ undef, { bar => 42 } ] )https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#defaul...
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
It allows references to another data structures.
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://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.
(temp5) ~/temp5 pip install .
ERROR: Directory '.' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.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).