Library helper functions¶
-
hl7apy.check_encoding_chars(encoding_chars)¶ Validate the given encoding chars
Parameters: encoding_chars ( dict) – the encoding chars (seehl7apy.set_default_encoding_chars())Raises: hl7apy.exceptions.InvalidEncodingCharsif the given encoding chars are not valid
-
hl7apy.check_validation_level(validation_level)¶ Validate the given validation level
Parameters: validation_level ( int) – validation level (seehl7apy.consts.VALIDATION_LEVEL)Raises: hl7apy.exceptions.UnknownValidationLevelif 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.UnsupportedVersionif 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 (
listortuple) – 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: dictReturns: a dictionary describing the element structure
Raise: hl7apy.exceptions.ChildNotFoundif 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'>
- name (
-
hl7apy.get_default_encoding_chars(version=None)¶ Get the default encoding chars
Return type: dictReturns: 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: strReturns: the default validation level >>> print(get_default_validation_level()) 2
-
hl7apy.get_default_version()¶ Get the default version
Return type: strReturns: 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: dictReturns: a dictionary describing the element structure
Raise: KeyErrorif the element has not been foundThe returned dictionary will contain the following keys:
Key Value cls an hl7apy.core.Elementsubclassname 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
- element_type (
-
hl7apy.set_default_encoding_chars(encoding_chars)¶ Set the given encoding chars as default
Parameters: encoding_chars ( dict) – the new encoding charsRaises: hl7apy.exceptions.InvalidEncodingCharsif the given encoding chars are not validThe encoding_chars dictionary should contain the following keys:
Key Default GROUP \rSEGMENT \rFIELD |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 (seehl7apy.consts.VALIDATION_LEVEL)Raises: hl7apy.exceptions.UnknownValidationLevelif 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.UnsupportedVersionif 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