How to use the hl7apy.core.Component function in hl7apy

To help you get started, we’ve selected a few hl7apy examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crs4 / hl7apy / tests / test_core.py View on Github external
def test_add_empty_subcomponent(self):
        c1 = Component('cx_4', validation_level=VALIDATION_LEVEL.STRICT)
        self.assertRaises(ChildNotValid, c1.add, SubComponent(datatype='ST'))

        c2 = Component('cx_4')
        c2.add(SubComponent(datatype='ST'))

        m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
        c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_4')
        self.assertRaises(ChildNotValid, c.add, SubComponent(datatype='ST'))

        m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.TOLERANT)
        c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_4')
        c.add(SubComponent(datatype='ST'))
github crs4 / hl7apy / tests / test_core.py View on Github external
# unknown
        c = Component()
        c.value = cmp_str
        self.assertEqual(c.to_er7(), cmp_str)

        # complex string
        cmp_str = '1&2'

        # tolerant
        c = Component('CWE_1', validation_level=VALIDATION_LEVEL.TOLERANT) # more child than allowed
        c.value = cmp_str
        self.assertEqual(c.to_er7(), cmp_str)
        self.assertEqual(len(c.children), 2)

        # strict
        c = Component('CWE_1', validation_level=VALIDATION_LEVEL.STRICT)
        with self.assertRaises(MaxChildLimitReached):
            c.value = cmp_str

        # unknown
        c = Component('CWE_1', validation_level=VALIDATION_LEVEL.TOLERANT) # more child than allowed
        c.value = cmp_str
        self.assertEqual(c.to_er7(), cmp_str)
        self.assertEqual(len(c.children), 2)

        # max length
        # tolerant
        for dt in ('ST', 'ID', 'FT', 'GTS', 'IS', 'TX'):
            c = Component(datatype=dt, validation_level=VALIDATION_LEVEL.TOLERANT) # max length reached string type
            c.value = 65537*'a'
        for dt in ('NM', 'SI'):
            c = Component(datatype=dt, validation_level=VALIDATION_LEVEL.TOLERANT)
github crs4 / hl7apy / tests / test_core.py View on Github external
def test_add_known_subcomponent_to_empty_component(self):
        c = Component()
        self.assertRaises(ChildNotValid, c.add, SubComponent('fn_1'))
github crs4 / hl7apy / tests / test_core.py View on Github external
def test_add_child_with_different_version(self):
        c = Component('CX_10', version='2.5')
        s = SubComponent('CWE_1', version='2.6')
        self.assertRaises(OperationNotAllowed, c.add, s)
github crs4 / hl7apy / tests / test_core.py View on Github external
with self.assertRaises(ChildNotValid):
            c.value = SI(1)

        # strict
        c = Component('CX_1', validation_level=VALIDATION_LEVEL.STRICT)
        c.value = ST('aaa')
        self.assertEqual(c.to_er7(), 'aaa')
        c.value = ST('bbb')
        self.assertEqual(c.to_er7(), 'bbb')

        with self.assertRaises(ChildNotValid):
            c.value = SI(1)

        # complex datatype
        # tolerant
        c = Component('CX_10', validation_level=VALIDATION_LEVEL.TOLERANT)
        with self.assertRaises(ChildNotValid):
            c.value = ST('aaa')

        # strict
        c = Component('CX_10', validation_level=VALIDATION_LEVEL.STRICT)
        with self.assertRaises(ChildNotValid):
            c.value = ST('aaa')
github crs4 / hl7apy / tests / test_core.py View on Github external
def test_add_empty_subcomponent(self):
        c1 = Component('cx_4', validation_level=VALIDATION_LEVEL.STRICT)
        self.assertRaises(ChildNotValid, c1.add, SubComponent(datatype='ST'))

        c2 = Component('cx_4')
        c2.add(SubComponent(datatype='ST'))

        m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
        c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_4')
        self.assertRaises(ChildNotValid, c.add, SubComponent(datatype='ST'))

        m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.TOLERANT)
        c = m.add_segment('QPD').add_field('QPD_8').add_component('CX_4')
        c.add(SubComponent(datatype='ST'))
github crs4 / hl7apy / tests / test_core.py View on Github external
def test_add_subcomponent_to_unknown_base_dt_component(self):
        c = Component(datatype='ST')
        self.assertRaises(ChildNotValid, c.add_subcomponent, 'HD_1')
github crs4 / hl7apy / hl7apy / parser.py View on Github external
version = _get_version(version)
    encoding_chars = _get_encoding_chars(encoding_chars, version)
    validation_level = _get_validation_level(validation_level)

    try:
        field = Field(name, version=version, validation_level=validation_level, reference=reference)
    except InvalidName:
        if force_varies:
            reference = ('leaf', None, 'varies', None, None, -1)
            field = Field(name, version=version, validation_level=validation_level, reference=reference)
        else:
            field = Field(version=version, validation_level=validation_level, reference=reference)

    if name in ('MSH_1', 'MSH_2'):
        s = SubComponent(datatype='ST', value=text, validation_level=validation_level, version=version)
        c = Component(datatype='ST', validation_level=validation_level, version=version)
        c.add(s)
        field.add(c)
    else:
        children = parse_components(text, field.datatype, version, encoding_chars, validation_level,
                                    field.structure_by_name)
        if Validator.is_tolerant(validation_level) and is_base_datatype(field.datatype, version) and \
                len(children) > 1:
            field.datatype = None
        field.children = children
    return field
github crs4 / hl7apy / hl7apy / core.py View on Github external
if self.name and is_base_datatype(self.datatype, self.version) and \
                len(self.children) >= 1:
            raise MaxChildLimitReached(self, obj, 1)

        # the name is different from the datatype (i.e. the name has been forced to be equal to the datatype)
        try:
            if obj.name and obj.name != obj.datatype:
                try:
                    if not _valid_child_name(obj.name, self.datatype):
                        raise ChildNotValid(obj.name, self)
                except AttributeError:
                    pass
        except ChildNotFound:  # obj.datatype causes ChildNotFound for some Elements (Message, Groups etc)
            raise ChildNotValid(obj, self)

        return super(Component, self).add(obj)