Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lucidfrontier45/pydanticio
Tiny IO utility library for Python with Pydantic inspired by Serdeio of Rust
https://github.com/lucidfrontier45/pydanticio
file file-reading file-writing input-output io pydantic python utility
Last synced: about 1 month ago
JSON representation
Tiny IO utility library for Python with Pydantic inspired by Serdeio of Rust
- Host: GitHub
- URL: https://github.com/lucidfrontier45/pydanticio
- Owner: lucidfrontier45
- License: mit
- Created: 2024-06-28T10:07:51.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-06-30T11:35:45.000Z (6 months ago)
- Last Synced: 2024-10-07T03:18:13.927Z (3 months ago)
- Topics: file, file-reading, file-writing, input-output, io, pydantic, python, utility
- Language: Python
- Homepage:
- Size: 26.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PydanticIO
PydanticIO (pronounce pidantisio) is a tiny file IO utility library for Python powered by Pydantic.
This library is a port of the Rust library [SerdeIO](https://github.com/lucidfrontier45/serdeio)# Install
```sh
# standard distribution
pip install pydanticio# with YAML backend
pip install pydanticio[yaml]
```# Supported Formats
- CSV by stdlib `csv` module
- JSON by stdlib `json` module
- JSON Lines by stdlib `json` module
- YAML by `pyyaml` library, it is an optional feature and you need enable it manually at when you install it.# Usage
- `read_record_from_reader` is used to read a type `T` which is a subclass of `pydantic.BaseModel` from `TextIO`. Data format must be specified by `DataFormat` literals.
- `read_records_from_reader` always tries to deserialize the data as `list[T]`.
- `read_record_from_file` and `read_records_from_file` accepts a `Path`. Data format is automatically determined by file extension.
- `write_*` functions follow the same rules as `read_*`.Note that some data format like CSV and JSON Lines support only reading records `list[T]`.
# Examples
```py
from pydantic import BaseModel
from pydanticio import read_records_from_file, write_records_to_fileclass User(BaseModel):
name: str
age: intusers = read_records_from_file("users.csv", User)
write_records_to_file("users.json", users)
```