COBOL Engine (dataspector.coboller)#

COBOL copybook parser, data decoder, and modern format converter.

Organized into four focused submodules:

References#

Copybook & Record AST#

COBOL copybook AST classes (Copybook, GroupItem, ElementaryItem).

class dataspector.coboller.copybook.ConditionName(name, raw_value, values=())[source]#

Bases: object

Level-88 condition-name attached to a COBOL data item.

Parameters:
name: str#
raw_value: str#
values: tuple[str, ...]#
class dataspector.coboller.copybook.ResolvedItem(name, field, offset, byte_length, is_redefines=False)[source]#

Bases: object

Flattened elementary item with exact byte offset and width in a record.

Parameters:
name: str#
field: ElementaryItem#
offset: int#
byte_length: int#
is_redefines: bool#
class dataspector.coboller.copybook.Copybook(copybook)[source]#

Bases: object

COBOL copybook parser, layout inspector, data reader, and format converter.

Parameters:
copybook

Path to a COBOL copybook file.

Parameters:

copybook (str | pathlib.Path)

copybook: Path#
raw: str#
records: list[GroupItem | ElementaryItem]#
classmethod from_file(path)[source]#

Construct a Copybook from a copybook file path.

Parameters:
path

Path to a COBOL copybook file.

Returns:
Copybook

Parsed copybook instance.

Parameters:

path (str | Path)

Return type:

Copybook

classmethod from_string(text, name='<string>')[source]#

Construct a Copybook directly from raw copybook source text.

Parameters:
text

Raw COBOL copybook source code.

name

Synthetic display name assigned to copybook.

Returns:
Copybook

Parsed copybook instance.

Parameters:
Return type:

Copybook

property columns: list[str]#

Alias for fields.

property decimal_comma: bool#

True if the copybook declares DECIMAL-POINT IS COMMA.

property exists: bool#

True if the underlying copybook file exists on disk.

property fields: list[str]#

Ordered elementary item names in primary record order.

property has_binary_fields: bool#

True if any item uses binary, packed, float, pointer, index, or wide storage.

property record_length: int#

Total physical byte length of a single record.

convert(datfile, output, chunksize=5000, encoding=None, float_format='ieee', byteorder='big', include_redefines=False, include_filler=False, line_ending='auto', dbcs_encoding='utf-16-be', decimal_comma=None)[source]#

Convert a fixed-width or compressed COBOL data file into a modern readable format.

Streams chunk-by-chunk via yield_data() for .csv, .tsv, and .jsonl outputs; also supports .json, .xlsx, and .parquet. Aligned with convert().

Parameters:
datfile

Path to the raw COBOL data file.

output

Destination file path whose extension selects the output format.

chunksize

Number of rows per streaming chunk.

encoding

Character encoding for DISPLAY items (e.g. 'cp500', 'ascii').

float_format

Floating-point representation for COMP-1/COMP-2 ('ieee' or 'ibm').

byteorder

Byte order for binary fields ('big' or 'little').

include_redefines

Include REDEFINES overlay columns in the converted output.

include_filler

Include FILLER columns (omitted by default for clean exports).

line_ending

Record delimiter mode ('auto', 'none', 'lf', or 'crlf').

dbcs_encoding

Character encoding for PIC G / DISPLAY-1 DBCS fields.

decimal_comma

Override copybook DECIMAL-POINT IS COMMA detection.

Returns:
pathlib.Path

Path to the written output file.

Parameters:
  • datfile (str | Path)

  • output (str | Path)

  • chunksize (int)

  • encoding (str | None)

  • float_format (Literal['ieee', 'ibm'])

  • byteorder (Literal['big', 'little'])

  • include_redefines (bool)

  • include_filler (bool)

  • line_ending (Literal['auto', 'none', 'lf', 'crlf'])

  • dbcs_encoding (str)

  • decimal_comma (bool | None)

Return type:

Path

find_record(name)[source]#

Find the first GroupItem or ElementaryItem matching name.

Parameters:
name

Exact COBOL item name to search for.

Returns:
GroupItem | ElementaryItem | None

