More Dots! Dot-access to Python dicts like Javascript
Project description
Defines a Data class that is much like dict but with dot-property accessors, and a few other features:
Quick Overview
This library defines a Data class that can serve as a replacement for dict, with additional features.
>>> from mo_dots import wrap, Data
See the full documentation for all the features of Mo-Dots.
Easy Definition
Define Data using named parameters, just like you would a dict
>>> Data(b=42, c="hello world") Data({'b': 42, 'c': 'hello world'})
You can also wrap existing dicts so they can be used like Data
>>> wrap({'b': 42, 'c': 'hello world'}) Data({'b': 42, 'c': 'hello world'})
Dot Access
Access properties with attribute dots: a.b == a["b"]. You have probably seen this before.
Path Access
Access properties by dot-delimited path.
>>> a = wrap({"b": {"c": 42}}) >>> a["b.c"] == 42 True
Safe Access
If a property does not exist then return Null rather than raising an error.
>>> a = Data() a == {} >>> a.b == None True >>> a.b.c == None True >>> a[None] == None True
Path assignment
No need to make intermediate objects
>>> a = Data() a == {} >>> a["b.c"] = 42 # same as a.b.c = 42 a == {"b": {"c": 42}}
Path accumulation
Use += to add to a property; default zero (0)
>>> a = Data() a == {} >>> a.b.c += 1 a == {"b": {"c": 1}} >>> a.b.c += 42 a == {"b": {"c": 43}}
Use += with a list ([]) to append to a list; default empty list ([])
>>> a = Data() a == {} >>> a.b.c += [1] a == {"b": {"c": [1]}} >>> a.b.c += [42] a == {"b": {"c": [1, 42]}}
Transforming JSON Data in Python
This library is the basis for a data transformation algebra: We want a succinct way of transforming data in Python. We want operations on data to result in yet more data. We do not want data operations to raise exceptions. This library is solves Python’s lack of consistency (lack of closure) under the dot (.) and slice [::] operators when operating on data objects.
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
File details
Details for the file mo-dots-1.5.17227.zip
.
File metadata
- Download URL: mo-dots-1.5.17227.zip
- Upload date:
- Size: 27.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9d60af745a03a2b2c5975dcf5a8e4acbb1e5e3e85ed8e8ebd0c65ddb1869c200 |
|
MD5 | b82f3c9aa8be699e5db2b05a1af18ad4 |
|
BLAKE2b-256 | fe379f020335dfb50d4ff6478a115771eba68b7ebe39420fa1433555c91b69d0 |