elegant-json
Deal with JSON files elegantly.
Warning
elegant-jsonis Work in Progress!
Installation
- use pip
pip install elegant-json
- clone repo
git clone https://github.com/hanjinliu/elegant-json
Motivation
Suppose you have a JSON file in the following format.
{
"title": "Title",
"data": {
"last modified": "2022.06.01",
"values": [0, 1, 2, 3]
}
}
What would you do?
Conventional way
import json
with open("path/to/json") as f:
js = json.load(f)
# `js` is a nested dictionary
js["title"]
js["data"]["last modified"]
js["data"]["values"][2]
This is terrible.
✗ typing
✗ missing values
✗ readability
In this module
Copy and paste the json text and substitute values you want with Attr objects.
from elegant_json import JsonClass, Attr
# define a class with a json template
class C(JsonClass):
__json_template__ = {
"title": Attr(),
"data": {
"last modified": Attr(name="last_modified"),
"values": Attr()
}
}
title: str
last_modified: str
values: list[int]
c = C.load("path/to/json") # or from a dict >>> c = C(js)
# now all the attributes can be accessed like below
c.title
c.last_modified
c.values[2]
Other helper functions
-
isformattedThis function checks if a dictionary is in the given format
from elegant_json import isformatted dict0 = { "title": "Formatted data", "data": { "last modified": "yyyy.mm.dd", "values": [] } } isformatted(dict0, C) # True dict1 = { "title": "Wrong data", "data": { "a": 0, "b": 2, } } isformatted(dict1, C) # False
Metadata
Release files for elegant-json 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| elegant-json-0.1.0.tar.gz | 6.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| elegant_json-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 14.2 kB
Release files / elegant-json-0.1.0.tar.gz
| Download URL | elegant-json-0.1.0.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
efdd48fdde37181d87584074844f1d985d8a5f1cc4dfb6b1b226b5ae1e437ba3
|
|
BLAKE2b-256 checksum How to use checksums |
a6d194216b42ec1e437d72824267ed34581204e87915548f44293fb810182e7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.9.7
|
Release files / elegant_json-0.1.0-py3-none-any.whl
| Download URL | elegant_json-0.1.0-py3-none-any.whl |
|---|---|
| Size | 7.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7fde6de32a23fbbb1a9696a162af0e3c64390f1eea52ddcf210489362d688a5e
|
|
BLAKE2b-256 checksum How to use checksums |
283be65cd6b663944a6e279b67ab604ea8597d516b981aa26a0e34575bb1aab0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.63.0 CPython/3.9.7
|
Security for elegant-json 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.