Skip to main content

Primitize is a library that facilitates converting dataclass instances into primitive objects.

Project description

Primitize

Primitize is a library that facilitates converting dataclass instances into primitive objects. It provides facilites to massage the data, validate it and write it out to file in pretty much any format you would want.

Interface

from dataclasses import dataclass

from primitize.core import Primitizable, primitize, primitized


@dataclass
class Obj(Primitizable):
    a: int
    b: bool = primitized(modifier=lambda self, b: str(b).lower())

    def primitize(self):
        return json.dump(super().primitize(self))


primitize(Obj(1, True)) == '{"a": 1, "b": "true"}'

Primitize will convert any data class down to primitive types. When calling primitize(d) on any d dataclass, it will execute the d.primitize() method if it is defined, otherwise it will use a default implementation. The default implementation will loop over every field defined in the dataclass. Each field is serialized in a 2 step process:

  1. First call the modifier(object, field_value).
  2. The return of this is then passed into validator(object, return_of_the_modifier).

Example usage

Generating configuration files

Imagine we want to generate configuration files for several clusters, we have good sensible defaults but nothing is always exactly the same. In this example, we want each cluster configuration to be written in a json file.

from dataclasses import dataclass
from enum import Enum
import json

from primitize.core import primitize, primitized


class HostType(Enum):
    COMPUTE = "Compute"
    WEB = "Web"
    STORAGE = "Storage"


@dataclass
class User:
    username: str


@dataclass
class Cluster(Primizable):
    name: str
    size: int = primitized(validator=lambda v, o: v > 0)
    host_type: HostType = primitized(
        default=HostType.WEB, modifier=lambda v, o: v.value
    )
    admins: Set[User] = primitized(
        default_factory=set,
        modifier=lambda v, o: sorted(x),
        validator: lambda v, o: len(x) > 0,
    )

    def primitize(self):
        prim = super().primitize(self)
        payload = json.dumps(prim, sort_keys=True, indent=4)
        with (Path(".") / "output" / f"{self.name}.json").open("w") as fd:
            fd.write(payload)


clusters ={
    Cluster("A", 3, HostType.COMPUTE, {User("root")}),
    Cluster("B", 3, admins={User("root")}),
    Cluster("C", 3, HostType.STORAGE, {User("foo")}),
}

for cluster in clusters:
    primitize(cluster)

Upon executing this, you will find the following files under ./output/:

./output/A.json:

{
    "name": "A",
    "size": 3,
    "host_type": "Compute",
    "admins": [
        "root"
    ]
}

./output/B.json:

{
    "name": "A",
    "size": 3,
    "host_type": "Web",
    "admins": [
        "root"
    ]
}

./output/C.json:

{
    "name": "C",
    "size": 3,
    "host_type": "Storage",
    "admins": [
        "foo"
    ]
}

How is this different from X ?

Primitize Protobuf/Thrift TypedDict Jinja
Modifiers Yes No No No
Validators Type & Custom Type Type No
Language support Python only Multiple Python Custom DSL
Full Python API Yes No Yes No
Format flexibility Yes No No Yes
  • Modifiers: Primitize allows you to define functions that will massage the values prior to serialization, this allows your to rename, or reformat data to make the serialization easier. A typical usecase for this is to have the values of a type that is easy to manipulate and use a modifier to rewrite it to the what your end format expects
  • Validators: Primitize uses standard Python typing so mypy will ensure you have type checking. In Primitize you can also define functions that allows you to ensure that the value is correct upon serialization.
  • Language support: Primitize is a python library, as such it is primarily useful in Python.
  • Full Python API: Unlike a restrictive DSL, with Primitize it is really just your Python program running, so you can do whatever you want, we just help you convert dataclasses into primitive types so it is easier to write out.
  • Format flexibility: Primitize doesn't write data out for you so you can write it out as anything you want. Do you want json ? json.dumps(primitize(data)) Do you want yaml ? yaml.dumps(primitize(data)). It's that simple.

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

primitize-2020.5.21.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

primitize-2020.5.21-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file primitize-2020.5.21.tar.gz.

File metadata

  • Download URL: primitize-2020.5.21.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for primitize-2020.5.21.tar.gz
Algorithm Hash digest
SHA256 c27aeb600e5db40d3ff039736c47c08b633f3319c960edd2ac15e310b87b2fbc
MD5 298695a351278ad5c73d60842e6072b1
BLAKE2b-256 3dc826488295843ef5275d8c19fd2405657825357e570f4651a4d404635f7ac8

See more details on using hashes here.

File details

Details for the file primitize-2020.5.21-py3-none-any.whl.

File metadata

  • Download URL: primitize-2020.5.21-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0

File hashes

Hashes for primitize-2020.5.21-py3-none-any.whl
Algorithm Hash digest
SHA256 8fb87ae186de28a312e64b8068359c68a6bf6700a621bf2b70e7ba679bd07f20
MD5 685fbb25a0ef4ac4a5588046bd370273
BLAKE2b-256 68ce3fe04a706436da56ce21a4f4501762c9c127ac0288e8ddd97a4fa27b5c65

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