JDICT
JavaScript-like Python dictionary. For background and design description look at this Medium article.
Installation
Use the package manager pip to install jdict from the PyPi site:
pip3 install pyjdict
The package name is
pyjdict, but the module name isjdict.
What is jdict?
Let's imagine we have some deeply nested json structure as following(this is not really deep, but it's enough for the sake of example):
{
"status": True,
"data": {
"file": {
"url": {
"short": "https://anonfiles.com/t3l5S2Gex3",
"full": "https://anonfiles.com/t3l5S2Gex3/test_txt",
},
"metadata": {
"size": {"bytes": 19, "readable": "19 B"},
"name": "test_txt",
"id": "t3l5S2Gex3",
},
}
},
}
Now, you need get the id of this data, yes you are going to feel the pain as: this_awesome_json["data"]["file"]["metadata"]["id"].
But how about accessing this id as: this_awesome_json.data.file.metadata.id? Even for loop and assign values directly using .[dot] access?
That's where the jdict shines. It is lightweight, nearly zero cost library converts your dictionaries to special jdict type and you are ready to go.
Usage
Now let's build small script to show the jdict. We are going to use anonymous file upload public API.
import requests
import json
from jdict import jdict, set_json_decoder
# Send post request and upload the test.txt file - you can create one
def _upload_file() -> str:
file = {"file": open("test.txt", "rb")}
result = requests.post("https://api.anonfiles.com/upload", files=file)
result_dict = result.json()
# Feel the pain here
return result_dict["data"]["file"]["metadata"]["id"]
# Send get request and get back the json information about the uploaded file
def _get_file_info() -> dict:
url = f"https://api.anonfiles.com/v2/file/{_upload_file()}/info"
return requests.get(url).json()
# Change codec to use jdict
def _convert_to_jdict() -> jdict:
set_json_decoder(json)
return _get_file_info()
The killer point here is to change the codec and then convert our dictionary to jdict.
If you are using simplejson, just pass it as pyjdict.set_codec(simplejson), it will do the same trick.
Great, now we are ready to use it:
if __name__ == "__main__":
"""
Sample data:
{
"status": True,
"data": {
"file": {
"url": {
"short": "https://anonfiles.com/t3l5S2Gex3",
"full": "https://anonfiles.com/t3l5S2Gex3/test_txt",
},
"metadata": {
"size": {"bytes": 19, "readable": "19 B"},
"name": "test_txt",
"id": "t3l5S2Gex3",
},
}
},
}
"""
data = _convert_to_jdict()
# Get the id:
print("ID: ", data.data.file.metadata.id)
# Use in for loop:
for key, value in data.data.file.metadata.items():
print(key, value)
# Set the id:
data.data.file.metadata.id = "MYID"
print("ID: ", data.data.file.metadata.id)
Sample output:
$ python3 main.py
ID: h8p2SbGfx2
size {'bytes': 19, 'readable': '19 B'}
name test_txt
id h8p2SbGfx2
ID: MYID
Patching
The next crucial feature is to ability to path core libraries with jdict.
Just think about the boto3 library, with AWS you may encounter really deeply nested json structures,
with jdict you can access those nested values with .[dot] notation as well.
By patching botocore.parsers you gain really powerful tooling to work with:
import os
import boto3
import jdict
jdict.patch_module('botocore.parsers')
def test_library():
response = boto3.client('s3').list_buckets()
assert(response.Buckets == response['Buckets'])
return 'OK'
Just keep in mind that you need to have valid AWS credentials to run this code
License
MIT License, Copyright (c) 2021-2022 BST LABS. See LICENSE file.
Metadata
Release files for pyjdict 1.0.6
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 | |
|---|---|---|---|---|
| pyjdict-1.0.6.tar.gz | 12.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyjdict-1.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 19.5 kB
Release files / pyjdict-1.0.6.tar.gz
| Download URL | pyjdict-1.0.6.tar.gz |
|---|---|
| Size | 12.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b363e6ba25830fc57d0387c2c7ef025ea6c5ad65e75da7c7828d557c36d056e6
|
|
BLAKE2b-256 checksum How to use checksums |
71812bc6742f2ea9e92a3854cd8ad86f224ff2ebd77b00c58fced34bfa1226a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.27.1
|
Release files / pyjdict-1.0.6-py3-none-any.whl
| Download URL | pyjdict-1.0.6-py3-none-any.whl |
|---|---|
| Size | 6.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
79994553a1db60668c3fb5ba8a3d21fd7e56af7d15b0708b9ae08b6927594d10
|
|
BLAKE2b-256 checksum How to use checksums |
395e3ec42419cd0a408a11b2fb3240e07c8e481c5abef397dc5dd5e29f97c273
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.27.1
|
Security for pyjdict 1.0.6
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.