Metadata-Version: 2.4
Name: csv-dataclass-gen
Version: 0.1.3
Summary: Generate Python dataclasses, loaders, from CSV/TSV files
Author-email: khwong-c <kin.hin.wong.c@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Kin Hin Wong
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/khwong-c/csv-dataclass-gen
Project-URL: Repository, https://github.com/khwong-c/csv-dataclass-gen
Project-URL: Issue Tracker, https://github.com/khwong-c/csv-dataclass-gen/issues
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Environment :: Console
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: Jinja2>=3.1.0
Requires-Dist: click>=8.1.0
Requires-Dist: click-log>=0.4.0
Dynamic: license-file

# CSV Dataclass Generator

Generate Python dataclasses and loader functions from CSV/TSV files.

## Features

- **Automatic Type Inference**: Detects `int`, `float`, and `str` types based on a sample of rows.
- **Dialect Detection**: Automatically identifies CSV delimiters (including TSV).
- **Name Sanitization**: Converts CSV column headers into valid Python identifiers.
- **Iterator Loading**: Generates a loading generator that yields dataclass instances, suitable for larger datasets.

## Installation

```bash
pip install csv-dataclass-gen
```

## Usage

### Command Line Interface (CLI)

The package provides a `csv-dataclass-gen` command.

```bash
# Generate code to stdout
csv-dataclass-gen data.csv

# Generate code and save to a directory
csv-dataclass-gen data.csv --output ./generated_models/

# Specify a custom class name and sample size for type inference
csv-dataclass-gen data.csv --name my_custom_data --sample-size 500
```

#### CLI Help Message:

```text
Usage: csv-dataclass-gen [OPTIONS] INPUT_FILE

  Generate dataclass and loader code from CSV files.

Options:
  -o, --output TEXT          Output directory for generated files. "-" outputs
                             the result to stdout.
  -s, --sample-size INTEGER  Number of rows to sample for type inference
  -n, --name TEXT            Alternative name for the generated name. Snake
                             case / spaced words is recommended.
  --help                     Show this message and exit.
```

#### Arguments:
- `INPUT_FILE`: Path to the CSV/TSV file (required).

### Example Generated Code

Given a CSV like `users.csv`:
```csv
id,user_name,score
1,alice,95.5
2,bob,88.0
```

The generator will produce:
```python
from dataclasses import dataclass
from pathlib import Path
from typing import Iterator
import csv

@dataclass
class Users:
    id: int  # Original: "id"
    user_name: str  # Original: "user_name"
    score: float  # Original: "score"

def load_users(csv_path: Path, max_rows: int | None = None, delimiter: str = ',') -> Iterator[Users]:
    # ... loading logic ...
    pass
```

## Development

### Dependencies

This project uses [uv](https://github.com/astral-sh/uv) for dependency management, but it can also be installed using standard tools.

```bash
uv sync --all-groups
```

### Running Tests

We use `pytest` for testing.

```bash
uv run pytest
```

Tests include:
- `tests/test_name_sanitizer.py`: Logic for sanitizing names into different formats.
- `tests/test_type_inferrer.py`: Logic for detecting data types.
- `tests/test_csv_analyzer.py`: Logic for CSV structure analysis.
- `tests/test_code_gen_e2e.py`: End-to-end tests that generate code and verify it by reconstructing the original CSV.

## License

MIT License – see the [LICENSE](LICENSE) file for details.