First matching item in depth-first order, or None if absent.

Parameters:

name (str)

Return type:

GroupItem | ElementaryItem | None

layout(include_redefines=False, include_filler=True)[source]#

Compute flat byte-offset layout of all elementary items in the copybook.

Parameters:
include_redefines

Include items defined under REDEFINES overlays.

include_filler

Include FILLER items in the resolved layout.

Returns:
list[ResolvedItem]

Ordered elementary items with exact byte offsets and widths.

Parameters:
  • include_redefines (bool)

  • include_filler (bool)

Return type:

list[ResolvedItem]

pic_to_fwf_kwargs(convert_compressed=True)[source]#

Return keyword arguments for pandas.read_fwf() on text copybooks.

Parameters:
convert_compressed

Attach byte-level decoders for binary/compressed fields when True; keep raw representations when False.

Returns:
dict[str, Any]

Dictionary containing colspecs, names, and converters suitable for unpacking into pandas.read_fwf().

Parameters:

convert_compressed (bool)

Return type:

dict[str, Any]

summary(include_redefines=False, include_filler=True)[source]#

Return a tabular overview of every elementary item’s byte layout and COBOL type.

Parameters:
include_redefines

Include REDEFINES overlay items in the summary table.

include_filler

Include FILLER items in the summary table.

Returns:
pandas.DataFrame

Table with columns name, level, pic, usage, kind, offset, byte_length, signed, decimals, and is_redefines.

Parameters:
  • include_redefines (bool)

  • include_filler (bool)

Return type:

DataFrame

to_dataframe(datfile, nrows=None, chunksize=500, encoding=None, convert_compressed=True, drop_converters=False, float_format='ieee', byteorder='big', as_decimal=False, include_redefines=False, include_filler=True, line_ending='auto', dbcs_encoding='utf-16-be', decimal_comma=None)[source]#

Decode datfile into a single materialized pandas.DataFrame.

Reads internally via chunked streaming (yield_data()) and stops early once nrows records have been collected. Aligned with to_dataframe().

Parameters:
datfile

Path to the COBOL data file.

nrows

Optional maximum number of records to return (None reads all).

chunksize

Number of rows decoded per internal streaming chunk.

encoding

Character encoding for DISPLAY items (auto-detected if None).

convert_compressed

Decode binary/compressed items; retain hex representation if False.

drop_converters

Keep all items as raw decoded strings / hex without numeric casting.

float_format

Floating-point representation for COMP-1/COMP-2 ('ieee' or 'ibm').

byteorder

Byte order for binary fields ('big' or 'little').

as_decimal

Preserve exact precision with decimal.Decimal for fixed-point numbers.

include_redefines

Include REDEFINES overlay columns.

include_filler

Include FILLER columns.

line_ending

Record delimiter mode ('auto', 'none', 'lf', or 'crlf').

dbcs_encoding

Character encoding for PIC G / DISPLAY-1 DBCS items.

decimal_comma

Override copybook DECIMAL-POINT IS COMMA detection.

Returns:
pandas.DataFrame

Decoded records (empty DataFrame with copybook columns if no rows).

Parameters:
  • datfile (str | Path)

  • nrows (int | None)

  • chunksize (int)

  • encoding (str | None)

  • convert_compressed (bool)

  • drop_converters (bool)

  • float_format (Literal['ieee', 'ibm'])

  • byteorder (Literal['big', 'little'])

  • as_decimal (bool)

  • include_redefines (bool)

  • include_filler (bool)

  • line_ending (Literal['auto', 'none', 'lf', 'crlf'])

  • dbcs_encoding (str)

  • decimal_comma (bool | None)

Return type:

DataFrame

Notes

Because the return value is a single in-memory DataFrame, pass nrows for quick previews of very large mainframe files, or iterate directly over yield_data() when streaming files larger than memory.

to_pandera_schema(include_redefines=False, include_filler=True, enforce_conditions=False, coerce=False)[source]#

Generate a pandera.pandas.DataFrameSchema from this copybook.

Parameters:
include_redefines

