Skip to main content

Advanced Python dictionaries with dot notation access

Project description

BuildStatus CoverageStatus 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.

Check out the new wiki!

Install

pip install --upgrade python-box[ruamel.yaml,toml,msgpack]

Check out more details on installation details and what extras are available.

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


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-5.0.0a0.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

python_box-5.0.0a0-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file python-box-5.0.0a0.tar.gz.

File metadata

  • Download URL: python-box-5.0.0a0.tar.gz
  • Upload date:
  • Size: 32.3 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.45.0 CPython/3.8.2

File hashes

Hashes for python-box-5.0.0a0.tar.gz
Algorithm Hash digest
SHA256 9160d527e359c1008ad72cb9cb84366a65bd1d841259a61b03ad0f12623016ba
MD5 eb63684a4470726d8612b5a575d65646
BLAKE2b-256 2035b0e0880a995b143adb60ce022fe625dd4cd5423b4233e49b5e69c6ab8e4c

See more details on using hashes here.

File details

Details for the file python_box-5.0.0a0-py3-none-any.whl.

File metadata

  • Download URL: python_box-5.0.0a0-py3-none-any.whl
  • Upload date:
  • Size: 20.2 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.45.0 CPython/3.8.2

File hashes

Hashes for python_box-5.0.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 e4379ee64f22351b853251b9f85359ef3a124434fc716b3d0aa15ec3a683c4f3
MD5 29afeb2164c540ef2bb630f5a740e065
BLAKE2b-256 40598742bee4b697128918afb20083d25e6d445f5b4eba64be2531b10cd40286

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