Python library and CLI tool for converting data to different Python data classes
Project description
data2model
Python library and CLI tool for generating different Python data classes from data.
Supported data formats:
- CSV
Supported data classes:
Requirements
- Python 3.8+
Installation
pip install data2model
Usage
As library
import asyncio
import pathlib
from data_to_model import ModelGenerator
files = [
{"input": pathlib.Path("example.csv"), "output": pathlib.Path("example.py")},
]
async def model_generator(input_file: pathlib.Path, output_file: pathlib.Path):
mg = ModelGenerator(input_file)
model = await mg.get_model()
await model.save(output_file)
async def main():
tasks = [model_generator(i["input"], i["output"]) for i in files]
await asyncio.gather(*tasks)
if __name__ == "__main__":
asyncio.run(main())
As CLI
$ cat data.csv
col1,col2
1,2
,2.0
$ data2model -if data.csv -of data.py
$ cat data.py
# This file was generated by data_to_model.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
@dataclass
class Data:
col1: Optional[int]
col2: Union[float, int]
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "Data":
col1 = data["col1"]
col2 = data["col2"]
return cls(col1=col1, col2=col2)
def to_dict(self) -> Dict[str, Any]:
return {"col1": self.col1, "col2": self.col2}
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
data2model-0.2.0b0.tar.gz
(9.5 kB
view hashes)
Built Distribution
Close
Hashes for data2model-0.2.0b0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 416f08d144d11a5765e59ab25c734c247c4edadc46081bc5840574afc146835b |
|
MD5 | 75ffd894cba4c0e8e06b56eff110e3b1 |
|
BLAKE2b-256 | 840a84b8a6bb311d8929dda3c53c2b892ad680c491ef07ea329261f871344689 |