Include REDEFINES overlay items as schema columns.

include_filler

Include FILLER items as schema columns.

enforce_conditions

Add isin checks derived from level-88 condition-name values.

coerce

Whether the returned schema coerces column dtypes on validation.

Returns:
pandera.pandas.DataFrameSchema

Schema encoding column dtypes, length/range bounds, and optional level-88 domain checks.

Parameters:
  • include_redefines (bool)

  • include_filler (bool)

  • enforce_conditions (bool)

  • coerce (bool)

Return type:

DataFrameSchema

yield_data(datfile, chunksize=500, encoding=None, convert_compressed=True, drop_converters=False, float_format='ieee', byteorder='big', as_decimal=False, include_redefines=False, include_filler=True, line_ending='auto', dbcs_encoding='utf-16-be', decimal_comma=None)[source]#

Stream records from datfile as pandas.DataFrame chunks.

Parameters:
datfile

Path to the raw COBOL data file.

chunksize

Maximum number of decoded records per yielded chunk.

encoding

Character encoding for DISPLAY items (auto-detected if None).

convert_compressed

Decode binary/compressed items; retain hex representation if False.

drop_converters

Keep all items as raw decoded strings / hex without numeric casting.

float_format

Floating-point representation for COMP-1/COMP-2 ('ieee' or 'ibm').

byteorder

Byte order for binary fields ('big' or 'little').

as_decimal

Preserve exact precision with decimal.Decimal for fixed-point numbers.

include_redefines

Include REDEFINES overlay columns.

include_filler

Include FILLER columns.

line_ending

Record delimiter mode ('auto', 'none', 'lf', or 'crlf').

dbcs_encoding

Character encoding for PIC G / DISPLAY-1 DBCS items.

decimal_comma

Override copybook DECIMAL-POINT IS COMMA detection.

Yields:
pandas.DataFrame

Decoded chunk of up to chunksize records.

Parameters:
  • datfile (str | Path)

  • chunksize (int)

  • encoding (str | None)

  • convert_compressed (bool)

  • drop_converters (bool)

  • float_format (Literal['ieee', 'ibm'])

  • byteorder (Literal['big', 'little'])

  • as_decimal (bool)

  • include_redefines (bool)

  • include_filler (bool)

  • line_ending (Literal['auto', 'none', 'lf', 'crlf'])

  • dbcs_encoding (str)

  • decimal_comma (bool | None)

Return type:

Iterator[DataFrame]

class dataspector.coboller.copybook.GroupItem(description)[source]#

Bases: object

Hierarchical COBOL group item containing subordinate items.

Parameters:

description (dict[str, Any])

description: dict[str, Any]#
redefinedby: list[str]#
records: list[GroupItem | ElementaryItem]#
conditions: list[ConditionName]#
property byte_length: int#

Physical byte width of a single occurrence of this group item (including wider redefines).

property level: int#

COBOL level number.

property name: str#

Group item name.

property occurs: int#

Repetition count from OCCURS (1 when non-repeating).

property redefines: str | None#

Name of the sibling item this group redefines, or None.

property usage: Usage | None#

Optional group-level USAGE inherited by subordinate elementary items.

append(level, record)[source]#

Insert record at the deepest nested subgroup whose level is < level.

Parameters:
level

COBOL level number of the item being inserted.

record

Group or elementary item to append.

Parameters:
Return type:

None

find_record(name)[source]#

Depth-first search for the first subordinate item named name.

Parameters:
name

Exact COBOL item name to search for.

Returns:
GroupItem | ElementaryItem | None

First matching subordinate item, or None if not found.

Parameters:

name (str)

Return type:

GroupItem | ElementaryItem | None

pic_to_fwf_kwargs(convert_compressed=True)[source]#

Return flattened pandas.read_fwf() column descriptors for this group item.

Parameters:
convert_compressed

Attach byte-level decoders for binary/compressed items when True.

Returns:
list[dict[str, Any]]

Column descriptor dictionaries expanded across any OCCURS repetitions.

Parameters:

convert_compressed (bool)

Return type:

list[dict[str, Any]]

