https://github.com/lasuillard/pydantic-fixedwidth
Custom Pydantic models for serializing and deserializing fixed-width format data.
https://github.com/lasuillard/pydantic-fixedwidth
fixed-width pydantic python
Last synced: 7 months ago
JSON representation
Custom Pydantic models for serializing and deserializing fixed-width format data.
- Host: GitHub
- URL: https://github.com/lasuillard/pydantic-fixedwidth
- Owner: lasuillard
- License: mit
- Created: 2025-02-19T14:37:48.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-02T03:10:14.000Z (8 months ago)
- Last Synced: 2025-07-02T03:16:30.801Z (8 months ago)
- Topics: fixed-width, pydantic, python
- Language: Python
- Homepage:
- Size: 330 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pydantic-fixedwidth
[](https://opensource.org/licenses/MIT)
[](https://github.com/lasuillard/pydantic-fixedwidth/actions/workflows/ci.yaml)
[](https://codecov.io/gh/lasuillard/pydantic-fixedwidth)
[](https://pypi.org/project/pydantic-fixedwidth/)
Custom Pydantic models for serializing and deserializing fixed-width format data.
## 🚀 Quick Start
Install this package with pip:
```shell
$ pip install pydantic-fixedwidth
```
Usage example:
```python
from datetime import datetime, timezone
from pydantic_fixedwidth import Fixedwidth, Padding
from pydantic_fixedwidth import OrderedField as Field
tzinfo = timezone.utc
class SomeRequest(Fixedwidth):
string: str = Field(length=8)
hangul: str = Field(length=6)
number: int = Field(length=10, justify="right", fill_char=b"0")
# Just an padding field
p_: str = Padding(length=10)
# This field will be ignored in ser/de
ignore: str = Field(length=10, default="IGNORE", exclude=True)
ts: datetime = Field(
length=20,
to_str=lambda dt: dt.strftime("%Y%m%d%H%M%S%f"),
from_str=lambda s: datetime.strptime(s, "%Y%m%d%H%M%S%f").replace(tzinfo=tzinfo),
)
# Format model to bytes
some_request = SomeRequest(
string="