Coder Social home page Coder Social logo

Read Only version about addict HOT 5 CLOSED

mewwts avatar mewwts commented on June 13, 2024
Read Only version

from addict.

Comments (5)

Ohjeah avatar Ohjeah commented on June 13, 2024

Had the same issue.
This works for me:

https://gist.github.com/Ohjeah/41b103e4bc15452af5bbc89747962369

from addict.

mewwts avatar mewwts commented on June 13, 2024

Hey!

Unfortunately, it's (afaik) not possible to get addicts functionality without creating an empty dictionary on lookup. Essentially, when you do my_dict.a.b = 2, Python first tries to figure out what my_dict.a.b is. If my_dict.a does not exist when __getattr__ is called on my_dict, Python will raise an exception. Hence, as a workaround, and tbh this is the whole idea that makes addict work, we return a empty dictionary, so that when the __setattr__ is called on my_dict.a we're able to do set the item in my_dict.

If I'm not misunderstanding the question you could always call .to_dict() in order to transform it into a regular python dict, and disallow further __setattr__.

Cheers,

Mats

from addict.

Ohjeah avatar Ohjeah commented on June 13, 2024

If you just use .to_dict() you will lose the dot-access to dict items too!

from addict.

mewwts avatar mewwts commented on June 13, 2024

That's true @Ohjeah.

from addict.

Ohjeah avatar Ohjeah commented on June 13, 2024

Hence the LockedDict (see the gist). It serves as a read only version of addict.Dict. Works well for me if I want an exception rather a None value return on a failed item lookup.

from addict.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.