Advanced Python dictionaries with dot notation access
Project description
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
# 6.7
Box will automatically make otherwise inaccessible keys safe to access as an attribute. You can always pass conversion_box=False to Box to disable that behavior.
Also, all new dict and lists added to a Box or BoxList object are converted automatically.
Check out the new wiki!
Install
pip install --upgrade python-box[all]
Box 5 is no longer forcing install of external dependencies such as yaml and toml. Instead you can specify which you want, for example, all is shorthand for:
pip install --upgrade [ruamel.yaml,toml,msgpack]
But you can also sub out “ruamel.yaml” for “PyYAML”.
Check out more details on installation details.
Box 5 is tested on python 3.6+ and pypy3, if you are upgrading from previous versions, please look through any breaking changes and new features.
If you have any issues please open a github issue with the error you are experiencing!
Overview
Box is designed to be an easy drop in transparently replacements for dictionaries, thanks to Python’s duck typing capabilities, which adds dot notation access. 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.
Check out the Quick Start for more in depth details.
Box can be instantiated the same ways as dict.
Box({'data': 2, 'count': 5})
Box(data=2, count=5)
Box({'data': 2, 'count': 1}, count=5)
Box([('data', 2), ('count', 5)])
# All will create
# <Box: {'data': 2, 'count': 5}>
Box is a subclass of dict which overrides some base functionality to make sure everything stored in the dict can be accessed as an attribute or key value.
small_box = Box({'data': 2, 'count': 5})
small_box.data == small_box['data'] == getattr(small_box, 'data')
All dicts (and lists) added to a Box will be converted on lookup to a Box (or BoxList), allowing for recursive dot notation access.
Box also includes helper functions to transform it back into a dict, as well as into JSON, YAML, TOML, or msgpack strings or files.
Thanks
A huge thank you to everyone that has given features and feedback over the years to Box! Check out everyone that has contributed.
A big thanks to Python Software Foundation, and PSF-Trademarks Committee, for official approval to use the Python logo on the Box logo!
Also special shout-out to PythonBytes, who featured Box on their podcast.
License
MIT License, Copyright (c) 2017-2020 Chris Griffith. See LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file python-box-5.0.0a1.tar.gz
.
File metadata
- Download URL: python-box-5.0.0a1.tar.gz
- Upload date:
- Size: 33.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3061b3fcd08618a14695a5564f83ce6d7dffdd5ce478ca0ad208240e428bfdc0 |
|
MD5 | 1c91d855d6f8040dac6b168150689263 |
|
BLAKE2b-256 | 020c28c67bdd8dbc41efccc311c87df0f92d02f0bfafd81d81e57720e882b8f3 |
File details
Details for the file python_box-5.0.0a1-py3-none-any.whl
.
File metadata
- Download URL: python_box-5.0.0a1-py3-none-any.whl
- Upload date:
- Size: 20.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c45c03c091229f904b8155811bcca10f82917e86b53b353d6e27553e4398c216 |
|
MD5 | 2fee63a73c270f00150b91d63c011445 |
|
BLAKE2b-256 | 38d8f1e76d35662804d42a7f3c1c6a06351d9fe511f9e7616abfde19cc901c66 |