Skip to main content

Data description language for defining data models

Project description

district42

Codecov PyPI PyPI - Downloads Python Version

Data description language for defining data models

(!) Work in progress, breaking changes are possible until v2.0 is released

Installation

pip3 install district42

Usage

from district42 import schema

UserSchema = schema.dict({
    "id": schema.int.min(1),
    "name": schema.str | schema.none,
    "is_deleted": schema.bool,
})

print(UserSchema)

More powerful with:

And more

Documentation

None

schema.none

sch = schema.none

Bool

schema.bool

sch = schema.bool

schema.bool(value)

sch = schema.bool(True)

Int

schema.int

sch = schema.int

schema.int(value)

sch = schema.int(42)

schema.int.min(value)

sch = schema.int.min(0)

schema.int.max(value)

sch = schema.int.max(0)

Float

schema.float

sch = schema.float

schema.float(value)

sch = schema.float(3.14)

schema.float.min(value)

sch = schema.float.min(0.0)

schema.float.max(value)

sch = schema.float.max(0.0)

Str

schema.str

sch = schema.str

schema.str.len(length)

sch = schema.str.len(10)

schema.str.len(min_length, max_length)

sch = schema.str.len(1, ...)
sch = schema.str.len(..., 32)
sch = schema.str.len(1, 32)

schema.str.alphabet(letters)

digits = "01234567890"
sch = schema.str.alphabet(digits)

schema.str.contains(substr)

sch = schema.str.contains("@")

schema.str.regex(pattern)

import re
sch = schema.str.regex(r"[a-z]+")

List

schema.list

sch = schema.list

schema.list(elements)

sch = schema.list([schema.int(1), schema.int(2)])

schema.list(type)

sch = schema.list(schema.int)

schema.list(type).len(length)

sch = schema.list(schema.int).len(3)

schema.list(type).len(min_length, max_length)

sch = schema.list(schema.int).len(1, ...)
sch = schema.list(schema.int).len(..., 10)
sch = schema.list(schema.int).len(1, 10)

Dict

schema.dict

sch = schema.dict

schema.dict(keys)

sch = schema.dict({
    "id": schema.int,
    "name": schema.str | schema.none,
    optional("platform"): schema.str,
})

Any

schema.any

sch = schema.any

schema.any(*types)

sch = schema.any(schema.str, schema.int)

Custom Types

1. Declare Schema

from typing import Any
from uuid import UUID
from district42 import Props, SchemaVisitor, SchemaVisitorReturnType as ReturnType
from district42.types import Schema
from niltype import Nilable


class UUIDProps(Props):
    @property
    def value(self) -> Nilable[UUID]:
        return self.get("value")


class UUIDSchema(Schema[UUIDProps]):
    def __accept__(self, visitor: SchemaVisitor[ReturnType], **kwargs: Any) -> ReturnType:
        return visitor.visit_uuid(self, **kwargs)

    def __call__(self, /, value: UUID) -> "UUIDSchema":
        return self.__class__(self.props.update(value=value))

2. Register Representor

from typing import Any
from district42.representor import Representor
from niltype import Nil


class UUIDRepresentor(Representor, extend=True):
    def visit_uuid(self, schema: UUIDSchema, *, indent: int = 0, **kwargs: Any) -> str:
        r = f"{self._name}.uuid"

        if schema.props.value is not Nil:
            r += f"({schema.props.value!r})"

        return r

3. Use

from uuid import uuid4
from district42 import register_type, schema

register_type("uuid", UUIDSchema)

print(schema.uuid(uuid4()))
# schema.uuid(UUID('ce80d2b7-cdce-4e24-ab26-00c75471ce78'))

Full code available here: district42_exp_types/uuid

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

district42-1.5.0.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

district42-1.5.0-py3-none-any.whl (46.2 kB view details)

Uploaded Python 3

File details

Details for the file district42-1.5.0.tar.gz.

File metadata

  • Download URL: district42-1.5.0.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for district42-1.5.0.tar.gz
Algorithm Hash digest
SHA256 256d78c00a56e069fa017e9407be0f96cd75de6ed1bd36402c758ac98e6aab2e
MD5 e2036f1701a396d118fdfb9024b5b3f8
BLAKE2b-256 d0b6cf8ba7d51cff11cc748d1d587db247352762bb4b3fe78389cab254e2a85b

See more details on using hashes here.

File details

Details for the file district42-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: district42-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 46.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for district42-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6753898679b95a83bccf43310ca778a0d9dada0d411b8571f598de6bd09c762b
MD5 c3873866df8e3be36cc6ecf8b2b331ec
BLAKE2b-256 fc6853dc35f8bd2fc139581db41d5879b8d10caffd89fe7e2007bab526c97afc

See more details on using hashes here.

Supported by

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