Python dictionary with dot notation.
Project description
Python dictionary with dot notation
- Partially compartible with
python-box
- Issue #87 of
python-box
fixed - Customization of key conversion
from diot import Diot
movie_data = {
"movies": {
"Spaceballs": {
"imdb stars": 7.1,
"rating": "PG",
"length": 96,
"director": "Mel Brooks",
"stars": [{"name": "Mel Brooks", "imdb": "nm0000316", "role": "President Skroob"},
{"name": "John Candy","imdb": "nm0001006", "role": "Barf"},
{"name": "Rick Moranis", "imdb": "nm0001548", "role": "Dark Helmet"}
]
},
"Robin Hood: Men in Tights": {
"imdb stars": 6.7,
"rating": "PG-13",
"length": 104,
"director": "Mel Brooks",
"stars": [
{"name": "Cary Elwes", "imdb": "nm0000144", "role": "Robin Hood"},
{"name": "Richard Lewis", "imdb": "nm0507659", "role": "Prince John"},
{"name": "Roger Rees", "imdb": "nm0715953", "role": "Sheriff of Rottingham"},
{"name": "Amy Yasbeck", "imdb": "nm0001865", "role": "Marian"}
]
}
}
}
# Box is a conversion_box by default, pass in `conversion_box=False` to disable that behavior
# Explicitly tell Diot to convert dict/list inside
movie_diot = Diot(movie_data)
movie_diot.movies.Robin_Hood_Men_in_Tights.imdb_stars
# 6.7
movie_diot.movies.Spaceballs.stars[0].name
# 'Mel Brooks'
# Different as box, you have to use Diot for new data in a list
movie_diot.movies.Spaceballs.stars.append(
Diot({"name": "Bill Pullman", "imdb": "nm0000597", "role": "Lone Starr"}))
movie_diot.movies.Spaceballs.stars[-1].role
# 'Lone Starr'
Install
pip install diot
Diot
Instantiated the same ways as dict
Diot({'data': 2, 'count': 5})
Diot(data=2, count=5)
Diot({'data': 2, 'count': 1}, count=5)
Diot([('data', 2), ('count', 5)])
# All will create
# Diot([('data', 2), ('count', 5)], diot_nest = True, diot_transform = 'safe')
Same as python-box
, Diot
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.
diot = Diot({'data': 2, 'count': 5})
diot.data == diot['data'] == getattr(diot, 'data')
By default, diot uses a safe transformation to transform keys into safe names that can be accessed by diot.xxx
dt = Diot({"321 Is a terrible Key!": "yes, really"})
dt._321_Is_a_terrible_Key_
# 'yes, really'
Different as python-box
, duplicate attributes are not allowed.
dt = Diot({"!bad!key!": "yes, really", ".bad.key.": "no doubt"})
# KeyError
Use different transform functions:
dt = Diot(oneTwo = 12, diot_transform = 'snake_case')
# or use alias:
# dt = SnakeDiot(oneTwo = 12)
dt.one_two == dt['one_two'] == dt['oneTwo'] == 12
dt = Diot(one_two = 12, diot_transform = 'camel_case')
# or use alias:
# dt = CamelDiot(one_two = 12)
dt.oneTwo == dt['one_two'] == dt['oneTwo'] == 12
dt = Diot(one_two = 12, diot_transform = 'upper')
dt.ONE_TWO == dt['one_two'] == dt['ONETWO'] == 12
dt = Diot(ONE_TWO = 12, diot_transform = 'lower')
dt.one_two == dt['ONE_TWO'] == dt['one_two'] == 12
Use your own transform function:
import inflection
dt = Diot(post = 10, diot_transform = inflection.pluralize)
dt.posts == dt['posts'] == dt['post'] == 10
OrderedDiot
diot_of_order = OrderedDiot()
diot_of_order.c = 1
diot_of_order.a = 2
diot_of_order.d = 3
list(diot_of_order.keys()) == ['c', 'a', 'd']
# insertion allowed for OrderedDiot
od = OrderedDiot()
od.insert(0, "c", "d")
od.insert(None, "x", "y")
od.insert_before('c', "e", "f")
od.insert_after("a", ("g", "h"))
od2 = OrderedDiot()
od2.a1 = 'b1'
od2.c1 = 'd1'
od.insert(-1, od2)
od3 = OrderedDiot()
od3.a2 = 'b2'
od3.c2 = 'd2'
od.insert_before('c', od3)
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
diot-0.0.7.tar.gz
(7.1 kB
view details)
Built Distribution
diot-0.0.7-py3-none-any.whl
(6.0 kB
view details)
File details
Details for the file diot-0.0.7.tar.gz
.
File metadata
- Download URL: diot-0.0.7.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.2 CPython/3.7.1 Linux/4.15.0-1028-gcp
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c8519e8a00297f139f0889294ca09a4b275fb26eeb7d431f7a32a227c3ba51eb |
|
MD5 | f395e3861d3af11c12174b6b3dc6f011 |
|
BLAKE2b-256 | 7e2b662b57d2f03f4d2333ce90a268c59df870211e454afda984ae0f78df614b |
File details
Details for the file diot-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: diot-0.0.7-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.2 CPython/3.7.1 Linux/4.15.0-1028-gcp
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e7c03d820dc5035750c0fe2907dcaa81d55afe9ef4ef38f7a328b1baac6c112 |
|
MD5 | 60e757ee8b2db7e0850a32b56058b2d9 |
|
BLAKE2b-256 | c98c6c462463b8aef1ff1a2d4487d759870e31be15e9ebed07f966570c64430d |