class dataspector.coboller.copybook.ElementaryItem(description)[source]#

Bases: object

COBOL elementary data item carrying a PICTURE or self-sized USAGE.

Parameters:

description (dict[str, Any])

description: dict[str, Any]#
redefinedby: list[str]#
conditions: list[ConditionName]#
classmethod from_clause(statement)[source]#

Construct an ElementaryItem from a single COBOL declaration line.

Parameters:
statement

Single COBOL data description clause (with or without trailing period).

Returns:
ElementaryItem

Parsed elementary item.

Parameters:

statement (str)

Return type:

ElementaryItem

property blank_when_zero: bool#

True if BLANK WHEN ZERO was declared.

property byte_length: int#

Physical byte width of a single occurrence of this elementary item.

property decimals: int#

Number of implied fractional decimal digits.

property digits: int#

Total count of 9 digit positions in the PIC clause.

property expanded_pic: str#

Fully expanded uppercase PIC string.

property exponent: int#

Base-10 scaling exponent from V and P symbols.

property is_binary: bool#

True if the item uses binary, packed, float, pointer, index, or wide storage.

property is_filler: bool#

True if this item is a FILLER item.

property kind: DataCategory#

COBOL DataCategory of this elementary item.

property level: int#

COBOL level number.

property name: str#

Item name ('FILLER' if unnamed).

property occurs: int#

Repetition count from OCCURS (1 when non-repeating).

property redefines: str | None#

Name of the sibling item this item redefines, or None.

property sign_mode: SignMode#

Sign encoding mode for USAGE DISPLAY signed numeric items.

property signed: bool#

True if this elementary item is signed.

property usage: Usage#

Normalized Usage storage representation.

decode_bytes(raw, encoding='ascii', float_format='ieee', byteorder='big', as_decimal=False, convert_compressed=True, drop_converters=False, dbcs_encoding='utf-16-be', decimal_comma=False)[source]#

Decode raw bytes for this elementary item into a Python value.

Parameters:
raw

Byte slice corresponding to this elementary item.

encoding

Character encoding for DISPLAY items (e.g. 'ascii', 'cp500').

float_format

Floating-point representation for COMP-1/COMP-2 ('ieee' or 'ibm').

byteorder

Byte order for binary and UTF-16 items ('big' or 'little').

as_decimal

Return decimal.Decimal instead of float/int for fixed-point numbers.

convert_compressed

Decode binary/packed items when True; return hex strings when False.

drop_converters

Return raw decoded text or hex without numeric conversion.

dbcs_encoding

Character encoding for PIC G / DISPLAY-1 DBCS items.

decimal_comma

Interpret commas as decimal separators per DECIMAL-POINT IS COMMA.

Returns:
Any

Decoded Python scalar (str, int, float, Decimal, or None).

Parameters:
  • raw (bytes)

  • encoding (str)

  • float_format (Literal['ieee', 'ibm'])

  • byteorder (Literal['big', 'little'])

  • as_decimal (bool)

  • convert_compressed (bool)

  • drop_converters (bool)

  • dbcs_encoding (str)

  • decimal_comma (bool)

Return type:

Any

pic_to_fwf_kwargs(convert_compressed=True)[source]#

Convert this item into column descriptor dicts for pandas.read_fwf().

Parameters:
convert_compressed

Attach a byte-level converter for binary/compressed storage when True.

Returns:
list[dict[str, Any]]

One descriptor per OCCURS repetition containing name, pic, and an optional converter callable.

Parameters:

convert_compressed (bool)

Return type:

list[dict[str, Any]]

dataspector.coboller.copybook.parse_copybook(rawtext)[source]#

Parse COBOL copybook source text or a file path into a record hierarchy.

Parameters:
rawtext

Raw copybook text or path to a copybook file.

Returns:
list[GroupItem | ElementaryItem]

Top-level record definitions parsed from the copybook.

Parameters:

rawtext (str | Path)

Return type:

list[GroupItem | ElementaryItem]

dataspector.coboller.copybook.CopyBook#

alias of Copybook

