Skip to main content

Python dictionaries with recursive dot notation access

Project description

Python dictionaries with recursive dot notation access.

from box import Box

my_box = Box({"owner": "Mr. Powers",
              "contents": ["blue crushed-velvet suit",
                           "frilly lace crava",
                           "gold medallion with peace symbol",
                           "Italian shoes",
                           "tie-dyed socks"],
              "affiliates": {
                  "Vanessa": "Sexy",
                  "Dr Evil": "Not groovy",
                  "Scott Evil": "Doesn't want to take over family business"
              }})

my_box.affiliates.Vanessa == my_box['affiliates']['Vanessa']

my_box.funny_line = "They tried to steal my lucky charms!"

my_box['funny_line']
# 'They tried to steal my luck charms!'

my_box.credits = {'Austin Powers': "Mike Myers", "Vanessa Kensington": "Elizabeth Hurley"}
# <Box: {'Austin Powers': 'Mike Myers', 'Vanessa Kensington': 'Elizabeth Hurley'}>

Install

pip install python-box

(Don’t see a box package, but alas, can’t claim the name for some reason.)

Box is tested on python 2.6+, 3.3+ and PyPy2, and should work on other interpreters as well. If it does not install with this command, please open a ticket with the error you are experiencing!

Overview

This module provides two main classes Box and ConfigBox. They are designed to be easy drop in replacements for dictionaries, with the latter having tools for dealing with config files.

Box is designed to transparently act as a dictionary, thanks to Python’s duck typing capabilities, but add dot notation access like classes do. Any sub dictionaries or ones set after initiation will be automatically converted to a Box object. You can always run .to_dict() on it to return the object and all sub objects back into a regular dictionary.

# Will only convert outermost object
dict(my_box)
# {'owner': 'Mr. Powers', 'affiliates': <Box: {'Vanessa': 'Sexy',
# 'Dr Evil': 'Not groovy', 'Scott Evil': "Doesn't want to take over family business"}>,
# 'credits': <Box: {'Austin Powers': 'Mike Myers', 'Vanessa Kensington': 'Elizabeth Hurley'}>}

my_box.to_dict()
# {'owner': 'Mr. Powers', 'affiliates': {'Vanessa': 'Sexy',
# 'Dr Evil': 'Not groovy', 'Scott Evil': "Doesn't want to take over family business"},
# 'credits': {'Austin Powers': 'Mike Myers', 'Vanessa Kensington': 'Elizabeth Hurley'}}

This module was pulled from my other project, reusables, so it has support for a ConfigBox.

test_config.ini

[General]
example=A regular string

[Examples]
my_bool=yes
anint=234
exampleList=234,123,234,543
floatly=4.4

With the combination of reusables and ConfigBox you can easily read python config values into python types. It supports list, bool, int and float.

import reusables
from box import ConfigBox

config = ConfigBox(reusables.config_dict("test_config.ini"))
# <ConfigBox: {'General': {'example': 'A regular string'},
# 'Examples': {'my_bool': 'yes', 'anint': '234', 'examplelist': '234,123,234,543', 'floatly': '4.4'}}>

config.Examples.list('examplelist')
# ['234', '123', '234', '543']

config.Examples.float('floatly')
# 4.4

Similar Libraries

Bunch

Does not work recursively.

EasyDict

EasyDict not have a way to make sub items recursively back into a regular dictionary.

Both EasyDicts str and repr print a dictionary look alike, Box makes it clear in repr that it is a unique object.

addict

Is a default dictionary, as in it will never fail on lookup. It also goes into lists and makes those into sub objects as.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-box-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distributions

python_box-1.0.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

python_box-1.0.0-py2-none-any.whl (9.8 kB view details)

Uploaded Python 2

File details

Details for the file python-box-1.0.0.tar.gz.

File metadata

  • Download URL: python-box-1.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for python-box-1.0.0.tar.gz
Algorithm Hash digest
SHA256 ba79d0fac7e20cb2a233a0914acd476831ad744109d8e3bf80426b0a175791f3
MD5 7e880bea7bd38b382ec194ba18614755
BLAKE2b-256 e9fd03d82bd702d93a2758b32267e23c850f8a58790f0f44842d267b48e83b76

See more details on using hashes here.

File details

Details for the file python_box-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_box-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 271b3515d34dd35c7e13743b4a88e6c5d71e8c6f38c4bc72dff924ca7bec8878
MD5 5d4320a52a10353016cb1279ae3106d2
BLAKE2b-256 2e7d8566218be34585e645ccdebfd5b6bde13a3f6f25c6ff73ac151e267dbb6c

See more details on using hashes here.

File details

Details for the file python_box-1.0.0-py2-none-any.whl.

File metadata

File hashes

Hashes for python_box-1.0.0-py2-none-any.whl
Algorithm Hash digest
SHA256 30d4bd2bd655b1b627f137c87b149d31725d559abed1ca97c8b5d1eb5dfd7ef3
MD5 08fe3c4165350368a8458b96d6929c47
BLAKE2b-256 c3126659c948bb5d96a6d7459bd318d359f43a2608707a7772f1ab023bf0dbce

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page