GithubHelp home page GithubHelp logo

Create subkeys with dots as needed about box HOT 3 OPEN

csm10495 avatar csm10495 commented on June 14, 2024
Create subkeys with dots as needed

from box.

Comments (3)

cdgriffith avatar cdgriffith commented on June 14, 2024

Currently no, but I am not opposed to that as a feature.

from box.

csm10495 avatar csm10495 commented on June 14, 2024

Found a 'cheap' way to do this:


In [1]: from box import Box

In [2]: box_data = Box(box_dots=True, default_box=True)

In [3]: box_data.a.b.c = 1

In [4]: box_data.a.b.c
Out[4]: 1

In [5]: v = 1234

In [6]: exec('box_data.c.a.b = v', locals())

In [7]: box_data.c.a.b
Out[7]: 1234

The exec mechanism can be used if you have a key as a string too:

In [8]: key = 'z.y.x'

In [9]: exec(f'box_data.{key} = v', locals())

In [10]: box_data.z.y.x
Out[10]: 1234

Optimally a better way wouldn't require something like an exec call.

from box.

cdgriffith avatar cdgriffith commented on June 14, 2024

With Box 7 going to have it so you can combine default_box and box_dots as you described and have it work with dots in strings as well. So:

a = Box(box_dots=True, default_box=True)
a['b.c'] = 3 
# Box({'b': {'c': 3}})

from box.

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.