Library helper functions

hl7apy.check_encoding_chars(encoding_chars)

Validate the given encoding chars

Parameters:encoding_chars (dict) – the encoding chars (see hl7apy.set_default_encoding_chars())
Raises:hl7apy.exceptions.InvalidEncodingChars if the given encoding chars are not valid
hl7apy.check_validation_level(validation_level)

Validate the given validation level

Parameters:validation_level (int) – validation level (see hl7apy.consts.VALIDATION_LEVEL)
Raises:hl7apy.exceptions.UnknownValidationLevel if the given validation level is unsupported
hl7apy.check_version(version)

Validate the given version number

Parameters:version (str) – the version to validate (e.g. 2.6)
Raises:hl7apy.exceptions.UnsupportedVersion if the given version is unsupported
hl7apy.find_reference(name, element_types, version)

Look for an element of the given name and version into the given types and return its reference structure

Parameters:
  • name (str) – the element name to look for (e.g. ‘MSH’)
  • types (list or tuple) – the element classes where to look for the element (e.g. (Group, Segment))
  • version (str) – the version of the library where to search the element (e.g. ‘2.6’)
Return type:

dict

Returns:

a dictionary describing the element structure

Raise:

hl7apy.exceptions.ChildNotFound if the element has not been found

>>> from hl7apy.core import Message, Segment
>>> find_reference('UNKNOWN', (Segment, ), '2.5')  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ChildNotFound: No child named UNKNOWN
>>> find_reference('ADT_A01', (Segment,),  '2.5')  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ChildNotFound: No child named ADT_A01
>>> r = find_reference('ADT_A01', (Message,),  '2.5')
>>> print('%s %s' % (r['name'], r['cls']))
ADT_A01 <class 'hl7apy.core.Message'>
hl7apy.get_default_encoding_chars(version=None)

Get the default encoding chars

Return type:dict
Returns:the encoding chars (see hl7apy.set_default_encoding_chars())
>>> print(get_default_encoding_chars('2.6')['FIELD'])
|
hl7apy.get_default_validation_level()

Get the default validation level

Return type:str
Returns:the default validation level
>>> print(get_default_validation_level())
2
hl7apy.get_default_version()

Get the default version

Return type:str
Returns:the default version
>>> print(get_default_version())
2.5
hl7apy.load_library(version)

Load the correct module according to the version

Parameters:version (str) – the version of the library to be loaded (e.g. ‘2.6’)
Return type:module object
hl7apy.load_reference(name, element_type, version)

Look for an element of the given type, name and version and return its reference structure

Parameters:
  • element_type (str) – the element type to look for (e.g. ‘Segment’)
  • name (str) – the element name to look for (e.g. ‘MSH’)
  • version (str) – the version of the library where to search the element (e.g. ‘2.6’)
Return type:

dict

Returns:

a dictionary describing the element structure

Raise:

KeyError if the element has not been found

The returned dictionary will contain the following keys:

Key Value
cls an hl7apy.core.Element subclass
name the Element name (e.g. PID)
ref

a tuple of one of the following format:

(‘leaf’, <datatype>, <longName>, <table>) (‘sequence’, (<child>, (<min>, <max>), …))

>>> load_reference('UNKNOWN', 'Segment', '2.5')  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ChildNotFound: No child named UNKNOWN
>>> r = load_reference('ADT_A01', 'Message', '2.5')
>>> print(r[0])
sequence
>>> r = load_reference('MSH_3', 'Field', '2.5')
>>> print(r[0])
sequence
hl7apy.set_default_encoding_chars(encoding_chars)

Set the given encoding chars as default

Parameters:encoding_chars (dict) – the new encoding chars
Raises:hl7apy.exceptions.InvalidEncodingChars if the given encoding chars are not valid

The encoding_chars dictionary should contain the following keys:

Key Default
GROUP \r
SEGMENT \r
FIELD |
COMPONENT ^
SUBCOMPONENT &
REPETITION ~
ESCAPE \
>>> set_default_encoding_chars({'FIELD': '!'})  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
    ...
InvalidEncodingChars: Missing required encoding chars
>>> set_default_encoding_chars({'FIELD': '!', 'COMPONENT': 'C', 'SUBCOMPONENT': 'S',                                     'REPETITION': 'R', 'ESCAPE': '\\'})
>>> print(get_default_encoding_chars('2.5')['FIELD'])
!
hl7apy.set_default_validation_level(validation_level)

Set the given validation level as default

Parameters:validation_level (int) – validation level (see hl7apy.consts.VALIDATION_LEVEL)
Raises:hl7apy.exceptions.UnknownValidationLevel if the given validation level is unsupported
>>> set_default_validation_level(3)  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
    ...
UnknownValidationLevel
>>> set_default_validation_level(VALIDATION_LEVEL.TOLERANT)
>>> print(get_default_validation_level())
2
hl7apy.set_default_version(version)

Set the given version as default

Parameters:version (str) – the new default version (e.g. 2.6)
Raises:hl7apy.exceptions.UnsupportedVersion if the given version is unsupported
>>> set_default_version('22')  # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
    ...
UnsupportedVersion: The version 22 is not supported
>>> set_default_version('2.3')
>>> print(get_default_version())
2.3