Softy
Soft access of nested data for more readable code
- Lightweight
- Pure Python
- One source file
- < 150 lines of code
- No dependencies
An item:
basket = {
"Fruits": [
{
"Type": "Apple",
"Color": "Green"
},
{
"Type": "Apple",
"Color": "Red"
}
],
"Blanket": {
"Material": "Cotton",
"Color": "Red"
}
}
Before:
# get the blanket color
blanket = basket.get('Blanket')
blanket_color = None
if blanket:
blanket_color = blanket.get('Color')
if blanket_color is not None:
print(f'Blanket is {blanket_color}')
else:
print('Unspecified blanket color')
# get the color of the third fruit
fruits = basket.get('Fruits')
fruit_color = None
if fruits is not None:
if len(fruits) > 2:
fruit_color = fruits[2].get('Color')
if fruit_color is not None:
print(f'3rd fruit color is {fruit_color}')
else:
print('Unspecified 3rd fruit color')
After:
import softy
basket = softy.soften(basket)
# get the blanket color
if basket.Blanket.Color is not softy.null:
print(f'Blanket is {basket.Blanket.Color}')
else:
print('Unspecified blanket color')
# get the color of the third fruit
if basket.Fruits.i(2).Color is not softy.null:
print(f'3rd fruit color is {fruit_color}')
else:
print('Unspecified 3rd fruit color')
# built-in indexing still works the same
try:
wine = basket['Wine']
except KeyError:
print('Forgot the wine')
else:
assert False
Metadata
Release files for softy 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| softy-0.0.4.tar.gz | 5.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| softy-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.5 kB
Release files / softy-0.0.4.tar.gz
| Download URL | softy-0.0.4.tar.gz |
|---|---|
| Size | 5.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
710c1e6fa56b4597ba07b83d1eca9175dcbf0c26a820d88f732561887e5a8c89
|
|
BLAKE2b-256 checksum How to use checksums |
4844a6c31caeba669956918e21f8dc13149e2b23f21a0eb8636fd960ff85afd6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|
Release files / softy-0.0.4-py3-none-any.whl
| Download URL | softy-0.0.4-py3-none-any.whl |
|---|---|
| Size | 3.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75009965e42e6907bd6092e79fa951c9cb8d6c003b0155db25f6ee7d8d69a9c0
|
|
BLAKE2b-256 checksum How to use checksums |
3200f80ab6ee88f91bb33879b1a125cdabd110cd081ff226535e7a6d07e1b279
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/4.0.2 CPython/3.11.4
|