News for hackers. Stuff that matters.
Re:DotDict: A simple Python library to make chained attribut... by kshay on Wednesday August 02, 2023 @05:38PM (#36977972)
So basically Perl autovivification for Python?
Re:DotDict: A simple Python library to make chained attribut... by sekia on Friday August 04, 2023 @02:11AM (#36996056)
Seems a subset of it. Practicality aside, Perl can autovivify not only hashes but also arrays:

  my %x;
  $x{foo}[1]{bar} = 42;  # %x is ( foo => [ undef, { bar => 42 } ] )
Re:DotDict: A simple Python library to make chained attribut... by dancenow on Wednesday August 02, 2023 @05:34PM (#36977909)
This is the way. And that pip command is just clean.
Re:DotDict: A simple Python library to make chained attribut... by Toxygene on Wednesday August 02, 2023 @05:46PM (#36978080)
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.
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 astrea on Wednesday August 02, 2023 @06:30PM (#36978672)
As per this comment, I added it to pypi. You can now just do ‘pip install attr-dot-dict’.
Re:DotDict: A simple Python library to make chained attribut... by i13e on Wednesday August 02, 2023 @05:20PM (#36977670)
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 astrea on Wednesday August 02, 2023 @05:44PM (#36978061)
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.
Re:DotDict: A simple Python library to make chained attribut... by Waterluvian on Wednesday August 02, 2023 @06:20PM (#36978547)
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 attribut... by richbell on Wednesday August 02, 2023 @06:28PM (#36978645)
Re:DotDict: A simple Python library to make chained attribut... by Waterluvian on Wednesday August 02, 2023 @06:31PM (#36978689)
This is horrifying and yet I can’t look away.
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 bee_rider on Wednesday August 02, 2023 @07:10PM (#36979139)
That is a work of art, and also you have to appreciate the author’s commitment to the bit.
Re:DotDict: A simple Python library to make chained attribut... by richbell on Wednesday August 02, 2023 @08:17PM (#36979699)
I feel bad that no one seemed to get the joke. At the same time, that makes the whole thing funnier.
Re:DotDict: A simple Python library to make chained attribut... by nicwolff on Wednesday August 02, 2023 @06:14PM (#36978486)
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 attribut... by astrea on Wednesday August 02, 2023 @06:31PM (#36978695)
That contains doesn’t permit the behavior, though. It would give you an AttributeError if you attempted this.

Yes, addict seems to be exactly what I was going for.

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.
Re:DotDict: A simple Python library to make chained attribut... by sidmitra on Wednesday August 02, 2023 @06:12PM (#36978464)
In case anyone is looking for mature alternatives that are used in production

https://pypi.org/project/python-box/

https://pypi.org/project/munch/

Re:DotDict: A simple Python library to make chained attribut... by Halvedat on Wednesday August 02, 2023 @06:45PM (#36978850)
Neither of these appear to have the chained attribute feature, from my surface level parse.
Re:DotDict: A simple Python library to make chained attribut... by jperras on Wednesday August 02, 2023 @07:38PM (#36979405)
python-box does this in the most intuitive way possible:

    >>> from box import Box
    >>> empty_box = Box(default_box=True)
    >>> empty_box.a.b.c.d.e.f.g # <Box: {}>
Re:DotDict: A simple Python library to make chained attribut... by sidmitra on Wednesday August 02, 2023 @07:19PM (#36979241)
It's there on the readme/pypi home for both; unless i'm misunderstanding what you mean by "chained"

    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
Re:DotDict: A simple Python library to make chained attribut... by Izkata on Wednesday August 02, 2023 @08:22PM (#36979742)
Chained means in your second example this would work without manually creating a new instance:

    b.foo.lol = True
Re:DotDict: A simple Python library to make chained attribut... by nmalaguti on Wednesday August 02, 2023 @06:48PM (#36978907)
Re:DotDict: A simple Python library to make chained attribut... by make3 on Wednesday August 02, 2023 @07:29PM (#36979332)
Munch is an unfortunate name
Re:DotDict: A simple Python library to make chained attribut... by KolenCh on Thursday August 03, 2023 @11:58AM (#36987663)
How does these compare to glom?

https://glom.readthedocs.io

Re:DotDict: A simple Python library to make chained attribut... by nathants on Wednesday August 02, 2023 @06:26PM (#36978616)
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 attribut... by bbstats on Wednesday August 02, 2023 @06:29PM (#36978648)
I...do not prefer this syntax.
Re:DotDict: A simple Python library to make chained attribut... by hk1337 on Wednesday August 02, 2023 @06:35PM (#36978744)
edict() is an interesting name for a dictionary function.
Re:DotDict: A simple Python library to make chained attribut... by astrea on Wednesday August 02, 2023 @06:43PM (#36978839)
It’s a class instantiation with no arguments
Re:DotDict: A simple Python library to make chained attribut... by hk1337 on Wednesday August 02, 2023 @07:20PM (#36979250)
Yeah, I was referring to the name choice. Edict being “A decree or proclamation issued by an authority and having the force of law.”
Re:DotDict: A simple Python library to make chained attribut... by nmalaguti on Wednesday August 02, 2023 @06:47PM (#36978891)
This seems similar to the functionality exposed by Box.

https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#defaul...

Re:DotDict: A simple Python library to make chained attribut... by claytonjy on Wednesday August 02, 2023 @07:05PM (#36979088)
My general rule is, if I have a dict with a fixed set of keys, it should be a dataclass. Problem solved!

Then if I need to send or receive it, it's an easy conversion to a Pydantic BaseModel.

Re:DotDict: A simple Python library to make chained attribut... by smashed on Wednesday August 02, 2023 @07:29PM (#36979333)
And the dataclass serves as documentation for the available attributes and type safety..

It's really the way to go, takes a minute or two to define and saves so much headaches in the long run.

Re:DotDict: A simple Python library to make chained attribut... by behnamoh on Wednesday August 02, 2023 @07:30PM (#36979337)
Can dataclasses be serialized just as easily?
Re:DotDict: A simple Python library to make chained attribut... by paulddraper on Wednesday August 02, 2023 @07:32PM (#36979348)
Yes via pickle.

Or for JSON, dataclasses_json

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 nurettin on Sunday August 06, 2023 @12:52AM (#37019081)
I use pickle to serialize RPC requests between internal services. Nothing beats it in terms of speed and ease of use.
Re:DotDict: A simple Python library to make chained attribut... by jammycrisp on Thursday August 03, 2023 @12:12AM (#36981274)
Yep! Both msgspec (https://jcristharif.com/msgspec/supported-types.html#datacla...) and orjson support encoding dataclasses to JSON natively.
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.
Re:DotDict: A simple Python library to make chained attribut... by stavros on Wednesday August 02, 2023 @07:08PM (#36979112)
I also created an alternative to this, a while back:

https://pypi.org/project/jsane/

Its syntax is a bit uglier, though.

Re:DotDict: A simple Python library to make chained attribut... by bee_rider on Wednesday August 02, 2023 @07:15PM (#36979189)
If I do

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?

Re:DotDict: A simple Python library to make chained attribut... by fatso784 on Wednesday August 02, 2023 @07:34PM (#36979362)
This is a pretty cool idea. One comment —the print representation should be entirely akin to a dict, just braces. No DotDict wrapper. That’d be cool.
Re:DotDict: A simple Python library to make chained attribut... by stuaxo on Wednesday August 02, 2023 @09:08PM (#36980064)
Oh, I thought this was going to be like ChainMap, but for attributes, I'll stick with the one ChatGPT started for me.
Re:DotDict: A simple Python library to make chained attribut... by barelysapient on Wednesday August 02, 2023 @10:46PM (#36980782)
Why not use Pydantic or Dataclasses instead?

At least with Pydantic, there's the discipline to define the model and validation that a model instance is probably correct.

Re:DotDict: A simple Python library to make chained attribut... by tniemi on Thursday August 03, 2023 @07:34AM (#36984321)
Here's my personal finger exercise: https://pypi.org/project/talamus-relatable/

It allows references to another data structures.