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": [{"qty": 1, "item": "blue crushed-velvet suit"},
{"qty": 1, "item": "frilly lace crava"},
{"qty": 1, "item": "gold medallion with peace symbol"},
{"qty": 1, "item": "Swedish-made enlarger pump"},
{"qty": 1, "item": "credit card receipt for Swedish-made enlarger pump, "
"signed Austin Powers."},
{"qty": 1, "item": "warranty card for Swedish-made enlarger pump, "
"filled out by Austin Powers."},
{"qty": 1, "item": "book, Swedish-Made Enlarger Pumps and Me"}],
"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.contents[0].item
'blue crushed-velvet suit'
# Here's something that no other library supports (that I know of)
# Automatic creation of Boxes in sub-lists
my_box.contents.append({"qty": 1, "item": "tie-dyed socks"})
my_box.contents[-1].item
'tie-dyed socks'
# Box object is callable, and returns a tuple of available keys
my_box()
('owner', 'contents', 'affiliates')
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'}>
my_box.to_yaml() # .to_json() also available
# owner: Mr. Powers
# affiliates:
# Dr Evil: Not groovy
# Scott Evil: Doesn't want to take over family business
# Vanessa: Sexy
# contents:
# - item: blue crushed-velvet suit
# qty: 1
# - item: frilly lace crava
# qty: 1
# - item: gold medallion with peace symbol
# qty: 1
# ...
Install
pip install python-box
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!
If you want to be able to use the to_yaml functionality make sure to install PyYAML as well.
Overview
Box is 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.
del my_box.contents # Lets keep this short
my_box.to_dict()
{'affiliates':
{'Dr Evil': 'Not groovy',
'Scott Evil': "Doesn't want to take over family business",
'Vanessa': 'Sexy'},
'owner': 'Mr. Powers'}
# 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"}>}}
Box was originally named Namespaces in the reusables project, created over three years ago. LightBox is the direct dependant of Namespace, and should operate as a near drop in replacement if you are switching over.
Box
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')
Any time a list or dict is added to a Box, it is converted into a BoxList or Box respectively.
Box includes helper functions to transform it back into dict, and into JSON or YAML strings.
to_dict
Return the Box object and all sub Box and BoxList objects into regular dicts and list.
my_box.to_dict()
{'owner': 'Mr. Powers',
'affiliates': {'Vanessa': 'Sexy',
'Dr Evil': 'Not groovy',
'Scott Evil': "Doesn't want to take over family business"}}
to_json
Available on all systems that support the default json library.:
to_json(filename=None, indent=4, **json_kwargs) Transform the Box object into a JSON string. :param filename: If provided will save to file :param indent: Automatic formatting by indent size in spaces :param json_kwargs: additional arguments to pass to json.dump(s) :return: string of JSON or return of `json.dump`
my_box.to_json()
{
"owner": "Mr. Powers",
"affiliates": {
"Vanessa": "Sexy",
"Dr Evil": "Not groovy",
"Scott Evil": "Doesn't want to take over family business"
}
}
to_yaml
Only available if PyYAML is installed (not automatically installed via pip or setup.py):
to_yaml(filename=None, default_flow_style=False, **yaml_kwargs) Transform the Box object into a YAML string. :param filename: If provided will save to file :param default_flow_style: False will recursively dump dicts :param yaml_kwargs: additional arguments to pass to yaml.dump :return: string of YAML or return of `yaml.dump`
my_box.to_yaml()
affiliates:
Dr Evil: Not groovy
Scott Evil: Doesn't want to take over family business
Vanessa: Sexy
owner: Mr. Powers
BoxList
To make sure all items added to lists in the box are also converted, all lists are covered into BoxList. It’s possible to initiate these directly and use them just like a Box.
from box import BoxList
my_boxlist = BoxList({'item': x} for x in range(10))
# <BoxList: [<Box: {'item': 0}>, <Box: {'item': 1}>, ...
my_boxlist[5].item
5
to_list
Transform a BoxList and all components back into regular list and dict items.
my_boxlist.to_list()
# [{'item': 0},
# {'item': 1},
# ...
LightBox
LightBox does not examine lists, but only converts dictionary objects.
from box import LightBox
light_box = LightBox({'my_list': [{'item': 1}, {'item': 2}])
light_box.my_list
[{'item': 1}, {'item': 2}]
ConfigBox
This module has support for a ConfigBox. It is based on top of LightBox as there are no lists of dicts to dive into in a configuration file.
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.
Adding new dicts to lists in the dictionary does not make them into EasyDicts.
Both EasyDicts str and repr print a dictionary look alike, Box makes it clear in repr that it is a Box object.
addict
Adding new dicts or lists does not make them into addict.Dict objects.
Is a default dictionary, as in it will never fail on lookup.
Both EasyDicts str and repr print a dictionary look alike, Box makes it clear in repr that it is a Box object.
License
MIT License, Copyright (c) 2017 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 Distributions
File details
Details for the file python-box-2.0.0.tar.gz
.
File metadata
- Download URL: python-box-2.0.0.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf74ad500ddb3688427176aa5ff994b0bb195abad03b865f9f8ca2b86bc67647 |
|
MD5 | e944708ef392afdfe711c98c3f958784 |
|
BLAKE2b-256 | 21010a3610ecfa59ec7285a67c01f5946e3426746c469442f4779bee8568a511 |
File details
Details for the file python_box-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: python_box-2.0.0-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ada517259e2b9c42a8e12132f2b9068a4a24a1f77e5afa024b1fbbef3ba182b0 |
|
MD5 | 2dfd55e0a5d4f736a472300ef2dcec54 |
|
BLAKE2b-256 | 43e1da611ca37b3e081fb0a55f6dc28f387e2f38ceb20c28ef01f39f37f68cf9 |
File details
Details for the file python_box-2.0.0-py2-none-any.whl
.
File metadata
- Download URL: python_box-2.0.0-py2-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a03bdab83aa338ea0aa516000b38b6309361d2ac5e2e7daf25f652a803b980fc |
|
MD5 | 62c347bbf73e32de843755cb700f379d |
|
BLAKE2b-256 | 3334c18ad72e934319310ec3fd27169cb3e2283295a8048456fef7e20d2d6459 |