-
Notifications
You must be signed in to change notification settings - Fork 106
Home
Chris Griffith edited this page Apr 9, 2020
·
6 revisions
Box is designed to be an easy drop in transparently replacements for dictionaries that adds dot notation access and a load of other features!
my_box = Box(funny_movie='Hudson Hawk', best_movie='Kung Fu Panda')
my_box.funny_movie
# 'Hudson Hawk'
By default, aka when conversion_box
is set to True
, it will also tweak keys that have spaces or bad characters to be dot accessible.
my_box["good tv series"] = "I Dream of Jeannie"
my_box["amazing tv series"] = "Avatar: The Last Airbender"
my_box.good_tv_series
# 'I Dream of Jeannie'
It's also super easy to convert boxes to and from markup languages such as json
and yaml
(see all the converters here)
my_box.to_json(filename="films.json")
new_box = Box.from_json(filename="films.json")
new_box.keys()
# dict_keys(['funny_movie', 'weird_movie', 'best_movie',
# 'good tv series', 'amazing tv series'])
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.
big_box = Box({"boxes": {"the": {"whole": {"way": {"down": "yup"}}}}})
big_box.boxes.the.whole.way.down
# 'yup'
These documents are for Box 5.0+