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 x: x > 0)
host_type: HostType = primitized(default=HostType.WEB, modifier=lambda x: x.value)
admins: Set[User] = primitized(
default_factory=set,
modifier=lambda x: sorted(x),
validator: lambda x: len(x) > 0,
)
def prepare_primitization(self: "Cluster") -> None:
"""If this method exists, it is called as the first step of primitization.
It is typical to perform x-fields updates here."""
pass
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
Release history Release notifications | RSS feed
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
File details
Details for the file primitize-2020.4.28.tar.gz
.
File metadata
- Download URL: primitize-2020.4.28.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a81438a1861d8cfdff0d1d3a7e55429639e369d41bb960ae9938569c9c2819a1 |
|
MD5 | 10d3a5360ff666869dd5524cb3bd5643 |
|
BLAKE2b-256 | c0c2e8e92cd3782d81e650a87a652aae679d17c78747c506153cc97d2552a151 |
File details
Details for the file primitize-2020.4.28-py3-none-any.whl
.
File metadata
- Download URL: primitize-2020.4.28-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/42.0.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4809071cc49a6ad1501707f9f8699cef6f08125e09e4942668200c2c4b46c2d9 |
|
MD5 | 08fced4fbaeff6ac0a2a69178575bcc8 |
|
BLAKE2b-256 | fcd9f8b203416c482751821a15174c617b9db91160603e6adb35f51b4d31ca52 |