Skip to content

Parsing and results

Read a file into a Result: the document, the tables and the diagnostics. See Reading for the guide.

parse

parse(
    data: Buffer,
    spec: Optional[Spec] = None,
    delimiters: Optional[Delimiters] = None,
    mask: Optional[Mask] = None,
) -> Result

Parses a whole file: indexes every segment, runs the loop engine, the structure checker and the projector, and returns the document, the tables and every diagnostic. Without spec, the built-in spec of the version the file declares is used, else the default one. With mask, the patient data of the tables and diagnostics is hidden.

parse_file

parse_file(
    path: Union[str, "os.PathLike[str]"],
    spec: Optional[Spec] = None,
    delimiters: Optional[Delimiters] = None,
    mask: Optional[Mask] = None,
) -> Result

Reads the file at path in binary mode and parses it.

stream

stream(
    data: Buffer,
    spec: Optional[Spec] = None,
    by: str = "transaction",
    delimiters: Optional[Delimiters] = None,
    mask: Optional[Mask] = None,
) -> Stream

Walks a file and yields a batch each time an instance of loop by closes, and one more at the end when anything is left. Without spec, the built-in spec of the version the file declares is used, else the default one. With mask, the patient data of every batch is hidden.

Mask

Hides the patient data of a parse: the columns the spec flags as patient data are replaced by keyed tokens or shifted dates, and the diagnostics that quote them are masked the same way. The same key always gives the same token, so rows still join on it; without the key the originals cannot be recovered.

Stream

Iterates the batches of a file. Each step runs with the GIL released.

A stream must be advanced from one thread at a time: a next() that finds another thread inside a step raises a RuntimeError naming the stream; unlike a generator, it does not raise ValueError.

Batch

The tables and diagnostics of one closed loop instance (or of the end of the stream).

tables property

tables: Tables

The rows appended since the previous batch, by table.

diagnostics property

diagnostics: list[Diagnostic]

The diagnostics emitted since the previous batch, in stream order.

Result

What one parse produced.

document property

document: Document

The file, held losslessly.

tables property

tables: Tables

The projected tables, by name.

spec property

spec: Spec

The spec that projected the tables: the one given, or the built-in selected from the file's version.

diagnostics property

diagnostics: list[Diagnostic]

Every diagnostic, in stream order.

payer property

payer: dict[str, str | None] | None

The payer organization from the payer_* columns of payments, or None when the file has none.

payee property

payee: dict[str, str | None] | None

The payee organization from the payee_* columns of payments, or None when the file has none.

count_claims

count_claims() -> builtins.int

The rows of claims.

count_patients

count_patients() -> builtins.int

The distinct non-null values of claims.patient_id. Ids are text: 0123 and 123 are two patients, where edi-835-parser reads both as the number 123 and counts one.

sum_payments

sum_payments() -> decimal.Decimal

The sum of payments.total_payment_amount, as a Decimal at the column's scale.

Tables

The tables of one parse or one batch, by name. Shared, never copied.

spec property

spec: Spec

The spec that projected the tables, which write uses by default.

keys

keys() -> builtins.list[builtins.str]

The table names, in spec order.

render

render() -> builtins.str

Every table as text: title, header and one line per row.

to_polars

to_polars() -> builtins.dict[
    builtins.str, polars.DataFrame
]

Every table as a polars DataFrame, by name, in table order.

to_pandas

to_pandas() -> builtins.dict[
    builtins.str, pandas.DataFrame
]

Every table as a pandas DataFrame, by name, in table order.

Table

One projected table. Shares the tables it belongs to.

name property

name: str

The table name.

columns property

columns: list[str]

The column names, in order.

to_polars

to_polars() -> polars.DataFrame

The table as a polars DataFrame, through the Arrow capsule.

to_pandas

to_pandas() -> pandas.DataFrame

The table as a pandas DataFrame, through pyarrow.

render

render() -> builtins.str

The table as text: title, header and one line per row.

__arrow_c_stream__

__arrow_c_stream__(
    requested_schema: object | None = None,
) -> typing_extensions.CapsuleType

Exports the table as an Arrow stream of one record batch, sharing the column buffers. requested_schema is ignored, as the protocol allows: the table keeps its own types.

__arrow_c_array__

__arrow_c_array__(
    requested_schema: object | None = None,
) -> builtins.tuple[
    typing_extensions.CapsuleType,
    typing_extensions.CapsuleType,
]

Exports the table as one Arrow struct array and its schema, sharing the column buffers. requested_schema is ignored, as the protocol allows.

__arrow_c_schema__

__arrow_c_schema__() -> typing_extensions.CapsuleType

Exports the table's schema.

Document

The file, indexed into segments and held byte for byte.

delimiters property

delimiters: Delimiters

The delimiters the file was indexed with.

write

write() -> bytes

The file, byte for byte.

Segment

One segment of a document, read from the document on access.

index property

index: int

The 0-based position of the segment in the document.

id property

id: bytes

The segment identifier; empty for an empty frame.

elements property

elements: list[bytes | list[bytes]]

The elements in X12 order (elements[0] is XX01): bytes for a simple element, a list of bytes for a composite one.

raw property

raw: bytes

The exact bytes of the segment: leading trivia, body and terminator.

span property

span: tuple[int, int]

The segment's byte range in the input, (start, end) with end exclusive: leading trivia, body and terminator, as raw holds them.

Delimiters

The five delimiters of an interchange.

element property

element: bytes

The element separator, one byte.

component property

component: bytes

The component separator, one byte.

segment property

segment: bytes

The segment terminator, one byte.

repetition property

repetition: Optional[bytes]

The repetition separator, or None when the file has none.

release property

release: Optional[bytes]

The release character, or None when it is not in use.

__new__

__new__(
    element: bytes = b"*",
    component: bytes = b":",
    segment: bytes = b"~",
    repetition: Optional[bytes] = None,
    release: Optional[bytes] = None,
) -> Delimiters

Each delimiter is one byte. element, component and segment default to *, : and ~; repetition and release default to None (not in use). Raises ValueError for a value that is not exactly one byte.

ParseError

Bases: ValueError

Input that cannot be indexed: its delimiters cannot be read or it exceeds the size limit; the message says what was found and where.