Strictus Dictus
pip install strictus-dictus
StrictusDictus (aka sdict) is a base class for special dict sub-classes, instances of which only accept keys that are declared in the class’s type hints.
This is useful for data transfer object definitions, for example, when you are expressing someone else’s JSON or YAML schema in your code and want to access the contents of the parsed dictionaries using dot notation and have your IDE auto-complete the attribute names.
sdict is suitable for nested structures.
from strictus_dictus import sdict
class Header(sdict):
title: str = "Hello, world!" # default value
sent: str
class Tag(sdict):
value: str
class Message(sdict):
header: Header
body: str
tags: List[Tag]
source = {
"header": {
"sent": "2018-10-20 18:09:42",
},
"body": "What is going on?",
"tags": [
{
"value": "unread",
},
],
}
# Parse the message
message = Message(source)
# Access attributes
assert message.header.title == "Hello, world!"
assert message.tags[0].value == "unread"
# It still is a dictionary so this works too:
assert message["header"]["title"] == "Hello, world!"
# Convert back to a standard dictionary
message.to_dict()
The values of these keys are accessible as attributes with dot notation as well as with [] notation, however, if the source dictionary is missing the key, StrictusDictus will not introduce it so access via [] notation will raise a KeyError as expected. However, the attribute will be initialised to hold the special EMPTY value.
To create an instance use YourClass(standard_dict) and to export to a standard dictionary use YourClass().to_dict().
Only a limited set of type hints are supported by StrictusDictus. Unsupported type hints will be silently ignored and values will be returned unprocessed.
Supported type hints are (SD denotes any class inheriting from StrictusDictus):
class Examples:
x1: primitive_type # could be any type, but not from typing.*; value won't be processed
x2: List # unprocessed list
x3: Dict # unprocessed dictionary
x4: SD
x5: List[SD]
x6: Dict[str, SD]
You can annotate x with List[Any] and Dict[Any, Any], but the values won’t be processed by StrictusDictus.
Limitations
An sdict sub-class cannot reference itself in its type hints (not even with forward references).
Dataclasses?
Dataclass is a great building block, but it doesn’t treat dictionaries seriously.
@dataclasses.dataclass
class Point:
x: float
y: float
@dataclasses.dataclass
class Line:
start: Point
end: Point
line = Line(**{"start": {"x": 1, "y": 1}, "end": {"x": 5, "y": 5}})
I would expect line.end.y to hold value 5 , but that’s not the case. In fact, print(line.end.y) raises an AttributeError:
AttributeError: 'dict' object has no attribute 'y'
Metadata
Release files for strictus-dictus 0.0.12
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 | Actions |
|---|---|---|---|---|
| strictus-dictus-0.0.12.tar.gz | 5.2 kB | Without provenance | Details for strictus-dictus-0.0.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| strictus_dictus-0.0.12-py3-none-any.whl | Python 3 | none | any | Details for strictus_dictus-0.0.12-py3-none-any.whl |
Total release size: 10.3 kB
Release files / strictus-dictus-0.0.12.tar.gz
| Download URL | strictus-dictus-0.0.12.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cbef15010006ec3154266e60abdc5dd8ea6bd0f5a46c65c633d9c8b617c64e93
|
|
BLAKE2b-256 checksum How to use checksums |
d69ba43c3a6236c57f0d3cb9d9039e6bc138474f2957a7c18ce719a55caab441
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6
|
Release files / strictus_dictus-0.0.12-py3-none-any.whl
| Download URL | strictus_dictus-0.0.12-py3-none-any.whl |
|---|---|
| Size | 5.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
69377eb506b0e4ae303568d722240b1ffe42bc7fd385766714656c6954d8265a
|
|
BLAKE2b-256 checksum How to use checksums |
3339d0d1d331a1f6f159e8143dc0346f413fd6813cfe3faf4bc269ebcd7ea0a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.0 CPython/3.6.6
|
Security for strictus-dictus 0.0.12
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.