Skip to main content

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

Project description

pydantic-fixedwidth

License: MIT CI 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.1.3.tar.gz (37.9 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.1.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pydantic_fixedwidth-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6df6ca3ca98d92decc66cd764eca23bf6658a58382a70fd3771be2121c18e612
MD5 4bcfba10988b597ef3cadfd3e9130938
BLAKE2b-256 f582f26e348ce2bbe2391fad1d92233097f59f522bb1468578895a713e2655f4

See more details on using hashes here.

Provenance

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

Publisher: release.yaml on lasuillard/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.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_fixedwidth-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1697b8fb6f0efc750d8294faa6a668c0d3b2da3ea0dfa06c969a186f1f5c70df
MD5 9b3dabe75125cd11c7753655c24db555
BLAKE2b-256 b06ec732e19ba8f8e83aad46bb6144f9512a3088f2e3fe89d1110a1f108f7f38

See more details on using hashes here.

Provenance

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

Publisher: release.yaml on lasuillard/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