dataspector.coboller.copybook.Group#

alias of GroupItem

dataspector.coboller.copybook.Field#

alias of ElementaryItem

dataspector.coboller.copybook.Condition#

alias of ConditionName

dataspector.coboller.copybook.ResolvedField#

alias of ResolvedItem

Binary & Text Codecs#

Pure binary and text codecs for COBOL data representations (alphabetical).

dataspector.coboller.codecs.bit_pack(bits)[source]#

Pack a '0'/'1' bit string into COBOL USAGE BIT bytes (zero-padded right).

Parameters:
bits

Non-empty string of '0' and '1' characters.

Returns:
bytes

MSB-first packed bytes right-padded with zero bits to a byte boundary.

Parameters:

bits (str)

Return type:

bytes

dataspector.coboller.codecs.bit_unpack(raw, n_bits)[source]#

Decode a COBOL-2002 PIC 1(n) USAGE BIT byte sequence into a bit string.

Parameters:
raw

Raw packed bytes.

n_bits

Number of leading bits to extract.

Returns:
str

Bit string of length n_bits containing '0' and '1' characters.

Parameters:
Return type:

str

dataspector.coboller.codecs.comp3_pack(value, digits, decimals=0, exponent=None, signed=True)[source]#

Pack a numeric value into COBOL COMP-3 (BCD packed decimal) bytes.

Parameters:
value

Numeric value to pack.

digits

Total number of decimal digit positions in the PIC clause.

decimals

Number of implied fractional decimal digits (ignored when exponent is set).

exponent

Optional explicit base-10 scaling exponent (e.g. -2 for V99, 3 for P(3)).

signed

Encode sign nibble 0xC/0xD when True, or unsigned 0xF when False.

Returns:
bytes

