Core classes

HL7apy - core classes

class hl7apy.core.Element(name=None, parent=None, reference=None, version=None, validation_level=None, traversal_parent=None)

Base class for all HL7 elements. It is not meant to be directly instantiated.

class hl7apy.core.Message(name=None, reference=None, version=None, validation_level=None, encoding_chars=None)

Class representing an HL7 message

Parameters:
  • name (str) – the HL7 name of the message (e.g. OML_O33)
  • name – the HL7 name of the segment (e.g. PID)
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see get_default_encoding_chars)
add(obj)

Add an instance of Element subclass to the list of children

Parameters:obj (hl7apy.core.Element) – an instance of hl7apy.core.Element subclass
>>> s = Segment('PID')
>>> f = Field('PID_5')
>>> f.value = 'EVERYMAN^ADAM'
>>> s.add(f)
>>> print(s.to_er7())
PID|||||EVERYMAN^ADAM
add_group(name)

Create an instance of Group having the given name

Parameters:name – the name of the group to be created (e.g. OML_O33_PATIENT)
Returns:an instance of Group
>>> m = Message('OML_O33')
>>> patient = m.add_group('OML_O33_PATIENT')
>>> print(patient)
<Group OML_O33_PATIENT>
>>> print(patient in m.children)
True
add_segment(name)

Create an instance of Segment having the given name

Parameters:name – the name of the segment to be created (e.g. PID)
Returns:an instance of Segment
>>> m = Message('QBP_Q11')
>>> qpd = m.add_segment('QPD')
>>> print(qpd)
<Segment QPD>
>>> print(qpd in m.children)
True
to_er7(encoding_chars=None, trailing_children=False)

Returns the HL7 representation of the Element. It adds the appropriate separator at the end if needed

Parameters:encoding_chars (dict) – The encoding chars to use. If it is None it uses self.encoding_chars, which by default is the ones return by get_default_encoding_chars values
Return type:str
Returns:the HL7 representation of the Element
to_mllp(encoding_chars=None, trailing_children=False)

Returns the er7 representation of the message wrapped with mllp encoding characters

Parameters:
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see get_default_encoding_chars)
  • trailing_children (bool) – if True, trailing children will be added even if their value is None
Returns:

the ER7-encoded string wrapped with the mllp encoding characters

validate(report_file=None)

Validate the HL7 element using the STRICT validation level. It calls the Validator.validate method passing the reference used in the instantiation of the element.

Param:report_file: the report file to pass to the validator
class hl7apy.core.Group(name=None, parent=None, reference=None, version=None, validation_level=None, traversal_parent=None)

Class representing an HL7 segment group

Parameters:
  • name (str) – the HL7 name of the message (e.g. RSP_K21_QUERY_RESPONSE)
  • parent (an instance of Message, Group or None) – the parent
  • reference – the reference structure (see load_reference)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see get_default_version)
  • validation_level – the validation level. Possible values are those defined in VALIDATION_LEVEL or None to use the default validation level (see get_default_validation_level)
add(obj)

Add an instance of Element subclass to the list of children

Parameters:obj (hl7apy.core.Element) – an instance of hl7apy.core.Element subclass
>>> s = Segment('PID')
>>> f = Field('PID_5')
>>> f.value = 'EVERYMAN^ADAM'
>>> s.add(f)
>>> print(s.to_er7())
PID|||||EVERYMAN^ADAM
add_group(name)

Create an instance of Group having the given name

Parameters:name – the name of the group to be created (e.g. OML_O33_PATIENT)
Returns:an instance of Group
>>> m = Message('OML_O33')
>>> patient = m.add_group('OML_O33_PATIENT')
>>> print(patient)
<Group OML_O33_PATIENT>
>>> print(patient in m.children)
True
add_segment(name)

Create an instance of Segment having the given name

