Skip to main content

Dataclass IO

Project description

dataclass_io

CI Python Versions MyPy Checked Ruff

Read and write dataclasses.

dataclass_io provides similar functionality to the standard library's csv.DictReader and csv.DictWriter, and adds type safety.

Installation

dataclass_io may be installed via pip:

pip install dataclass_io

Quickstart

Reading

from dataclasses import dataclass
from dataclass_io import DataclassReader


@dataclass
class MyData:
    foo: int
    bar: str


with open("test.tsv", "w") as testfile:
    testfile.write("foo\tbar\n")
    testfile.write("1\tabc\n")
    testfile.write("2\tdef\n")

with DataclassReader.open("test.tsv", MyData) as reader:
    for record in reader:
        print(record.foo)

Writing

from dataclasses import dataclass
from dataclass_io import DataclassWriter


@dataclass
class MyData:
    foo: int
    bar: str


with DataclassWriter.open("test.tsv", MyData) as writer:
    for i in range(3):
        record = MyData(foo=i, bar="something")
        writer.write(record)

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

dataclass_io-0.2.0.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

dataclass_io-0.2.0-py3-none-any.whl (10.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page