Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
f = m.rsp_k21_query_response.pid.add_field('PID_3') # it is a complex datatype field
with self.assertRaises(ChildNotValid):
f.value = ST('aaa')
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
f = m.rsp_k21_query_response.pid.add_field('PID_1')
f.value = SI(1)
self.assertEqual(f.to_er7(), '1')
f.value = SI(2)
self.assertEqual(f.to_er7(), '2')
with self.assertRaises(ChildNotValid):
f.value = ST('aaa')
m = Message('RSP_K21', reference=self.rsp_k21_mp, validation_level=VALIDATION_LEVEL.STRICT)
f = m.rsp_k21_query_response.pid.add_field('PID_3') # it is a complex datatype field
with self.assertRaises(ChildNotValid):
f.value = ST('aaa')
if isinstance(value, ElementProxy):
value = value[0].to_er7()
name = name.upper()
reference = None if name is None else self.element.find_child_reference(name)
child_ref, child_name = (None, None) if reference is None else (reference['ref'], reference['name'])
if isinstance(value, basestring): # if the value is a basestring, parse it
child = self.element.parse_child(value, child_name=child_name, reference=child_ref)
elif isinstance(value, Element): # it is already an instance of Element
child = value
elif isinstance(value, BaseDataType):
child = self.create_element(name, False, reference)
child.value = value
else:
raise ChildNotValid(value, child_name)
if child.name != child_name: # e.g. message.pid = Segment('SPM') is forbidden
raise ChildNotValid(value, child_name)
child_to_remove = self.child_at_index(child_name, index)
if child_to_remove is None:
self.append(child)
else:
self.replace_child(child_to_remove, child)
# a set has been called, change the temporary parent to be the actual one
self.element.set_parent_to_traversal()
def find_child_reference(self, name):
name = name.upper()
if isinstance(self.structure_by_name, MutableMapping):
element = self.structure_by_name.get(name) or self.structure_by_longname.get(name)
else:
element = None
if element is None: # not found in self.structure
element = find_reference(name, self.child_classes.values(), self.version)
if element is None:
raise ChildNotFound(name)
# it means that the child exists but it's not valid for the Element (e.g. Field('pid_3').ce_1)
if self.structure_by_name is not None:
raise ChildNotValid(name, self)
return element
"""
name = name.upper()
element = self.structure_by_name.get(name, None) or self.structure_by_longname.get(name, None)
if element is None: # not found in self.structure
if self.allow_infinite_children and _valid_child_name(name, self.name):
if _valid_z_field_name(name):
datatype = 'ST'
else:
datatype = 'varies'
element = {'cls': Field, 'name': name, 'ref': ('leaf', None, datatype, None, None, -1)}
else:
element = find_reference(name, self.child_classes.values(), self.version)
if element:
raise ChildNotValid(name, self)
else:
raise ChildNotFound(name)
return element
if self.element._is_valid_child(child):
if child.parent != self.element and child.traversal_parent != self.element: # avoid infinite recursion
child.parent = self.element
else:
# if validation is strict, check the child cardinality
if Validator.is_strict(self.element.validation_level):
min_rep, max_rep = self.element.repetitions.get(child.name, (0, -1))
if len(self.indexes.get(child.name, [])) + 1 > int(max_rep) and max_rep > -1:
raise MaxChildLimitReached(self.element, child, max_rep)
if self.element.validation_level != child.validation_level:
raise OperationNotAllowed('Cannot add a child with a different validation_level')
if self.element.version != child.version:
raise OperationNotAllowed('Cannot add a child with a different HL7 version')
return True
else:
raise ChildNotValid(child, self.element)
return False
def find_child_reference(self, name):
name = name.upper()
if isinstance(self.structure_by_name, MutableMapping):
element = self.structure_by_name.get(name) or self.structure_by_longname.get(name)
else:
element = None
if element is None: # not found in self.structure
if _valid_z_segment_name(name):
element = {'cls': Segment, 'name': name, 'ref': ('sequence', ())}
else:
element = find_reference(name, self.child_classes.values(), self.version)
if Validator.is_strict(self.validation_level): # cannot be created if validation is strict
raise ChildNotValid(name, self)
return element
name = name.upper()
reference = None if name is None else self.element.find_child_reference(name)
child_ref, child_name = (None, None) if reference is None else (reference['ref'], reference['name'])
if isinstance(value, basestring): # if the value is a basestring, parse it
child = self.element.parse_child(value, child_name=child_name, reference=child_ref)
elif isinstance(value, Element): # it is already an instance of Element
child = value
elif isinstance(value, BaseDataType):
child = self.create_element(name, False, reference)
child.value = value
else:
raise ChildNotValid(value, child_name)
if child.name != child_name: # e.g. message.pid = Segment('SPM') is forbidden
raise ChildNotValid(value, child_name)
child_to_remove = self.child_at_index(child_name, index)
if child_to_remove is None:
self.append(child)
else:
self.replace_child(child_to_remove, child)
# a set has been called, change the temporary parent to be the actual one
self.element.set_parent_to_traversal()
>>> c.add(s)
>>> c.add(s2)
>>> print(c.to_er7())
EXAMPLE_ID&&&ALT_ID
"""
# base datatype components can't have more than one child
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)