Parameters:name – the name of the segment to be created (e.g. PID)
Returns:an instance of Segment
>>> m = Message('QBP_Q11')
>>> qpd = m.add_segment('QPD')
>>> print(qpd)
<Segment QPD>
>>> print(qpd in m.children)
True
to_er7(encoding_chars=None, trailing_children=False)

Returns the HL7 representation of the Element. It adds the appropriate separator at the end if needed

Parameters:encoding_chars (dict) – The encoding chars to use. If it is None it uses self.encoding_chars, which by default is the ones return by get_default_encoding_chars values
Return type:str
Returns:the HL7 representation of the Element
validate(report_file=None)

Validate the HL7 element using the STRICT validation level. It calls the Validator.validate method passing the reference used in the instantiation of the element.

Param:report_file: the report file to pass to the validator
class hl7apy.core.Segment(name=None, parent=None, reference=None, version=None, validation_level=None, traversal_parent=None)

Class representing an HL7 segment.

Parameters:
add(obj)

Add an instance of Element subclass to the list of children

Parameters:obj (hl7apy.core.Element) – an instance of hl7apy.core.Element subclass
>>> s = Segment('PID')
>>> f = Field('PID_5')
>>> f.value = 'EVERYMAN^ADAM'
>>> s.add(f)
>>> print(s.to_er7())
PID|||||EVERYMAN^ADAM
add_field(name)

Create an instance of Field having the given name

Parameters:name – the name of the field to be created (e.g. PID_1)
Returns:an instance of Field
>>> s = Segment('PID')
>>> print(s.add_field('PID_1'))
<Field PID_1 (SET_ID_PID) of type SI>
to_er7(encoding_chars=None, trailing_children=False)

Return the ER7-encoded string

Parameters:
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see get_default_encoding_chars)
  • trailing_children (bool) – if True, trailing children will be added even if their value is None
Returns:

the ER7-encoded string

>>> pid = Segment("PID")
>>> pid.pid_1 = '1'
>>> pid.pid_5 = "EVERYMAN^ADAM"
>>> print(pid.to_er7())
PID|1||||EVERYMAN^ADAM
validate(report_file=None)

Validate the HL7 element using the STRICT validation level. It calls the Validator.validate method passing the reference used in the instantiation of the element.

Param:report_file: the report file to pass to the validator
class hl7apy.core.Field(name=None, datatype=None, parent=None, reference=None, version=None, validation_level=None, traversal_parent=None)

Class representing an HL7 field.

Parameters:
  • name (str) – the HL7 name of the field (e.g. PID_5)
  • datatype (str) – the datatype of the field (e.g. CE)
  • parent (an instance of hl7apy.core.Segment or None) – the parent
  • reference – the reference structure (see load_reference)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see get_default_version)
  • validation_level – the validation level. Possible values are those defined in VALIDATION_LEVEL or None to use the default validation level (see get_default_validation_level)
  • traversal_parent (an instance of Segment or None) – the temporary parent used during traversal
add(obj)

Add an instance of Component to the list of children

Parameters:obj – an instance of Component
>>> f = Field('PID_5')
>>> f.xpn_1 = 'EVERYMAN'
>>> c = Component('XPN_2')
>>> c.value = 'ADAM'
>>> f.add(c)
>>> print(f.to_er7())
EVERYMAN^ADAM
add_component(name)

Create an instance of Component having the given name

Parameters:name – the name of the component to be created (e.g. XPN_2)
Returns:an instance of Component
>>> s = Field('PID_5')
>>> print(s.add_component('XPN_2'))
<Component XPN_2 (GIVEN_NAME) of type ST>
to_er7(encoding_chars=None, trailing_children=False)

Return the ER7-encoded string

Parameters:
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see get_default_encoding)
  • trailing_children (bool) – if True, trailing children will be added even if their value is None
Returns:

the ER7-encoded string

