Skip to main content

The easiest way to parse your CSV files

Project description

Classy CSV

Classy CSV is the easiest way to parse your CSV files; you are just one dataclass away from your data.

The main use case for Classy CSV is to serialize and deserialize tabular data produced internally by applications you are in control of. By creating a dataclass that describes the structure of your CSV data, you can seamlessly convert between CSV files and Python objects.

Data will be read in a struct-of-arrays or array-of-structs manner depending on your data representation. See the API section for more details.

Goals and Non-Goals

Goals

  • Ease of Use: The primary goal of Classy CSV is to provide a simple and intuitive way to parse and serialize CSV files using Python's built-in dataclass mechanism.
  • Type Safety and Compatibility: Classy CSV is designed to play nicely with type hints, linters, and language server protocols such as pyright and mypy. This ensures that the code you write is type-safe and can be checked with standard Python tooling.
  • Zero Dependencies: Zero worries about dependency issues.

Non-Goals

  • Performance: Classy CSV is not optimized for high performance. It is intended to be used with small CSV files.
  • Error Handling: Classy CSV is not designed to deal gracefully with malformed CSV files. It assumes that the input CSV files are well-formed.

Installation

You can install Class CSV using pip:

pip install classy-csv

, alternatively you can just copy classy_csv/classy_csv.py into your project and start using it now!

API

Classy CSV exposes the following functions:

  • CSVLine: A base dataclass for representing a single line in a CSV file. Each attribute of the dataclass corresponds to a column in the CSV file. Optional parsers and serializers can be defined for each attribute.
  • CSVColumns: A base dataclass for representing columns in a CSV file as lists. Each attribute of the dataclass corresponds to a column in the CSV file and must be a list. Optional parsers and serializers can be defined for each attribute. Loaded data using a column format based on this class will be returned in a struct-of-arrays style.
  • csvfield: A helper function to add parser and serializer configurations to fields in CSVLine and CSVColumns. It works similarly to dataclass.field but provides additional parameters for parsing and serializing CSV data. It also accepts all the parameters that field accepts.
  • dump: Serialize data to a CSV formatted file. The data can be a list of CSVLine objects or a single CSVColumns object.
  • load: Load data from a CSV formatted file into a list of CSVLine objects or a single CSVColumns object, depending on the provided dataclass type.
  • dumps and loads: Same as dump and load but instead of writing into a file handle or reading from it they produce and load from string representations.

Example

from pathlib import Path
import dataclasses as dc
from classy_csv import CSVLine, CSVColumns, csvfield, dump, load, loads, dumps

@dc.dataclass
class CustomerData(CSVLine):
    name: str
    age: int = csvfield(parser=int)
    height_m: float = csvfield(parser=float, serializer=lambda x: f"{x:.2f}")
    weight_kg: float = csvfield(parser=float)

rows = [
    CustomerData(name="Jane", age=42, height_m=1.65, weight_kg=58),
    CustomerData(name="Joe", age=36, height_m=1.75, weight_kg=75),
]

# It's important to open files with `newline=""`!
with Path("./example.csv").open(mode="w", newline="") as csvfile:
    dump(csvfile, rows)

with Path("./example.csv").open(mode="r", newline="") as csvfile:
    loaded_rows = load(csvfile, CustomerData)

print(loaded_rows)
# Output: [
#  CustomerData(name='Jane', age=42, height_m=1.65, weight_kg=58),
#  CustomerData(name='Joe', age=36, height_m=1.75, weight_kg=75),
# ]

More examples can be found under the examples/ folder.

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

classy_csv-0.1.2.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

classy_csv-0.1.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file classy_csv-0.1.2.tar.gz.

File metadata

  • Download URL: classy_csv-0.1.2.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.8.0-31-generic

File hashes

Hashes for classy_csv-0.1.2.tar.gz
Algorithm Hash digest
SHA256 8bcd8fab52490cfd0c68d796ad1ffb3985e100710bf69acaa4d15ba8dd2b6d27
MD5 f7b1b6801c9d8b9cefa2a64193bdf653
BLAKE2b-256 04bf0232b7fddd23748e1ce41d74b9982df44e65a789de3703038e1ba220a162

See more details on using hashes here.

File details

Details for the file classy_csv-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: classy_csv-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.8.0-31-generic

File hashes

Hashes for classy_csv-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8a71bf9ac5055480b30785c8e4ee78180066076567fea1ef1cb96c6312bd03e0
MD5 91735fdb3597bc5059967500fb905c5a
BLAKE2b-256 a8c04cb6b7e758f82e3b625b1ae0c2cb8be38df8c4feee52db7c90fc100ddab4

See more details on using hashes here.

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