[HN Gopher] DotDict: A simple Python library to make chained att...
       ___________________________________________________________________
        
       DotDict: A simple Python library to make chained attributes
       possible
        
       Author : astrea
       Score  : 28 points
       Date   : 2023-08-02 21:16 UTC (1 hours ago)
        
 (HTM) web link (github.com)
 (TXT) w3m dump (github.com)
        
       | i13e wrote:
       | Good video on this subject:
       | https://www.youtube.com/watch?v=nDd2WVb_Enw
        
       | sidmitra wrote:
       | In case anyone is looking for mature alternatives that are used
       | in production
       | 
       | https://pypi.org/project/python-box/
       | 
       | https://pypi.org/project/munch/
        
         | Halvedat wrote:
         | Neither of these appear to have the chained attribute feature,
         | from my surface level parse.
        
           | nmalaguti wrote:
           | Box can do it with
           | 
           | https://github.com/cdgriffith/Box/wiki/Types-of-
           | Boxes#defaul...
        
       | kshay wrote:
       | So basically Perl autovivification for Python?
        
       | hk1337 wrote:
       | edict() is an interesting name for a dictionary function.
        
         | astrea wrote:
         | It's a class instantiation with no arguments
        
       | astrea wrote:
       | 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.
        
         | nicwolff wrote:
         | Could have been even easier: `argparse.Namespace` already has
         | `__contains__()` tsu
         | 
         | That said, https://pypi.org/project/addict/ has been around a
         | long time...
        
           | astrea wrote:
           | 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.
        
         | Waterluvian wrote:
         | 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.
        
           | richbell wrote:
           | My favorite example of this: https://old.reddit.com/r/learnpr
           | ogramming/comments/kegalv/my...
        
             | Waterluvian wrote:
             | This is horrifying and yet I can't look away.
        
       | nathants wrote:
       | 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...
        
       | gorgoiler wrote:
       | 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.
        
         | Izkata wrote:
         | Code looks like it's on access, but I didn't test it.
        
         | kzrdude wrote:
         | I don't think it's possible in Python to detect the difference
         | between access or write (when it's nested attributes).
        
         | heisenzombie wrote:
         | 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.
        
           | samwillis wrote:
           | 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)
        
             | rozab wrote:
             | The __contains__ method should be used (and indeed is).
             | What if a.b.c = False?
        
       | nmalaguti wrote:
       | This seems similar to the functionality exposed by Box.
       | 
       | https://github.com/cdgriffith/Box/wiki/Types-of-Boxes#defaul...
        
       | bbstats wrote:
       | I...do not prefer this syntax.
        
       | dancenow wrote:
       | This is the way. And that pip command is just clean.
        
         | Toxygene wrote:
         | 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.
        
           | Izkata wrote:
           | 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).
        
             | astrea wrote:
             | As per this comment, I added it to pypi. You can now just
             | do 'pip install attr-dot-dict'.
        
       ___________________________________________________________________
       (page generated 2023-08-02 23:00 UTC)