Skip to main content

antibiotics

NamedTuple / dataclasses <-> delimited text

"The best treatment for acute episodes of PANDAS is to treat the strep infection causing the symptoms, if it is still present, with antibiotics."
-- National Institute of Mental Health

antibiotics is a minimalist type-driven serialization/deserialization library inspired by Serde and cassava.

It uses type annotations to automatically read and write NamedTuple or @dataclass objects to or from delimited text files.

Out of the box, it only knows about Python scalar types and typing.Unions of them (including typing.Optional), but an extension mechanism for arbitrary type-directed serialization and deserialization is provided through the type_serde_ext argument to the Delimited constructor - see examples/advanced.py.

For Union types, serialization is driven by the runtime type, and deserialization is attempted in the order of declaration of the Union arguments - except that NoneType is tried first if present, to preserve the expected behavior when deserializing null/missing values of types whose deserializers do not throw when receiving '' as an argument.

A type ExternalName is also provided which may be used with typing.Annotated to specify the name which should be used for a member when serializing or deserializing (e.g. to match CSV headers).

Please note that as with the built-in csv module, file-like objects used with this library should be opened with newline=''.

Basic example

from antibiotics import Delimited
from dataclasses import dataclass
from typing import NamedTuple, Optional

@dataclass
class SampleDC():
    w: Optional[float]
    x: int
    y: bool
    z: str

class SampleNT(NamedTuple):
    w: Optional[float]
    x: int
    y: bool
    z: str

if __name__ == '__main__':
    dcs = list()
    nts = list()
    for i in range(10):
        even = i % 2 == 0
        dcs.append(SampleDC(
            i * 3.5 if even else None,
            i,
            not even,
            f'_",\t_{i}'
        ))
        nts.append(SampleNT(
            i * 3.5 if even else None,
            i,
            not even,
            f'_",\t_{i}'
        ))

    csv = Delimited()
    with open('dcs.csv', 'w', newline='') as f:
        csv.write(SampleDC, dcs, f)

    tsv = Delimited(sep='\t', escape='\\', newline='\n')
    with open('nts.tsv', 'w', newline='') as f:
        tsv.write(SampleNT, dcs, f, header=False)

    with open('dcs.csv', 'r', newline='') as f:
        for r in csv.read(SampleDC, f):
            print(r)

    with open('nts.tsv', 'r', newline='') as f:
        for r in tsv.read(SampleNT, f, header=False):
            print(r)

Example with custom external names

from antibiotics import Delimited, ExternalName
from dataclasses import dataclass
from typing import Annotated, Optional

@dataclass
class SampleDC():
    w: Annotated[Optional[float], ExternalName('BigW')]
    x: Annotated[int, ExternalName('Fancy X')]
    y: bool
    z: str

if __name__ == '__main__':
    dcs = list()
    for i in range(10):
        even = i % 2 == 0
        dcs.append(SampleDC(
            i * 3.5 if even else None,
            i,
            not even,
            f'_",\t_{i}'
        ))

    csv = Delimited()
    with open('dcs.csv', 'w', newline='') as f:
        csv.write(SampleDC, dcs, f)

    with open('dcs.csv', 'r', newline='') as f:
        for dc in csv.read(SampleDC, f):
            print(dc)

Documentation

Documentation strings and type annotations are provided for public types and functions. We recommend viewing "nice" documentation pages using pdoc; e.g. in the same environment as the antibiotics package is installed, install pdoc with pip install pdoc, then run pdoc antibiotics.


Install with:

pip install antibiotics

Or download directly from PyPI.

(c) 2023 dwt | terminus data science, LLC

available under the Apache License 2.0

Metadata

Release files for antibiotics 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for antibiotics 0.4
File Size Uploaded
antibiotics-0.4.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for antibiotics 0.4
File Interpreter ABI Platform
antibiotics-0.4-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / antibiotics-0.4.tar.gz

Download URL antibiotics-0.4.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3fb26b4e55ec016670b5529dd57792c2a899bda43570ce3fc4e0beebb2f0e856
BLAKE2b-256 checksum
How to use checksums
fd74815ccc554817ee7aecf7d3dfcff435523fc552ad8e3f881c47c96654eea8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / antibiotics-0.4-py3-none-any.whl

Download URL antibiotics-0.4-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8903c3329fa89327db2584e34a05a7b1b29fc0df5a1966fa2faf106c98f16e82
BLAKE2b-256 checksum
How to use checksums
40ac244d95842c7fca534be2ad86424c80d9b1ea005866ff1796a5bc775ec956
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.4 This release

2 release files

0.3

2 release files

0.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page