Skip to main content

Advanced Python dictionaries with dot notation access

Project description

BuildStatus License

BoxImage

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.

There are over a half dozen ways to customize your Box and make it work for you.

Check out the new Box github wiki for more details and examples!

Install

Version Pin Your Box!

If you aren’t in the habit of version pinning your libraries, it will eventually bite you. Box has a list of breaking change between major versions you should always check out before updating.

requirements.txt

python-box[all]~=7.0

As Box adheres to semantic versioning (aka API changes will only occur on between major version), it is best to use Compatible release matching using the ~= clause.

Install from command line

python -m pip install --upgrade pip
pip install python-box[all]~=7.0 --upgrade

Install with selected dependencies

Box does not install external dependencies such as yaml and toml writers. Instead you can specify which you want, for example, [all] is shorthand for:

pip install python-box[ruamel.yaml,tomli_w,msgpack]~=7.0 --upgrade

But you can also sub out ruamel.yaml for PyYAML.

Check out more details on installation details.

Box 7 is tested on python 3.7+, if you are upgrading from previous versions, please look through any breaking changes and new features.

Optimized Version

Box has introduced Cython optimizations for major platforms by default. Loading large data sets can be up to 10x faster!

If you are not on a x86_64 supported system you will need to do some extra work to install the optimized version. There will be an warning of “WARNING: Cython not installed, could not optimize box” during install. You will need python development files, system compiler, and the python packages Cython and wheel.

Linux Example:

First make sure you have python development files installed (python3-dev or python3-devel in most repos). You will then need Cython and wheel installed and then install (or re-install with –force) python-box.

pip install Cython wheel
pip install python-box[all]~=7.0 --upgrade --force

If you have any issues please open a github issue with the error you are experiencing!

Overview

Box is designed to be a near transparent drop in replacements for dictionaries that add dot notation access and other powerful feature.

There are a lot of types of boxes to customize it for your needs, as well as handy converters!

Keep in mind any sub dictionaries or ones set after initiation will be automatically converted to a Box object, and lists will be converted to BoxList, all other objects stay intact.

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 insertion 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-2023 Chris Griffith. See LICENSE file.

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-7.1.0.tar.gz (44.7 kB view details)

Uploaded Source

Built Distributions

python_box-7.1.0-py3-none-any.whl (29.0 kB view details)

Uploaded Python 3

python_box-7.1.0-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11 Windows x86-64

python_box-7.1.0-cp311-cp311-macosx_10_9_universal2.whl (1.8 MB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

python_box-7.1.0-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10 Windows x86-64

python_box-7.1.0-cp310-cp310-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

python_box-7.1.0-cp39-cp39-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.9 Windows x86-64

python_box-7.1.0-cp39-cp39-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

python_box-7.1.0-cp38-cp38-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.8 Windows x86-64

python_box-7.1.0-cp38-cp38-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8 macOS 11.0+ x86-64

File details

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

File metadata

  • Download URL: python-box-7.1.0.tar.gz
  • Upload date:
  • Size: 44.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for python-box-7.1.0.tar.gz
Algorithm Hash digest
SHA256 695eab5c10198d76011f2020ba4dec4a41f640a389fd9991169a060eb1ed132f
MD5 29c4a3f940a407cbecf418def46f0652
BLAKE2b-256 d4ebe2fef51eeb6104216c54e0ebf40b8bb9ca5dfd365f99f7ef0c2172176cab

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: python_box-7.1.0-py3-none-any.whl
  • Upload date:
  • Size: 29.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for python_box-7.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56102faf1609e2ecde1d9e3562730c45fe65a3263458ca5212f7d82c65d357c8
MD5 af9fc0f91383adf19cd7a861bdfb6709
BLAKE2b-256 b0bfebfb15f99d9d23d0526e531d0ca84f71ed7ecccac1f7ab7af493c2b1fa5f

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 9dc25f3e898b664b18fd0ccb9ec03c79b176fb917d2b59a83fa7e1886fe401b5
MD5 22b0b5d168b25b4f0675af3b67ccbcfe
BLAKE2b-256 22e200fdda701ad32786cb25db25c4a733845e2c7a15bf50b0ae9ec3bc4ba472

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 90039cd61f0518d9f8d36e54074bdd4b75e44de333bb47a9c69a7fd36d93899a
MD5 ff1de5a461e0b4b09c57f504272a0a8c
BLAKE2b-256 10de1aaf1d27ed591e4d0abdb94d31841a91f7f067c873b178f459f809e52295

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 0417d7fc8ba53906bd6a5e94703ae857516f135c68bcbcdcd30e42bd18e238b0
MD5 659a5e47afa7bd3146786bdabb11b3f0
BLAKE2b-256 f32eeace5bb50e59366b9d6ba8c565d6ee5f728a573fc8c3bd117989929fe34a

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 a83011fdded8f91f67d6cb116a159a0ab94b178a5b8b735614ed2361c8b14364
MD5 0e817aadb487a946ca004b65bb353405
BLAKE2b-256 59cd0153a25c71fb04f3e295c8f4a523dd8ab74cd38c1b8857a2ba4586763298

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 a5bb32c5e096f019fe775098ab9d504f9cd6cfac0e82818a17acd83128160f5b
MD5 f9849bff4e319a107d8d8efeb6facc0e
BLAKE2b-256 ae4e8a82366919d760280fe45c5592e0bfb29283a01a658c17cf3fe5fffb26a1

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 f053041c5d8c341da36508686a97acc113793cb7837e21fe82bfe1a3e96ef138
MD5 898bde06a2244896f4cbae80c84fff5f
BLAKE2b-256 84833746065d05ff73a4a93dd08ca38ff1068989779d1419e1b8cc097547957e

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp38-cp38-win_amd64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 57acf0699eed5c22949e269f0f5baa2de0d48f1733329ef2f5b94def4374c429
MD5 8de097ec5c90297e25528c057bc96747
BLAKE2b-256 5aad3c370a1ee784a54e980e11aed178d4fd8483f47620467ac6e3082ca68e65

See more details on using hashes here.

Provenance

File details

Details for the file python_box-7.1.0-cp38-cp38-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for python_box-7.1.0-cp38-cp38-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 a78069db82f1879222c4a8aa49685167e3cc9f859a03b8f6de7fd8232c5050e1
MD5 c2bf598c972e670db64c8a965e73af96
BLAKE2b-256 a2358956c0b77716692b7554a087923f57b4dbb7285347755bbd392112f6748b

See more details on using hashes here.

Provenance

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