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.

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:
    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,
    )



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:
    prim = primitize(cluster)
    payload = json.dumps(prim, sort_keys=True, indent=4)
    with (Path(".") / "output" / f"{cluster.name}.json").open("w") as fd:
        fd.write(payload)

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 ?

Why not use protobuf or thrift ?

Similar to protobuf and thrift, primitizer allows you to validate and serialize data. However you are in control of the serialization and the file format. Primitizer can easily generate pretty much any format (json, yaml, properties, xml, ini, etc.).

Why no use jsonnet ?

Primitizer can generate any file format, just not json

Why not jinja ?

Primitizer is full Python, so you have access to the entire API. As such you can do whatever you want with the objects to are manipulating. Primitizer also offers strong validation primitives to allow you to check the data for errors before writing it out.

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.4.28.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

primitize-2020.4.28.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: primitize-2020.4.28.1.tar.gz
  • Upload date:
  • Size: 4.1 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.4.28.1.tar.gz
Algorithm Hash digest
SHA256 c9e5c46ecce128caf561b9414f13811b55c3f1fbcda13b5cb382677a6901c9cc
MD5 75b3a523658e60a27caec4936c9fa5da
BLAKE2b-256 bb38267472e8b13e5dc5b022a1ea87148d10d09a1b205f276e684abb6744edb8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: primitize-2020.4.28.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 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.4.28.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7308ec15a976192a3aa2d2019234a43f13f23fd912aca0db4e88b076b8f88c26
MD5 3a6461cb78ccb6d87aefe94a8ac9b90c
BLAKE2b-256 e2ba8006ecc5f0f16c51de6729499284e61fd356ccd4d6b0b12d37e5e15d2b6b

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