Package: python-box
fromboximportBoxmovie_box=Box({"Robin Hood: Men in Tights":{"imdb stars":6.7,"length":104}})movie_box.Robin_Hood_Men_in_Tights.imdb_stars# 6.7Box will automatically make otherwise inaccessible keys safe to access as an attribute.
You can always passconversion_box=FalsetoBoxto 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 newBox github wikifor more details and examples!InstallVersion Pin Your Box!If you aren’t in the habit of version pinning your libraries, it will eventually bite you.
Box has alist of breaking changebetween major versions you should always check out before updating.requirements.txtpython-box[all]~=7.0As Box adheres to semantic versioning (aka API changes will only occur on between major version),
it is best to useCompatible releasematching using the~=clause.Install from command linepython-mpipinstall--upgradepippipinstallpython-box[all]~=7.0--upgradeInstall with selected dependenciesBox does not install external dependencies such as yaml and toml writers. Instead you can specify which you want,
for example,[all]is shorthand for:pipinstallpython-box[ruamel.yaml,tomli_w,msgpack]~=7.0--upgradeBut you can also sub outruamel.yamlforPyYAML.Check outmore detailson installation details.Box 7 is tested on python 3.7+, if you are upgrading from previous versions, please look throughany breaking changes and new features.Optimized VersionBox has introduced Cython optimizations for major platforms by default.
Loading large data sets can be up to 10x faster!If you arenoton 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 packagesCythonandwheel.Linux Example:First make sure you have python development files installed (python3-devorpython3-develin most repos).
You will then needCythonandwheelinstalled and then install (or re-install with–force)python-box.pipinstallCythonwheelpipinstallpython-box[all]~=7.0--upgrade--forceIf you have any issues please open a github issue with the error you are experiencing!OverviewBoxis designed to be a near transparent drop in replacements for
dictionaries that add dot notation access and other powerful feature.There are a lot oftypes of boxesto customize it for your needs, as well as handyconverters!Keep in mind any sub dictionaries or ones set after initiation will be automatically converted to
aBoxobject, and lists will be converted toBoxList, all other objects stay intact.Check out theQuick Startfor more in depth details.Boxcan be instantiated the same ways asdict.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}>Boxis a subclass ofdictwhich 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 aBoxwill be converted on insertion to aBox(orBoxList),
allowing for recursive dot notation access.Boxalso includes helper functions to transform it back into adict,
as well as intoJSON,YAML,TOML, ormsgpackstrings or files.ThanksA huge thank you to everyone that has given features and feedback over the years to Box! Check out everyone that hascontributed.A big thanks to Python Software Foundation, and PSF-Trademarks Committee, for official approval to use the Python logo on theBoxlogo!Also special shout-out toPythonBytes, who featured Box on their podcast.LicenseMIT License, Copyright (c) 2017-2023 Chris Griffith. SeeLICENSEfile.