>>> msh_9 = Field("MSH_9")
>>> msh_9.value = "ADT^A01^ADT_A01"
>>> print(msh_9.to_er7())
ADT^A01^ADT_A01
validate(report_file=None)

Validate the HL7 element using the STRICT validation level. It calls the Validator.validate method passing the reference used in the instantiation of the element.

Param:report_file: the report file to pass to the validator
class hl7apy.core.Component(name=None, datatype=None, parent=None, reference=None, version=None, validation_level=None, traversal_parent=None)

Class representing an HL7 component.

Parameters:
  • name (str) – the HL7 name of the component (e.g. XPN_2)
  • datatype (str) – the datatype of the component (e.g. CE)
  • parent (an instance of hl7apy.core.Field or None) – the parent
  • reference – the reference structure (see load_reference)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see get_default_version)
  • validation_level (int) – the validation level. Possible values are those defined in VALIDATION_LEVEL or None to use the default validation level (see get_default_validation_level)
  • traversal_parent (an instance of Field hl7apy.core.Field or None) – the temporary parent used during traversal
add(obj)

Add an instance of SubComponent to the list of children

Parameters:obj – an instance of SubComponent
>>> c = Component('CX_10')
>>> s = SubComponent(name='CWE_1', value='EXAMPLE_ID')
>>> s2 = SubComponent(name='CWE_4', value='ALT_ID')
>>> c.add(s)
>>> c.add(s2)
>>> print(c.to_er7())
EXAMPLE_ID&&&ALT_ID
add_subcomponent(name)

Create an instance of SubComponent having the given name

Parameters:name – the name of the subcomponent to be created (e.g. CE_1)
Returns:an instance of SubComponent
>>> c = Component(datatype='CE')
>>> ce_1 = c.add_subcomponent('CE_1')
>>> print(ce_1)
<SubComponent CE_1>
>>> print(ce_1 in c.children)
True
to_er7(encoding_chars=None, trailing_children=False)

Returns the HL7 representation of the Element. It adds the appropriate separator at the end if needed

Parameters:encoding_chars (dict) – The encoding chars to use. If it is None it uses self.encoding_chars, which by default is the ones return by get_default_encoding_chars values
Return type:str
Returns:the HL7 representation of the Element
validate(report_file=None)

Validate the HL7 element using the STRICT validation level. It calls the Validator.validate method passing the reference used in the instantiation of the element.

Param:report_file: the report file to pass to the validator
class hl7apy.core.SubComponent(name=None, datatype=None, value=None, parent=None, reference=None, version=None, validation_level=None, traversal_parent=None)

Class representing an HL7 subcomponent.

Parameters:
  • name (str) – the HL7 name of the subcomponent (e.g. CWE_1)
  • datatype (str) – the datatype of the component (e.g. ST)
  • value (str or instance of BaseDataType) – the value of the subcomponent (e.g. ADT_A01)
  • parent (an instance of Component or None) – the parent
  • reference – the reference structure (see load_refernce)
  • version (str) – the HL7 version (e.g. “2.5”), or None to use the default (see get_default_version)
  • validation_level (int) – the validation level. Possible values are defined in VALIDATION_LEVEL class or None to use the default validation level (see get_default_validation_level)
  • traversal_parent (an instance of Component or None) – the temporary parent used during traversal
to_er7(encoding_chars=None, trailing_children=False)

Return the ER7-encoded string

Parameters:
  • encoding_chars (dict) – a dictionary containing the encoding chars or None to use the default (see get_default_encoding_chars)
  • trailing_children (bool) – if True, trailing children will be added even if their value is None
Returns:

the ER7-encoded string

>>> s = SubComponent("CE_1")
>>> s.value = "IDENTIFIER"
>>> print(s.to_er7())
IDENTIFIER
validate(report_file=None)

Validate the HL7 element using the STRICT validation level. It calls the Validator.validate method passing the reference used in the instantiation of the element.

Param:report_file: the report file to pass to the validator