A python library for serializing dataclasses to bytes and back.
Project description
Databrief
databrief is a Python library for serializing dataclasses to bytes and deserializing bytes back to dataclasses.
Features
- Compact serialization
- Supports the following field types:
- Primitive types:
int,float,bool,str - Collections:
list,set,tuple,dict - Nested dataclasses
- Primitive types:
Installation
pip install databrief
Usage
Dumping a Dataclass to Bytes
To serialize a dataclass instance to bytes, use the dump function:
from databrief import dump
from dataclasses import dataclass
@dataclass
class TestData:
a: int
b: float
c: bool
data = TestData(a=42, b=3.14, c=True)
serialized = dump(data)
print(serialized)
Loading Bytes to a Dataclass
To deserialize bytes back to a dataclass instance, use the load function:
from databrief import load
deserialized = load(serialized, TestData)
print(deserialized)
Examples
Here is a complete example:
from dataclasses import dataclass
from databrief import dump, load
@dataclass
class Example:
a: int
b: float
c: bool
d: bool
e: bool
f: bool
g: bool
h: bool
i: bool
j: bool
k: bool
l: float
m: int
n: int
o: bool
example = Example(1, 2.0, True, False, True, False, True, False, True, True, False, 87543653.35197087, 1351346, -46583278, True)
serialized = dump(example)
deserialized = load(serialized, Example)
print(deserialized)
Here is another example:
from dataclasses import dataclass
from databrief import dump, load
@dataclass
class InnerData:
x: int
y: str
@dataclass
class OuterData:
a: int
b: InnerData
c: bool
example = OuterData(a=1, b=InnerData(x=42, y="hello"), c=True)
serialized = dump(example)
deserialized = load(serialized, OuterData)
print(deserialized)
Contributing
Contributions are welcome! Please read our Contributing Guide for more information.
License
Databrief is distributed under the MIT license.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file databrief-0.0.2.tar.gz.
File metadata
- Download URL: databrief-0.0.2.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
550f2e1d3217caa3e0a4dd86e25434b23ba342f69e82c0887af268d6adb8d69c
|
|
| MD5 |
ed1553a437f0fcdc41ca0ee6b8f3bb54
|
|
| BLAKE2b-256 |
658dbf51c1fe02ccfad785839d583398a75535473bf3e6d46ddf4475d46f7043
|
File details
Details for the file databrief-0.0.2-py3-none-any.whl.
File metadata
- Download URL: databrief-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ba428c014ea30791de640f196f95d90dbba8d4995f26f58a779f30defb629d9
|
|
| MD5 |
aa1188f7a6bc52ad81c74a94e636b1df
|
|
| BLAKE2b-256 |
e59cd66c5504991b777880bc12b3ef85d14b3407c75defdc6e2777b0d616d152
|