Skip to main content

Custom Pydantic models for serializing and deserializing fixed-width data.

Project description

pydantic-fixedwidth

License: MIT codecov PyPI - Version

Custom Pydantic models for serializing and deserializing fixed-width data.

✨ Features

This small Python package provides tools for working with fixed-width data and for serializing and deserializing it using Pydantic models.

🚀 Quick Start

Install this package with pip:

$ pip install pydantic-fixedwidth

Usage example:

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="<DFG&",
    hangul="한글",
    number=381,
    ts=datetime(2024, 1, 23, 14, 11, 20, 124277, tzinfo=tzinfo),
)
b = some_request.format_bytes()

assert len(b) == 54
assert b == b"<DFG&   \xed\x95\x9c\xea\xb8\x800000000381          20240123141120124277"

# Parse bytes into model
parsed_request = SomeRequest.parse_bytes(b)

assert parsed_request == some_request

💖 Contributing

Please see CONTRIBUTING.md for information on contributing to this project.

📜 License

This project is licensed under the MIT License.

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

pydantic_fixedwidth-0.3.1.tar.gz (62.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_fixedwidth-0.3.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_fixedwidth-0.3.1.tar.gz.

File metadata

  • Download URL: pydantic_fixedwidth-0.3.1.tar.gz
  • Upload date:
  • Size: 62.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pydantic_fixedwidth-0.3.1.tar.gz
Algorithm Hash digest
SHA256 69fc903ff44b96833dd1bca003b2b2148a6b0a5b605bfc89681cadf6369b3acd
MD5 ebc237bde28791971776aa8f5685f8ad
BLAKE2b-256 56f8a23e7445d8db75eb7495be72be174672e0430ea58c86a8cb98f6a20934b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_fixedwidth-0.3.1.tar.gz:

Publisher: release.yaml on lasuillard-s/pydantic-fixedwidth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pydantic_fixedwidth-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_fixedwidth-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 03757a45046fb35dbd57f899fe818975cb4c7cddcaee47339af2644f4eee5b2e
MD5 1952b49f48ed5a0dd9948f16bf464ce7
BLAKE2b-256 c4b5f10894746797bc98d6ea478063cd2125b15b4e9c3f6ed75ae85aa4743d00

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_fixedwidth-0.3.1-py3-none-any.whl:

Publisher: release.yaml on lasuillard-s/pydantic-fixedwidth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page