Skip to main content

Strictus Dictus

Project description

pip install strictus-dictus

StrictusDictus 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 quick 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.

from strictus_dictus import StrictusDictus

class Header(StrictusDictus):
    title: str
    sent: str

class Tag(StrictusDictus):
    value: str

class Message(StrictusDictus):
    header: Header
    body: str
    tags: List[Tag]

source = {
    "header": {
        "title": "Hello, world!",
        "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"

# 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. 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.

A StrictusDictus class cannot reference itself in its type hints (not even with forward references).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

strictus_dictus-0.0.5-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file strictus-dictus-0.0.5.macosx-10.13-x86_64.tar.gz.

File metadata

  • Download URL: strictus-dictus-0.0.5.macosx-10.13-x86_64.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using 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.1 CPython/3.6.6

File hashes

Hashes for strictus-dictus-0.0.5.macosx-10.13-x86_64.tar.gz
Algorithm Hash digest
SHA256 a6e7ccf088062d21a1b8398cca3df41622ac0fa6cd8d77c23c5dcf1cb4559b4c
MD5 e08710e39bee70fe4f25d7d438da8f59
BLAKE2b-256 7a01531bb87a3f8aa60feadc14deaa9befc7a7e298d99fa8545535aed1eb75a6

See more details on using hashes here.

File details

Details for the file strictus_dictus-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: strictus_dictus-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using 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.1 CPython/3.6.6

File hashes

Hashes for strictus_dictus-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3a520c51013ddefe96f64a7de27fcc121aae3adada670f4781721c08d81365e6
MD5 422ac9c8d8207c2e7129eefd6c0b17e4
BLAKE2b-256 df5c5c86cff843eeff52899a87771efa4840fd0f40aeee501ae8e0d83b1dbebd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page