def test_add_field(self): e = Segment('PID') pid_5 = e.add_field('PID_5') self.assertEqual(pid_5.classname, 'Field') self.assertTrue(pid_5.is_named('PID_5')) self.assertTrue(pid_5.is_named('PATIENT_NAME')) self.assertRaises(ChildNotFound, e.add_field, 'UNKNOWN_FIELD')
def test_assign_wrong_field_to_known_position(self): s1 = Segment('MSH',validation_level=VALIDATION_LEVEL.STRICT) s2 = Segment('QPD') with self.assertRaises(ChildNotValid): s1.msh_10 = Field('spm_10') with self.assertRaises(ChildNotValid): s2.qpd_3 = Field('pid_3')
def parse_segment(text, version=None, encoding_chars=None, validation_level=None, reference=None): """ Parse the given ER7-encoded segment and return an instance of :class:`hl7apy.core.Segment`. :type text: ``basestring`` :param text: the ER7-encoded string containing the segment to be parsed :type version: ``basestring`` :param version: the HL7 version (e.g. "2.5"), or ``None`` to use the default (see :func:`hl7apy.set_default_version`) :type encoding_chars: ``dict`` :param encoding_chars: a dictionary containing the encoding chars or None to use the default (see :func:`hl7apy.set_default_encoding_chars`) :param validation_level: the validation level. Possible values are those defined in :class:`hl7apy.consts.VALIDATION_LEVEL` class or ``None`` to use the default validation level (see :func:`hl7apy.set_default_validation_level`) :type reference: ``dict`` :param reference: a dictionary containing the element structure returned by :func:`hl7apy.load_reference` or :func:`hl7apy.find_reference` :return: an instance of :class:`hl7apy.core.Segment` >>> segment = "EVN||20080115153000||||20080114003000" >>> s = parse_segment(segment) >>> print s <Segment EVN> >>> print s.to_er7() EVN||20080115153000||||20080114003000 """ version = _get_version(version) encoding_chars = _get_encoding_chars(encoding_chars) segment_name = text[:3] text = text[4:] if segment_name != 'MSH' else text[3:] segment = Segment(segment_name, version=version, validation_level=validation_level, reference=reference) segment.children = parse_fields(text, segment_name, version, encoding_chars, validation_level, segment.allow_infinite_children) return segment
def test_assign_value_traversal(self): field_str = '1010110909194822^^^AUTH&1.3.6.1.4.1.21367.2011.2.5.17&ISO^PK' s1 = Segment('PID') s2 = Segment('PID') s1.pid_3.value = field_str s2.pid_3 = field_str self.assertEqual(s1.to_er7(), s2.to_er7())
def reply(message): """ Parse the incoming message and return the ER7 encoded response :param message: incoming message :return: response encoded to ER7 - it can be a NAK or RSP_K11 message """ print "Received by LIP", repr(message) try: # parse the incoming message m = parse_message(message, find_groups=False) except: print 'parsing failed', repr(message) response = LIP.nak() else: print "Message type:", m.MSH.message_type.to_er7() print "Message content:", repr(m.to_er7()) if m.MSH.MSH_9.MSH_9_3.to_er7() == 'QBP_Q11': # create a new RSP_K11 message response = Message("RSP_K11") response.MSH.MSH_9 = "RSP^K11^RSP_K11" # add MSA segment response.MSA = "MSA|AA" response.MSA.MSA_2 = m.MSH.MSH_10 # create a QAK segment qak = Segment("QAK") qak.qak_1 = m.QPD.QPD_2 qak.qak_2 = "OK" qak.qak_3 = "Q22^Specimen Labeling Instructions^IHE_LABTF" qak.qak_4 = "1" # add the QAK segment to the RSP_K11 message response.add(qak) # copy the QPD segment from the incoming message response.QPD = m.QPD # create a PID segment response.PID.PID_1 = '1' response.PID.PID_5.PID_5_1 = 'PATIENT_SURNAME' response.PID.PID_5.PID_5_2 = 'PATIENT_NAME' response.PID.PID_6 = "19800101" response.PID.PID_7 = "F" # create a SPM segment spm = Segment("SPM") # create an OBR segment obr = Segment("OBR") spm.SPM_1 = '1' spm.SPM_2 = "12345" obr.OBR_4 = "ORDER^DESCRIPTION" # add spm and obr to the RSP_K11 response response.add(spm) response.add(obr) else: response = LIP.nak(m) return response.to_mllp() # encode to ER7
def parse_segment(text, version=None, encoding_chars=None, validation_level=None, reference=None): """ Parse the given ER7-encoded segment and return an instance of :class:`Segment <hl7apy.core.Segment>`. :type text: ``str`` :param text: the ER7-encoded string containing the segment to be parsed :type version: ``str`` :param version: the HL7 version (e.g. "2.5"), or ``None`` to use the default (see :func:`set_default_version <hl7apy.set_default_version>`) :type encoding_chars: ``dict`` :param encoding_chars: a dictionary containing the encoding chars or None to use the default (see :func:`set_default_encoding_chars <hl7apy.set_default_encoding_chars>`) :type validation_level: ``int`` :param validation_level: the validation level. Possible values are those defined in :class:`VALIDATION_LEVEL <hl7apy.consts.VALIDATION_LEVEL>` class or ``None`` to use the default validation level (see :func:`set_default_validation_level <hl7apy.set_default_validation_level>`) :type reference: ``dict`` :param reference: a dictionary containing the element structure returned by :func:`load_reference <hl7apy.load_reference>`, :func:`find_reference <hl7apy.find_reference>` or belonging to a message profile :return: an instance of :class:`Segment <hl7apy.core.Segment>` >>> segment = "EVN||20080115153000||||20080114003000" >>> s = parse_segment(segment) >>> print(s) <Segment EVN> >>> print(s.to_er7()) EVN||20080115153000||||20080114003000 """ version = _get_version(version) encoding_chars = _get_encoding_chars(encoding_chars, version) validation_level = _get_validation_level(validation_level) segment_name = text[:3] text = text[4:] if segment_name != 'MSH' else text[3:] segment = Segment(segment_name, version=version, validation_level=validation_level, reference=reference) segment.children = parse_fields(text, segment_name, version, encoding_chars, validation_level, segment.structure_by_name, segment.allow_infinite_children) return segment
def test_assign_value_traversal(self): subcmp_str = 'xxx' c1 = Component('CX_10') c2 = Component('CX_10') c1.cwe_1 = subcmp_str c2.cwe_1.value = subcmp_str self.assertEqual(c1.to_er7(), c2.to_er7()) s1 = Segment('PID') s2 = Segment('PID') s1.pid_4.pid_4_10_1.value = subcmp_str s2.pid_4.pid_4_10_1 = subcmp_str self.assertEqual(s1.to_er7(), s2.to_er7())
def _populate_telecom_fields(segment: Segment, personal_telecom: str, work_telecom: str, telecom_list) -> None: for telecom in telecom_list: telecom_type = telecom.get("use", "") telecom_value = telecom.get("value", "") if telecom_type in ["home", "temp", "old", "mobile", ""]: telecom = personal_telecom else: telecom = work_telecom if telecom_value: telecom_field = Field(telecom) telecom_field.XTN_2 = _get_telecom_use_code(telecom_type) telecom_field.XTN_12 = telecom_value segment.add(telecom_field)
def _create_pid_segment(fhir_resource: dict) -> Segment: pid = Segment("PID") pid.PID_1 = str(1) for id_ in fhir_resource.get("identifier"): id_value = id_.get("value", "") id_type = id_.get("type", {}) if any([id_value, id_type]): pid_3 = pid.add_field("PID_3") pid_3.PID_3_1 = id_value pid_3.PID_3_4 = id_.get("system", "") if id_type_coding := id_type.get("coding"): pid_3.PID_3_5 = id_type_coding[0].get("code", "") if assigner := id_.get("assigner"): pid_3.PID_3_9 = assigner.get("display", "")
def test_assign_value(self): segment_str = 'PID|1||123-456-789^^^HOSPITAL^MR||SURNAME^NAME^A|||M|||1111 SOMEWHERE STREET^^SOMEWHERE^^^USA||555-555-2004~444-333-222|||M\r' s = Segment('PID') parsed_s = parse_segment(segment_str) s.value = segment_str self.assertEqual(s.to_er7(), parsed_s.to_er7()) s = Segment('PID', validation_level=VALIDATION_LEVEL.STRICT) parsed_a = parse_segment(segment_str) s.value = segment_str self.assertEqual(s.to_er7(), parsed_a.to_er7())
def generate_OBX(self): """ **********OBX Segment********** Generate a separate OBX Segement for each of the following observations : Sr. No. | Field Sequence Number | Field Description 1 | 1 | Observation Sequence ID 2 | 3 | Observation Identifier (Observation Sequence ID, Observation Name) 3 | 5 | Observation Value 4 | 6 | Units 4 | 14 | Observation Timestamp """ # date_time = datetime.fromtimestamp(timestamp) # d = date_time.strftime("\'%Y-%m-%d %H:%M:%S\' UTC") for each in records: for i in range(len(obx_codes)): obx = Segment('OBX') obx.obx_1 = each['record_id'] obx.obx_3 = obx_codes[i] obx.obx_5 = str(each['obx_test'][i]) obx.obx_6 = obx_units[i] obx.obx_14 = each['timestamp'] self.m.add(obx)
def test_recursive_traversal(self): obr = Segment('OBR') obr.obr_26 = 'xxx&yyy^zzz^www' by_name = obr.parent_result.parent_observation_identifier by_position = obr.obr_26.obr_26_1 self.assertEqual(by_name[0], by_position[0]) # bug!
def responder(m): # cria uma mensagem de resposta RSP_K11 response = Message("RSP_K11") response.MSH.MSH_9 = "RSP^K11^RSP_K11" response.MSA = "MSA|AA" response.MSA.MSA_2 = m.MSH.MSH_10 qak = Segment("QAK") qak.qak_1 = m.QPD.QPD_2 qak.qak_2 = "OK" qak.qak_3 = "Q22^Specimen Labeling Instructions^IHE_LABTF" qak.qak_4 = "1" response.add(qak) response.QPD = m.QPD response.PID.PID_1 = '1' response.PID.PID_5.PID_5_1 = 'CUNHA' response.PID.PID_5.PID_5_2 = 'JOSE' response.PID.PID_6 = "19800101" response.PID.PID_7 = "F" response.PID.PID_23 = "Brasil" spm = Segment("SPM") obr = Segment("OBR") spm.SPM_1 = '1' spm.SPM_2 = "12345" obr.OBR_4 = "ORDER^DESCRIPTION" response.add(spm) response.add(obr) return response.to_mllp()
def reply(message): """ Parse the incoming message and return the ER7 encoded response :param message: incoming message :return: response encoded to ER7 - it can be a NAK or RSP_K11 message """ print("Received by LIP", repr(message)) try: # parse the incoming message m = parse_message(message, find_groups=False) except: print('parsing failed', repr(message)) response = LIP.nak() else: print("Message type:", m.MSH.message_type.to_er7()) print("Message content:", repr(m.to_er7())) if m.MSH.MSH_9.MSH_9_3.to_er7() == 'QBP_Q11': # create a new RSP_K11 message response = Message("RSP_K11") response.MSH.MSH_9 = "RSP^K11^RSP_K11" # add MSA segment response.MSA = "MSA|AA" response.MSA.MSA_2 = m.MSH.MSH_10 # create a QAK segment qak = Segment("QAK") qak.qak_1 = m.QPD.QPD_2 qak.qak_2 = "OK" qak.qak_3 = "Q22^Specimen Labeling Instructions^IHE_LABTF" qak.qak_4 = "1" # add the QAK segment to the RSP_K11 message response.add(qak) # copy the QPD segment from the incoming message response.QPD = m.QPD # create a PID segment response.PID.PID_1 = '1' response.PID.PID_5.PID_5_1 = 'PATIENT_SURNAME' response.PID.PID_5.PID_5_2 = 'PATIENT_NAME' response.PID.PID_6 = "19800101" response.PID.PID_7 = "F" # create a SPM segment spm = Segment("SPM") # create an OBR segment obr = Segment("OBR") spm.SPM_1 = '1' spm.SPM_2 = "12345" obr.OBR_4 = "ORDER^DESCRIPTION" # add spm and obr to the RSP_K11 response response.add(spm) response.add(obr) else: response = LIP.nak(m) return response.to_mllp() # encode to ER7
def test_to_string_z_segment_with_unknown_fields(self): f1 = Field() f1.value = 'abc' f2 = Field() f2.value = 'cba' zin = Segment('ZIN') zin.zin_1 = 'yyy' zin.add(f1) self.assertEqual(zin.to_er7(), 'ZIN|yyy|abc') zin.zin_4 = 'zzz' self.assertEqual(zin.to_er7(), 'ZIN|yyy|||zzz|abc') zin.add(f2) self.assertEqual(zin.to_er7(), 'ZIN|yyy|||zzz|abc|cba')
def test_to_string_segment_with_infinite_children(self): qpd = Segment('QPD', validation_level=VALIDATION_LEVEL.STRICT) qpd.qpd_3 = 'abc' qpd.qpd_10 = 'cba' self.assertEqual(qpd.to_er7(), 'QPD|||abc|||||||cba') zin = Segment('ZIN',validation_level=VALIDATION_LEVEL.STRICT) zin.zin_1 = 'yyy' self.assertEqual(zin.to_er7(), 'ZIN|yyy') zin.zin_10 = 'www' self.assertEqual(zin.to_er7(), 'ZIN|yyy|||||||||www')
def _populate_nk1_segment(nk1: Segment, contact: dict, set_id: int) -> None: nk1.NK1_1 = str(set_id) if name := contact.get("name"): nk1_2 = Field("NK1_2") _populate_name_field(nk1_2, name) nk1.add(nk1_2)
def test_traversal_equality(self): obr = Segment('OBR') obr.obr_26 = 'xxx&yyy^zzz^www' obr_26 = obr.parent_result self.assertTrue(isinstance(obr_26, ElementProxy)) self.assertTrue(obr_26[0] == obr.obr_26[0], 'obr.parent_result != obr.obr_26')
def test_add_empty_field(self): s = Segment('SPM', validation_level=VALIDATION_LEVEL.STRICT) self.assertRaises(ChildNotValid, s.add, Field()) s = Segment('SPM') s.add(Field())
def test_assign_complex_field_datatype_by_get(self): p = Segment('PID') p.pid_5 = 'test^test' self.assertEqual(p.pid_5.xpn_1.to_er7(), 'test') self.assertEqual(p.pid_5.xpn_2.to_er7(), 'test')
def test_assign_field_by_get(self): s = Segment('MSH') s.msh_10 = 'Value' s.msh_10.msh_10_1 = 'Value'
def test_assign_wrong_value(self): s = Segment('PID') wrong_segment_str = 'EVN|1||123-456-789^^^HOSPITAL^MR||SURNAME^NAME^A|||M|||1111 SOMEWHERE STREET^^SOMEWHERE^^^USA||555-555-2004~444-333-222|||M\r' with self.assertRaises(OperationNotAllowed): s.value = wrong_segment_str