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.1.tar.gz (82.0 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.1-py3-none-any.whl (42.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cfinterface-1.10.1.tar.gz
  • Upload date:
  • Size: 82.0 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.1.tar.gz
Algorithm Hash digest
SHA256 18cbefd0e3cde531a76040cc8aae99584a03bfccb066bec489d55a18e0a09944
MD5 28127d12ab85fb7984c488f706738763
BLAKE2b-256 fbb9425e3eb6bf06778e55771a057db1f6f696ea53cae826ad1851062cc96c19

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfinterface-1.10.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: cfinterface-1.10.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9de23ac94ac2f8ba56d215d773df32ce429ad2db2260894356913e961cc7ef82
MD5 34112d97cfb98ac89ea967bb74d57042
BLAKE2b-256 084170bd41267a4bc8f26bd96faea977f853f3f7b72776a44503042a412e7943

See more details on using hashes here.

Provenance

The following attestation bundles were made for cfinterface-1.10.1-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