pidibble.baseparsers module

class pidibble.baseparsers.ListParser(d=',')[source]

Bases: object

A simple parser for lists of strings, with a customizable delimiter.

parse(string)[source]

Parse a string into a list of strings, using the specified delimiter. If no delimiter is specified, it splits on whitespace.

Parameters:

string (str) – The string to parse.

Returns:

A list of strings parsed from the input string.

Return type:

list

class pidibble.baseparsers.StringParser(fmtdict, typemap, allowed={})[source]

Bases: object

A parser for fixed-width strings, with a customizable field map.

Parameters:
  • fmtdict (dict) – A dictionary mapping field names to tuples of (type, byte_range).

  • typemap (dict) – A dictionary mapping type names to Python types.

  • allowed (dict, optional) – A dictionary mapping field values to allowed values, for validation.

parse(record)[source]

Parse a fixed-width string record into a dictionary of fields.

Parameters:

record (str) – The fixed-width string record to parse.

Returns:

A dictionary of fields parsed from the input record.

Return type:

dict

report_field_error(record, k)[source]

Report an error in parsing a specific field from a fixed-width string record.

Parameters:
  • record (str) – The fixed-width string record that caused the error.

  • k (str) – The field name that caused the error.

report_record_error(record, byte_range=[])[source]

Report an error in parsing a fixed-width string record.

Parameters:
  • record (str) – The fixed-width string record that caused the error.

  • byte_range (list, optional) – A list of byte ranges to highlight in the error message. If empty, the entire record is reported.

pidibble.baseparsers.list_parse(obj, d)[source]

A factory function to create a ListParser with a specific delimiter.

Parameters:
  • obj (type) – The class to instantiate (should be ListParser).

  • d (str or None) – The delimiter to use for parsing. If None, it will split on whitespace.

Returns:

A function that takes a string and returns a list of parsed strings.

Return type:

function

pidibble.baseparsers.safe_float(x)[source]

Convert a string to a float, returning 0.0 if the string is ‘nan’.

pidibble.baseparsers.str2int_sig(arg: str)[source]

Convert a string to an integer, returning -1 if the string is not numeric. If the string starts with a ‘-’, it is returned as an integer.