Packed BCD byte sequence of length (digits // 2) + 1.

Parameters:
Return type:

bytes

dataspector.coboller.codecs.comp3_unpack(raw, decimals=0, exponent=None, as_decimal=False)[source]#

Unpack a COBOL COMP-3 / PACKED-DECIMAL byte sequence.

Parameters:
raw

Packed BCD bytes whose final low nibble carries the sign.

decimals

Number of implied fractional decimal digits (ignored when exponent is set).

exponent

Optional explicit base-10 scaling exponent.

as_decimal

Return a decimal.Decimal when True instead of int/float.

Returns:
int | float | Decimal

Decoded numeric value.

Parameters:
Return type:

int | float | Decimal

dataspector.coboller.codecs.comp3decompress(x, decimals=0, as_decimal=False)[source]#

Backward-compatible alias for comp3_unpack().

Parameters:
x

Packed BCD bytes.

decimals

Number of implied fractional decimal digits.

as_decimal

Return a decimal.Decimal when True.

Returns:
int | float | Decimal

Decoded numeric value.

Parameters:
Return type:

int | float | Decimal

dataspector.coboller.codecs.comp6_pack(value, digits, decimals=0, exponent=None)[source]#

Pack a non-negative value into COBOL COMP-6 (unsigned packed decimal) bytes.

Parameters:
value

Non-negative numeric value to pack.

digits

Total number of decimal digit positions in the PIC clause.

decimals

Number of implied fractional decimal digits (ignored when exponent is set).

exponent

Optional explicit base-10 scaling exponent.

Returns:
bytes

Signless packed BCD byte sequence of length (digits + 1) // 2.

Parameters:
Return type:

bytes

dataspector.coboller.codecs.comp6_unpack(raw, decimals=0, exponent=None, as_decimal=False)[source]#

Unpack a COBOL COMP-6 (unsigned packed decimal, no sign nibble) byte sequence.

Parameters:
raw

Signless packed BCD bytes (two decimal digits per byte).

decimals

Number of implied fractional decimal digits (ignored when exponent is set).

exponent

Optional explicit base-10 scaling exponent.

as_decimal

Return a decimal.Decimal when True.

Returns:
int | float | Decimal

Decoded non-negative numeric value.

Parameters:
Return type:

int | float | Decimal

dataspector.coboller.codecs.comp_float_pack(value, size=4, float_format='ieee', byteorder='big')[source]#

Pack a Python float into COMP-1 (4-byte) or COMP-2 (8-byte) bytes.

Parameters:
value

Floating-point number to encode.

size

Target storage width in bytes (4 for COMP-1, 8 for COMP-2).

float_format

Binary floating-point format ('ieee' or 'ibm' HFP).

byteorder

Byte order for IEEE 754 encoding ('big' or 'little').

Returns:
bytes

Encoded 4-byte or 8-byte floating-point sequence.

Parameters:
Return type:

bytes

dataspector.coboller.codecs.comp_float_unpack(raw, float_format='ieee', byteorder='big')[source]#

Unpack a COBOL COMP-1 (4-byte) or COMP-2 (8-byte) floating-point value.

Parameters:
raw

4-byte or 8-byte floating-point representation.

float_format

Binary floating-point format ('ieee' or 'ibm' HFP).

byteorder

Byte order for IEEE 754 decoding ('big' or 'little').

Returns:
float

Decoded Python floating-point number.

Parameters:
Return type:

float

dataspector.coboller.codecs.comp_pack(value, byte_length, signed=True, byteorder='big', exponent=0)[source]#

Pack a numeric value into COBOL binary COMP bytes.

Parameters:
value

Numeric value to pack.

byte_length

Exact width in bytes of the target binary integer field.

signed

Use two’s-complement signed encoding when True, unsigned when False.

byteorder

Byte order ('big' or 'little').

exponent

Base-10 scaling exponent applied before integer packing.

Returns:
bytes

Binary integer byte sequence of length byte_length.

Parameters:
Return type:

bytes

dataspector.coboller.codecs.comp_unpack(raw, signed=True, byteorder='big', exponent=0, as_decimal=False)[source]#

Unpack a COBOL binary integer (COMP, COMP-4, COMP-5, COMP-X, BINARY-*).

Parameters:
raw

Raw binary integer bytes.

signed

Interpret as two’s-complement signed when True, unsigned when False.

byteorder

Byte order ('big' or 'little').

exponent

Base-10 scaling exponent applied to the unpacked integer.

as_decimal

Return a decimal.Decimal when True.

Returns:
int | float | Decimal

Decoded numeric value.

Parameters:
Return type:

int | float | Decimal

dataspector.coboller.codecs.expand_pic(pic, max_repeat=100000)[source]#

Expand repeat counts in a COBOL PIC string (e.g. 'S9(5)V9(2)' -> 'S99999V99').

Parameters:
pic

Raw COBOL PICTURE clause string.

max_repeat

Upper bound on any single repeat factor or expanded string length (guards against memory exhaustion on malformed copybooks).

Returns:
str

Uppercase expanded PICTURE string with repeat factors unfolded.

Parameters:
Return type:

str

dataspector.coboller.codecs.external_float_unpack(raw, expanded_pic='', encoding='ascii', decimal_comma=False)[source]#

Decode a COBOL external floating-point USAGE DISPLAY item.

Parameters:
raw

Encoded character bytes representing the external floating-point value.

expanded_pic

Expanded PICTURE clause used to infer implied V decimal placement.

encoding

Character encoding of raw (e.g. 'ascii', 'cp500').

decimal_comma

Interpret commas as decimal points when True.

Returns:
float

Decoded floating-point value.

Parameters:
Return type:

float

dataspector.coboller.codecs.float_to_ibm_hfp(value, size=4)[source]#

Encode a Python float into 4-byte or 8-byte IBM Hexadecimal Floating-Point.

Parameters:
value

Floating-point value to encode.

size

Target HFP storage size in bytes (4 short or 8 long).

Returns:
bytes

Big-endian IBM S/360 / z/Architecture HFP bytes.

Parameters:
Return type:

bytes

dataspector.coboller.codecs.ibm_hfp_to_float(raw)[source]#

Decode a 32-bit or 64-bit IBM Hexadecimal Floating-Point (HFP) byte sequence.

Parameters:
raw

4-byte (short) or 8-byte (long) big-endian IBM HFP bytes.

Returns:
float

Decoded Python floating-point value.

Parameters:

raw (bytes)

Return type:

float

dataspector.coboller.codecs.parse_numeric_edited(text, blank_when_zero=False, decimal_comma=False)[source]#

Parse a COBOL numeric-edited display string (e.g. '  $1,234.50-') to float.

Parameters:
text

Formatted numeric-edited string containing currency, suppression, or sign tokens.

blank_when_zero

Return 0.0 instead of None for all-blank fields when True.

decimal_comma

Interpret commas as decimal points and periods as thousands separators when True.

Returns:
float | None

Parsed numeric value, or None for blank fields when blank_when_zero is False.

Parameters:
  • text (str)

  • blank_when_zero (bool)

  • decimal_comma (bool)

Return type:

float | None

dataspector.coboller.codecs.predict_encoding(file_path, n_lines=20)[source]#

Predict a file’s character encoding via chardet, falling back to 'ascii'.

Parameters:
file_path

Path to the file to inspect.

n_lines

Number of leading lines to sample for detection.

Returns:
str

Detected encoding name, or 'ascii' if detection is inconclusive.

Parameters:
Return type:

str

dataspector.coboller.codecs.zoned_pack(value, digits, encoding='ascii', signed=False, sign_mode=SignMode.TRAILING_OVERPUNCH, exponent=0)[source]#

Pack a numeric value into COBOL USAGE DISPLAY zoned decimal bytes.

Parameters:
value

Numeric value to encode.

digits

Total number of digit positions in the PIC clause.

encoding

Target character encoding (EBCDIC codepage or ASCII-compatible encoding).

signed

Encode sign information when True.

sign_mode

Sign placement/encoding convention (leading/trailing, overpunch/separate).

exponent

Base-10 scaling exponent applied before encoding digits.

Returns:
bytes

Encoded zoned decimal byte sequence.

Parameters:
Return type:

bytes

dataspector.coboller.codecs.zoned_unpack(raw, encoding='ascii', signed=False, sign_mode=SignMode.TRAILING_OVERPUNCH, exponent=0, as_decimal=False)[source]#

Unpack a COBOL USAGE DISPLAY zoned decimal byte sequence.

Parameters:
raw

Encoded zoned decimal bytes.

encoding

Character encoding of raw (EBCDIC or ASCII-compatible).

signed

Decode sign overpunch or separate sign character when True.

sign_mode

Sign placement/encoding convention (leading/trailing, overpunch/separate).

exponent

Base-10 scaling exponent applied to the decoded integer.

as_decimal

Return a decimal.Decimal when True.

Returns:
int | float | Decimal

Decoded numeric value.

Parameters:
Return type:

int | float | Decimal

dataspector.coboller.codecs.compute_decimal_exponent(expanded_pic)[source]#

Return base-10 exponent implied by V and P symbols in a PIC string.

Parameters:
expanded_pic

Expanded uppercase COBOL PICTURE string.

Returns:
int

Base-10 exponent (negative for fractional digits, positive for trailing P).

Parameters:

expanded_pic (str)

Return type:

int

dataspector.coboller.codecs.is_ebcdic(encoding)[source]#

Return True if encoding refers to an EBCDIC codepage.

Parameters:
encoding

Character encoding name (e.g. 'cp500', 'cp037', 'ascii').

Returns:
bool

True for recognized EBCDIC codepages, False otherwise.

Parameters:

encoding (str)

Return type:

bool

dataspector.coboller.codecs.scale_integer(value, exponent, as_decimal=False)[source]#

Scale an integer by 10 ** exponent.

Parameters:
value

Unscaled integer value.

exponent

Base-10 exponent to apply.

as_decimal

Return an exact decimal.Decimal when True.

Returns:
int | float | Decimal

Scaled numeric value.

Parameters:
Return type:

int | float | Decimal

Copybook Statement Parser#

Stateless COBOL copybook line cleaning and clause parsing helpers.

dataspector.coboller.parser.clean_cobol_line(line)[source]#

Strip fixed-format COBOL sequence/comment/ID areas and return code text.

Parameters:
line

Raw single line from a COBOL copybook.

Returns:
str

Cleaned statement fragment, or "" for blank/comment lines.

Parameters:

line (str)

Return type:

str

dataspector.coboller.parser.extract_statements(rawtext)[source]#

Join multi-line COBOL statements and split on terminating periods.

Parameters:
rawtext

Full COBOL copybook text.

Returns:
list[str]

Cleaned period-stripped COBOL statements.

Parameters:

rawtext (str)

Return type:

list[str]

dataspector.coboller.parser.parse_condition_values(raw_value)[source]#

Parse discrete literal values from a level-88 VALUE clause.

Parameters:
raw_value

Raw substring following VALUE/VALUES in a level-88 declaration.

Returns:
tuple[str, …]

Tuple of normalized discrete literal values (empty for THRU ranges).

Parameters:

raw_value (str)

Return type:

tuple[str, …]

dataspector.coboller.parser.parse_statement(stmt)[source]#

Parse a single period-stripped COBOL data description statement.

Parameters:
stmt

Cleaned COBOL statement without its trailing period.

Returns:
dict[str, Any] | None

Dictionary of extracted clauses (level, name, pic, usage, occurs, redefines, etc.), or None if stmt is not a data description entry.

Parameters:

stmt (str)

Return type:

dict[str, Any] | None

Constants & Enums#

Enums, compiled regular expressions, and lookup tables for dataspector.coboller.

class dataspector.coboller.constants.Usage(*values)[source]#

COBOL USAGE storage representations.

DISPLAY = 'DISPLAY'#
DISPLAY_1 = 'DISPLAY-1'#
COMP = 'COMP'#
COMP_1 = 'COMP-1'#
COMP_2 = 'COMP-2'#
COMP_3 = 'COMP-3'#
COMP_5 = 'COMP-5'#
COMP_6 = 'COMP-6'#
COMP_X = 'COMP-X'#
BINARY_CHAR = 'BINARY-CHAR'#
BINARY_SHORT = 'BINARY-SHORT'#
BINARY_LONG = 'BINARY-LONG'#
BINARY_DOUBLE = 'BINARY-DOUBLE'#
FLOAT_SHORT = 'FLOAT-SHORT'#
FLOAT_LONG = 'FLOAT-LONG'#
NATIONAL = 'NATIONAL'#
UTF_8 = 'UTF-8'#
BIT = 'BIT'#
POINTER = 'POINTER'#
PROCEDURE_POINTER = 'PROCEDURE-POINTER'#
FUNCTION_POINTER = 'FUNCTION-POINTER'#
INDEX = 'INDEX'#
class dataspector.coboller.constants.SignMode(*values)[source]#

COBOL sign encoding modes for USAGE DISPLAY signed numeric items.

NONE = 'NONE'#
TRAILING_OVERPUNCH = 'TRAILING'#
LEADING_OVERPUNCH = 'LEADING'#
TRAILING_SEPARATE = 'TRAILING SEPARATE'#
LEADING_SEPARATE = 'LEADING SEPARATE'#
class dataspector.coboller.constants.DataCategory(*values)[source]#

COBOL data category of an elementary item.

ALPHABETIC = 'ALPHABETIC'#
ALPHANUMERIC = 'ALPHANUMERIC'#
NATIONAL = 'NATIONAL'#
UTF8 = 'UTF8'#
DBCS = 'DBCS'#
BIT = 'BIT'#
INTEGER = 'INTEGER'#
DECIMAL = 'DECIMAL'#
NUMERIC_EDITED = 'NUMERIC_EDITED'#
EXTERNAL_FLOAT = 'EXTERNAL_FLOAT'#
BINARY = 'BINARY'#
PACKED_DECIMAL = 'PACKED_DECIMAL'#
PACKED_UNSIGNED = 'PACKED_UNSIGNED'#
FLOAT = 'FLOAT'#
POINTER = 'POINTER'#
INDEX = 'INDEX'#
dataspector.coboller.constants.FieldKind#

alias of DataCategory