pyx12.x12context

Parse a ANSI X12 data file.

Maintain context state Start saving context and segments Interface to read and alter segments

TODO: Attach errors to returned dicts

Classes

X12DataNode

Capture the segment data and X12 definition for a loop subtree

X12LoopDataNode

Capture the X12 definition for a loop subtree

X12SegmentDataNode

Capture the segment data and X12 definition

X12ContextReader

Read an X12 input stream

Module Contents

class pyx12.x12context.X12DataNode(x12_node, seg_data, ntype='seg')[source]

Capture the segment data and X12 definition for a loop subtree Alter relational data Iterate over contents

Parameters:
x12_map_node: Any[source]
type: str | None[source]
seg_data: pyx12.segment.Segment | None[source]
parent: X12DataNode | None[source]
children: list[X12DataNode][source]
errors: list[Any][source]
seg_count: int | None[source]
cur_line_number: int | None[source]
delete()[source]

Delete this node. Mark type as deleted.

Return type:

None

abstractmethod iterate_segments()[source]

Iterate over this node and children, return any segments found

Return type:

collections.abc.Iterator[dict[str, Any]]

abstractmethod iterate_loop_segments()[source]

Iterate over this node and children, return loop start and loop end and any segments found

Return type:

collections.abc.Iterator[dict[str, Any]]

abstractmethod get_value(x12_path)[source]
Returns:

the element value at the relative X12 path

Return type:

string

Parameters:

x12_path (str)

abstractmethod set_value(x12_path, val)[source]

Set the value of simple element at the first found segment at the given path :param x12_path: An X12 path :type x12_path: string :param val: The new element value :type val: string

Parameters:
Return type:

None

exists(x12_path_str)[source]

Does at least one child at the x12 path exist? :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: True if found :rtype: boolean

Parameters:

x12_path_str (str)

Return type:

bool

select(x12_path_str)[source]

Get a slice of sub-nodes at the relative X12 path. Note: All interaction/modification with a X12DataNode tree (having a loop root) is done in place. :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: Iterator on the matching sub-nodes, relative to the instance. :rtype: L{node<x12context.X12DataNode>}

Parameters:

x12_path_str (str)

Return type:

collections.abc.Iterator[X12DataNode]

first(x12_path_str)[source]

Get the first sub-node matching the relative X12 path. Note: All interaction/modification with a X12DataNode tree (having a loop root) is done in place. :param x12_path_str: Relative X12 path - ie 2400/2430 :type x12_path_str: string :return: The matching sub-node, relative to the instance. :rtype: L{node<x12context.X12DataNode>}

Parameters:

x12_path_str (str)

Return type:

X12DataNode | None

count(x12_path_str)[source]

Get a count of sub-nodes at the relative X12 path. :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: Count of matching sub-nodes :rtype: int

Parameters:

x12_path_str (str)

Return type:

int

abstractmethod get_first_matching_segment(x12_path_str)[source]

Get first found Segment at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None.

Parameters:

x12_path_str (string) – Relative X12 Path

Returns:

First matching data segment

Return type:

L{node<segment.Segment>}

Raises:

X12PathError – On blank or invalid path

copy()[source]
Return type:

X12DataNode

property id: str | None[source]

x12 node id :rtype: string

Type:

return

Return type:

str | None

property cur_path: str[source]

x12 node path :rtype: string

Type:

return

Return type:

str

class pyx12.x12context.X12LoopDataNode(x12_node, end_loops=None, parent=None)[source]

Bases: X12DataNode

Capture the X12 definition for a loop subtree Alter relational data Iterate over contents

Construct an X12LoopDataNode

Parameters:
end_loops: list[Any][source]
x12_map_node[source]
type = 'loop'[source]
seg_data = None[source]
parent = None[source]
children = [][source]
errors = [][source]
delete()[source]

Delete this node. Mark type as deleted.

Return type:

None

get_value(x12_path_str)[source]

Returns the element value at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None. If multiple values exist, this function returns the first.

Parameters:

x12_path_str (string) – Relative X12 Path

Returns:

the element value at the relative X12 path

Return type:

string

Raises:

X12PathError – On blank or invalid path

set_value(x12_path_str, val)[source]

Set the value of simple element at the first found segment at the given path :param x12_path_str: Relative X12 Path :type x12_path_str: string :param val: The new element value :type val: string

Parameters:
  • x12_path_str (str)

  • val (str)

Return type:

None

iterate_segments()[source]

Iterate over this node and children

Return type:

collections.abc.Iterator[dict[str, Any]]

iterate_loop_segments()[source]

Iterate over this node and children, return loop start and loop end

Return type:

collections.abc.Iterator[dict[str, Any]]

add_segment(seg_data)[source]

Add the segment to this loop node iif the segment is the anchor for a child loop, also adds the loop

Parameters:

seg_data (L{node<segment.Segment>} or string) – Segment data

Returns:

New segment, or None if failed

Return type:

L{node<x12context.X12SegmentDataNode>}

Raises:

pyx12.errors.X12PathError – If invalid segment

TODO: Check counts?

add_loop(seg_data)[source]

Add a new loop in the correct location :param seg_data: Segment data :type seg_data: L{node<segment.Segment>} or string :return: New loop_data_node, or None if failed :rtype: L{node<x12context.X12LoopDataNode>}

Parameters:

seg_data (pyx12.segment.Segment | str)

Return type:

X12LoopDataNode

add_node(data_node)[source]

