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.2.0.tar.gz (42.3 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.2.0-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_fixedwidth-0.2.0.tar.gz
  • Upload date:
  • Size: 42.3 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.2.0.tar.gz
Algorithm Hash digest
SHA256 9cac5be1ab391b0515ce0719ddf27cda792448d10d9622e3a8ec7c1c59d8c007
MD5 b3b315638e11c0fa8a3a8aaa1130bebb
BLAKE2b-256 8817eed9aae1ab118eec507631c39bd2171bef6eb2d5a372e3a6530fbf2fa5d9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pydantic_fixedwidth-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80553284ad65e9de5bcc9ce15d25837495fb59a3ff9fdb93e8b23921e6d1de63
MD5 55b3741aa9d2360c098958a24ce9cf3c
BLAKE2b-256 8ac44550e9bae35397fc59b4d5e7465637d55ed79e8af30de67afeb4be3c72a4

See more details on using hashes here.

Provenance

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