Skip to main content

Custom Pydantic models for parsing and serializing fixed-width format of data.

Project description

pydantic-fixedwidth

License: MIT codecov PyPI - Version

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

🚀 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

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.0.tar.gz (57.6 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.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_fixedwidth-0.3.0.tar.gz
Algorithm Hash digest
SHA256 dc77eda038160a1ad6ad8bcc0836b39cad1098d0e1eb7421f6b179b4a819bbd6
MD5 bf1b41fbb7c0405588af3054f0aec061
BLAKE2b-256 4847f50dc34aac50846d197cc70fd5f438211a4ebf0bcd99e2118a4c5eefeff3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_fixedwidth-0.3.0.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.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_fixedwidth-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3be7fe2a1de302600b7c569d2acd22a6137e398f1d0b2afa4b50a7c849b609d0
MD5 48a0b8ed529fc8b5495d25f31926f731
BLAKE2b-256 e2743a2862fb0db504ef6eefe31fbf2e233d41a44e7b7c747f87eac5b3fbb5f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pydantic_fixedwidth-0.3.0-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