Skip to main content

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

Installation

pip install databrief

Usage

Dumping a Dataclass to Bytes

To serialize a dataclass instance to bytes with specific integer and floating point sizes, 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, 4, 8) # int32 and fp64
print(serialized)

Loading Bytes to a Dataclass

To deserialize bytes back to a dataclass instance with given integer and floating point sizes, use the load function:

from databrief import load

deserialized = load(serialized, TestData, 4, 8) # int32 and fp64
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, 4, 8)
deserialized = load(serialized, Example, 4, 8)
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, 4, 8)
deserialized = load(serialized, OuterData, 4, 8)
print(deserialized)

Contributing

Code Style and Static Type Checking

Run style checker.

flake8 ./databrief

Run static type checker.

mypy --strict databrief

Unit and Documentation Testing

Run unit tests.

python -m unittest

License

Databrief is distributed under the MIT license.

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

databrief-0.1.1.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

databrief-0.1.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file databrief-0.1.1.tar.gz.

File metadata

  • Download URL: databrief-0.1.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for databrief-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9558e788afa88f18365f506b8da1a945cf38969f9fbfe6ecbe0709f7a2222dbf
MD5 92c0e21e2d4a8878881f07cb1a7752a5
BLAKE2b-256 bab4b344d8e4d1161db7a15d4a11ff8f207fa93b12fb14125dc59f179ef7c648

See more details on using hashes here.

File details

Details for the file databrief-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: databrief-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for databrief-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1948359c0bf7a5d48c1b9112a1c59e30873cfdc329bbe9b7b2287a9b26edc6e
MD5 58413915bb3b8ce87da282d635c77079
BLAKE2b-256 55854e98d683fe6ac516a0a52ea5cd1ce90cc48f9d3c848bbf412f8bf6c31067

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