a treatment for PANDAS
Project description
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.Union
s
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.
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') as f:
csv.write(SampleDC, dcs, f)
tsv = Delimited(sep='\t', escape='\\')
with open('nts.tsv', 'w') as f:
tsv.write(SampleNT, dcs, f, header=False)
with open('dcs.csv', 'r') as f:
for r in csv.read(SampleDC, f):
print(r)
with open('nts.tsv', 'r') as f:
for r in tsv.read(SampleNT, f, header=False):
print(r)
View the full pdoc3-generated API documentation.
Install with:
pip install --index-url https://test.pypi.org/simple/ antibiotics
Or download directly from Test PyPI.
(c) 2020 dwt | terminus data science, LLC
available under the Apache License 2.0
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
Hashes for antibiotics-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6c73eb62cfe82f0aa574ec9e429d6de6760a7802d5567e1a5f09322226ed329d |
|
MD5 | fa261f032c378f4d443359a2ba2a4172 |
|
BLAKE2b-256 | c0d55c2e15384ee91295ce08576dcea1eb07fbde5bd7e4a65caf5888bddad433 |