Skip to main content

Interface for handling custom formatted files

Project description

cfinterface

tests codecov PyPI version Python versions

Python framework for modeling custom file formats with declarative reading, formatting, and writing.

Key Features

  • Fields — typed value containers (LiteralField, IntegerField, FloatField, DatetimeField) with positional formatting for both text and binary files
  • Line — ordered collection of fields that models a single line or record
  • Register / RegisterFile — single-line blocks identified by a beginning pattern, ideal for files with many line types
  • Block / BlockFile — multi-line blocks with beginning/ending patterns, for complex structured files
  • Section / SectionFile — ordered, non-overlapping divisions of file content
  • TabularParser — schema-driven tabular parsing with fixed-width or delimiter-separated layouts
  • VersioningVERSIONS dict with resolve_version() and validate_version() for files that evolve over time
  • Binary supportStorageType.TEXT / StorageType.BINARY for seamless text or binary I/O
  • Batch readingread_many() for reading multiple files in one call

Install

cfinterface requires Python >= 3.10:

pip install cfinterface

Optional Dependencies

For pandas DataFrame integration (TabularParser.to_dataframe()):

pip install cfinterface[pandas]

Quick Start

Model a line-based file with Register and RegisterFile:

from datetime import datetime
from cfinterface import (
    DatetimeField, FloatField, LiteralField, Line, Register, RegisterFile,
)

class DataHigh(Register):
    IDENTIFIER = "DATA_HIGH"
    IDENTIFIER_DIGITS = 9
    LINE = Line(
        [
            LiteralField(size=6, starting_position=11),
            LiteralField(size=9, starting_position=19),
            DatetimeField(size=10, starting_position=30, format="%m/%d/%Y"),
            FloatField(size=6, starting_position=42, decimal_digits=2),
        ]
    )

class MyFile(RegisterFile):
    REGISTERS = [DataHigh]

file = MyFile.read("data.txt")
for reg in file.data.get_registers_of_type(DataHigh):
    print(reg.data)

Parse tabular data with TabularParser:

from cfinterface import IntegerField, FloatField, LiteralField
from cfinterface.components.tabular import ColumnDef, TabularParser

columns = [
    ColumnDef(name="City", field=LiteralField(size=12, starting_position=0)),
    ColumnDef(name="Pop", field=IntegerField(size=10, starting_position=12)),
    ColumnDef(name="Area", field=FloatField(size=8, starting_position=22, decimal_digits=1)),
]

parser = TabularParser(columns)
data = parser.parse_lines(["Springfield  1200000    115.4\n"])
# {"City": ["Springfield"], "Pop": [1200000], "Area": [115.4]}

Documentation

Guides, tutorials, and API reference: https://rjmalves.github.io/cfinterface

Changelog

See CHANGELOG.md for release history.

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

cfinterface-1.10.0.tar.gz (81.9 kB view details)

Uploaded Source

Built Distribution

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

cfinterface-1.10.0-py3-none-any.whl (42.1 kB view details)

Uploaded Python 3

File details

Details for the file cfinterface-1.10.0.tar.gz.

File metadata

  • Download URL: cfinterface-1.10.0.tar.gz
  • Upload date:
  • Size: 81.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cfinterface-1.10.0.tar.gz
Algorithm Hash digest
SHA256 4611642b0415910181356b38924c1e71d9d4048f9f7241969702948d8d7826b7
MD5 e5ad061fec978c184b1849387ab83ed6
BLAKE2b-256 a413fa6377c41d2cb38c706c7f0f496d932bffdab2baf70705c50840f9b82c1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfinterface-1.10.0.tar.gz:

Publisher: publish.yml on rjmalves/cfinterface

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

File details

Details for the file cfinterface-1.10.0-py3-none-any.whl.

File metadata

  • Download URL: cfinterface-1.10.0-py3-none-any.whl
  • Upload date:
  • Size: 42.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for cfinterface-1.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0865f6c5272b910de5813b92bb3431e8b68e952a15c4d8253a40e805df56048a
MD5 ec58471afe7d3be9a99fc9ac6fbd6a3f
BLAKE2b-256 ceaea9fbf40ba134a1f7f180b0f043472fc22ee09adbd558c6400d29cf6b6282

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfinterface-1.10.0-py3-none-any.whl:

Publisher: publish.yml on rjmalves/cfinterface

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