Add a X12DataNode instance The x12_map_node of the given data_node must be a direct child of this object’s x12_map_node :param data_node: The child loop node to add :type data_node : L{node<x12context.X12DataNode>} :raises errors.X12PathError: On blank or invalid path

Parameters:

data_node (X12DataNode)

Return type:

None

delete_segment(seg_data)[source]
Delete the given segment from this loop node
  • Do not delete the first segment in a loop

  • Does not descend into child loops

  • Only delete the first found matching segment

Parameters:

seg_data (L{node<segment.Segment>} or string) – Segment data

Returns:

True if found and deleted, else False

Return type:

Boolean

TODO: Check counts?

delete_node(x12_path_str)[source]

Delete the first node at the given relative path. If the path is not a valid relative path, return False If multiple values exist, this function deletes the first.

Returns:

True if found and deleted, else False

Return type:

Boolean

Raises:

X12PathError – On blank or invalid path

Parameters:

x12_path_str (str)

TODO: Check counts?

get_first_matching_segment(x12_path_str)[source]

Get first found Segment at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None.

Parameters:

x12_path_str (string) – Relative X12 Path

Returns:

First matching data segment

Return type:

L{node<segment.Segment>}

Raises:

X12PathError – On blank or invalid path

property seg_count: int | None[source]
Return type:

int | None

property cur_line_number: int | None[source]
Return type:

int | None

class pyx12.x12context.X12SegmentDataNode(x12_node, seg_data, parent=None, start_loops=None, end_loops=None)[source]

Bases: X12DataNode

Capture the segment data and X12 definition Alter relational data Iterate over contents

Parameters:
start_loops: list[Any][source]
end_loops: list[Any][source]
err_isa: list[Any][source]
err_gs: list[Any][source]
err_st: list[Any][source]
err_seg: list[Any][source]
err_ele: list[Any][source]
x12_map_node[source]
type = 'seg'[source]
seg_data[source]
parent = None[source]
errors = [][source]
seg_count = None[source]
cur_line_number = None[source]
handle_errh_errors(errh)[source]

Attach validation errors to segment node

TODO: move errors to parent loops if necessary

Parameters:

errh (Any)

Return type:

None

delete()[source]

Delete this node. Mark type as deleted.

Return type:

None

get_value(x12_path_str)[source]

Get the value of the first found element at the given path :param x12_path_str: Relative X12 Path :type x12_path_str: string :return: the element value at the relative X12 path :rtype: string

Parameters:

x12_path_str (str)

Return type:

str | None

set_value(x12_path_str, val)[source]

Set the value of simple element at the first found segment at the given path :param x12_path_str: Relative X12 Path :type x12_path_str: string :param val: The new element value :type val: string

Parameters:
  • x12_path_str (str)

  • val (str)

Return type:

None

get_first_matching_segment(x12_path_str)[source]

Get first found Segment at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None.

Parameters:

x12_path_str (string) – Relative X12 Path

Returns:

First matching data segment

Return type:

L{node<segment.Segment>}

Raises:

X12PathError – On blank or invalid path

iterate_segments()[source]

Iterate on this node, return the segment

Return type:

collections.abc.Iterator[dict[str, Any]]

iterate_loop_segments()[source]

Iterate over this node and children, return loop start and loop end and any segments found

Return type:

collections.abc.Iterator[dict[str, Any]]

select(x12_path_str)[source]

Segment nodes have no sub-nodes so return None :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: Iterator on the matching sub-nodes, relative to the instance. :rtype: L{node<x12context.X12DataNode>}

Parameters:

x12_path_str (str)

Return type:

collections.abc.Iterator[X12DataNode]

property err_ct: int[source]

Count of errors for this segment :rtype: int

Type:

return

Return type:

int

class pyx12.x12context.X12ContextReader(param, errh, src_file_obj, xslt_files=None, map_path=None)[source]

Read an X12 input stream Keep context when needed

Parameters:
  • param (Any) – pyx12.param instance

  • errh (Any) – Error Handler object

  • src_file_obj (string) – Source document

  • xslt_files (Any)

  • map_path (str | None)

Return type:

boolean

param: Any[source]
map_path: str | None[source]
errh: Any[source]
icvn: str | None[source]
fic: str | None[source]
vriic: str | None[source]
tspc: str | None[source]
src: pyx12.x12file.X12Reader[source]
map_file: str | None[source]
control_map: Any[source]
map_index_if: pyx12.map_index.map_index[source]
x12_map_node: Any[source]
walker: pyx12.map_walker.walk_tree[source]
close()[source]

Close the underlying X12Reader. Idempotent.

Return type:

None

iter_segments(loop_id=None)[source]

Simple segment or tree iterator :return: X12 Data Node - simple segment or tree :rtype: L{node<x12context.X12DataNode>}

Parameters:

loop_id (str | None)

Return type:

collections.abc.Iterator[X12DataNode]

register_error_callback(callback, err_type)[source]

Future: Callbacks for X12 validation errors

Parameters:
  • callback (Any)

  • err_type (str)

Return type:

None

property seg_term: str | None[source]

Current X12 segment terminator :rtype: string

Type:

return

Return type:

str | None

property ele_term: str | None[source]

Current X12 element terminator :rtype: string

Type:

return

Return type:

str | None

property subele_term: str | None[source]

Current X12 sub-element terminator :rtype: string

Type:

return

Return type:

str | None

property cur_seg_count: int[source]
Return type:

int

property get_cur_line: int[source]
Return type:

int