Parser

hl7apy.parser.parse_message(message, validation_level=None, find_groups=True, message_profile=None, report_file=None, force_validation=False)

Parse the given ER7-encoded message and return an instance of Message.

Parameters:
  • message (str) – the ER7-encoded message to be parsed
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
  • find_groups (bool) – if True, automatically assign the segments found to the appropriate Groups instances. If False, the segments found are assigned as children of the Message instance
Returns:

an instance of Message

>>> message = "MSH|^~\&|GHH_ADT||||20080115153000||OML^O33^OML_O33|0123456789|P|2.5||||AL\rPID|1||"     "566-554-3423^^^GHH^MR||EVERYMAN^ADAM^A|||M|||2222 HOME STREET^^ANN ARBOR^MI^^USA||555-555-2004|||M\r"
>>> m = parse_message(message)
>>> print(m)
<Message OML_O33>
>>> print(m.msh.sending_application.to_er7())
GHH_ADT
>>> print(m.children)
[<Segment MSH>, <Group OML_O33_PATIENT>]
hl7apy.parser.parse_segments(text, version=None, encoding_chars=None, validation_level=None, references=None, find_groups=False)

Parse the given ER7-encoded segments and return a list of hl7apy.core.Segment instances.

Parameters:
  • text (str) – the ER7-encoded string containing the segments to be parsed
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see validation_level)
  • references (list) – A list of the references of the Segment’s children
  • find_groups (bool) – if True, automatically assign the segments found to the appropriate Groups instances. If False, the segments found are assigned as children of the Message instance
Returns:

a list of Segment instances

>>> segments = "EVN||20080115153000||||20080114003000\rPID|1||566-554-3423^^^GHH^MR||EVERYMAN^ADAM^A|||M|||"     "2222 HOME STREET^^ANN ARBOR^MI^^USA||555-555-2004|||M\r"
>>> print(parse_segments(segments))
[<Segment EVN>, <Segment PID>]
hl7apy.parser.parse_segment(text, version=None, encoding_chars=None, validation_level=None, reference=None)

Parse the given ER7-encoded segment and return an instance of Segment.

Parameters:
  • text (str) – the ER7-encoded string containing the segment to be parsed
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
  • reference (dict) – a dictionary containing the element structure returned by load_reference, find_reference or belonging to a message profile
Returns:

an instance of Segment

>>> segment = "EVN||20080115153000||||20080114003000"
>>> s =  parse_segment(segment)
>>> print(s)
<Segment EVN>
>>> print(s.to_er7())
EVN||20080115153000||||20080114003000
hl7apy.parser.parse_fields(text, name_prefix=None, version=None, encoding_chars=None, validation_level=None, references=None, force_varies=False)

Parse the given ER7-encoded fields and return a list of hl7apy.core.Field.

Parameters:
  • text (str) – the ER7-encoded string containing the fields to be parsed
  • name_prefix (str) – the field prefix (e.g. MSH)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
  • references (list) – A list of the references of the Field’s children
  • force_varies (bool) – flag that force the fields to use a varies structure when no reference is found. It is used when a segment ends with a field of type varies that thus support infinite children
Returns:

a list of Field instances

>>> fields = "1|NUCLEAR^NELDA^W|SPO|2222 HOME STREET^^ANN ARBOR^MI^^USA"
>>> nk1_fields = parse_fields(fields, name_prefix="NK1")
>>> print(nk1_fields)
[<Field NK1_1 (SET_ID_NK1) of type SI>, <Field NK1_2 (NAME) of type XPN>, <Field NK1_3 (RELATIONSHIP) of type CE>, <Field NK1_4 (ADDRESS) of type XAD>]
>>> s = Segment("NK1")
>>> s.children = nk1_fields
>>> print(s.to_er7())
NK1|1|NUCLEAR^NELDA^W|SPO|2222 HOME STREET^^ANN ARBOR^MI^^USA
>>> unknown_fields = parse_fields(fields)
>>> s.children = unknown_fields
>>> print(s.to_er7())
NK1||||||||||||||||||||||||||||||||||||||||1|NUCLEAR^NELDA^W|SPO|2222 HOME STREET^^ANN ARBOR^MI^^USA
hl7apy.parser.parse_field(text, name=None, version=None, encoding_chars=None, validation_level=None, reference=None, force_varies=False)

Parse the given ER7-encoded field and return an instance of Field.

Parameters:
  • text (str) – the ER7-encoded string containing the fields to be parsed
  • name (str) – the field name (e.g. MSH_7)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
  • reference (dict) – a dictionary containing the element structure returned by load_reference or find_reference or belonging to a message profile
  • force_varies (boolean) – flag that force the fields to use a varies structure when no reference is found. It is used when a segment ends with a field of type varies that thus support infinite children
Returns:

an instance of Field

>>> field = "NUCLEAR^NELDA^W"
>>> nk1_2 = parse_field(field, name="NK1_2")
>>> print(nk1_2)
<Field NK1_2 (NAME) of type XPN>
>>> print(nk1_2.to_er7())
NUCLEAR^NELDA^W
>>> unknown = parse_field(field)
>>> print(unknown)
<Field of type None>
>>> print(unknown.to_er7())
NUCLEAR^NELDA^W
hl7apy.parser.parse_components(text, field_datatype='ST', version=None, encoding_chars=None, validation_level=None, references=None)

Parse the given ER7-encoded components and return a list of Component instances.

Parameters:
  • text (str) – the ER7-encoded string containing the components to be parsed
  • field_datatype (str) – the datatype of the components (e.g. ST)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
  • references (list) – A list of the references of the Component’s children
Returns:

a list of Component instances

>>> components = "NUCLEAR^NELDA^W^^TEST"
>>> xpn = parse_components(components, field_datatype="XPN")
>>> print(xpn)
[<Component XPN_1 (FAMILY_NAME) of type FN>, <Component XPN_2 (GIVEN_NAME) of type ST>, <Component XPN_3 (SECOND_AND_FURTHER_GIVEN_NAMES_OR_INITIALS_THEREOF) of type ST>, <Component XPN_5 (PREFIX_E_G_DR) of type ST>]
>>> print(parse_components(components))
[<Component ST (None) of type ST>, <Component ST (None) of type ST>, <Component ST (None) of type ST>, <Component ST (None) of type ST>, <Component ST (None) of type ST>]
hl7apy.parser.parse_component(text, name=None, datatype='ST', version=None, encoding_chars=None, validation_level=None, reference=None)

Parse the given ER7-encoded component and return an instance of Component.

Parameters:
  • text (str) – the ER7-encoded string containing the components to be parsed
  • name (str) – the component’s name (e.g. XPN_2)
  • datatype (str) – the datatype of the component (e.g. ST)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
  • reference (dict) – a dictionary containing the element structure returned by load_reference or find_reference or belonging to a message profile
Returns:

an instance of Component

>>> component = "GATEWAY&1.3.6.1.4.1.21367.2011.2.5.17"
>>> cx_4 = parse_component(component, name="CX_4")
>>> print(cx_4)
<Component CX_4 (ASSIGNING_AUTHORITY) of type None>
>>> print(cx_4.to_er7())
GATEWAY&1.3.6.1.4.1.21367.2011.2.5.17
>>> print(parse_component(component))
<Component ST (None) of type None>
hl7apy.parser.parse_subcomponents(text, component_datatype='ST', version=None, encoding_chars=None, validation_level=None)

Parse the given ER7-encoded subcomponents and return a list of SubComponent instances.

Parameters:
  • text (str) – the ER7-encoded string containing the components to be parsed
  • component_datatype (str) – the datatype of the subcomponents (e.g. ST)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see set_default_version)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see set_default_encoding_chars)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL class or None to use the default validation level (see set_default_validation_level)
Returns:

a list of SubComponent instances

>>> subcomponents= "ID&TEST&&AHAH"
>>> cwe = parse_subcomponents(subcomponents, component_datatype="CWE")
>>> print(cwe)
[<SubComponent CWE_1>, <SubComponent CWE_2>, <SubComponent CWE_4>]
>>> c = Component(datatype='CWE')
>>> c.children = cwe
>>> print(c.to_er7())
ID&TEST&&AHAH
>>> subs = parse_subcomponents(subcomponents)
>>> print(subs)
[<SubComponent ST>, <SubComponent ST>, <SubComponent ST>, <SubComponent ST>]
>>> c.children = subs
>>> print(c.to_er7())
&&&&&&&&&ID&TEST&&AHAH