text
stringlengths
0
828
return default_value"
4033,"def get_element_text(parent_to_parse, element_path=None, default_value=u''):
""""""
:return: text from the parsed parent element if it has a text value,
otherwise the default value.
:see: get_element(parent_to_parse, element_path)
""""""
parent_element = get_element(parent_to_parse, element_path)
if parent_element is None:
return default_value
if parent_element.text:
return parent_element.text.strip() or default_value
return default_value"
4034,"def get_elements_attributes(parent_to_parse, element_path=None, attrib_name=None):
""""""
:return: list of text representing an attribute of parent or each element at element path,
or a list of dicts representing all the attributes parsed from each element
""""""
attrs = _get_elements_property(parent_to_parse, element_path, 'attrib')
if not attrib_name:
return attrs
return [attr[attrib_name] for attr in attrs if attrib_name in attr]"
4035,"def _get_elements_property(parent_to_parse, element_path, prop_name):
"""""" A helper to construct a list of values from """"""
parent_element = get_element(parent_to_parse)
if parent_element is None:
return []
if element_path and not element_exists(parent_element, element_path):
return []
if not element_path:
texts = getattr(parent_element, prop_name)
texts = texts.strip() if isinstance(texts, string_types) else texts
texts = [texts] if texts else []
else:
texts = [t for t in (
prop.strip() if isinstance(prop, string_types) else prop
for prop in (getattr(node, prop_name) for node in parent_element.findall(element_path)) if prop
) if t]
return texts"
4036,"def set_element_tail(parent_to_parse, element_path=None, element_tail=u''):
""""""
Assigns the text following the parsed parent element and then returns it.
If element_path is provided and doesn't exist, it is inserted with element_tail.
:see: get_element(parent_to_parse, element_path)
""""""
return _set_element_property(parent_to_parse, element_path, _ELEM_TAIL, element_tail)"
4037,"def set_element_text(parent_to_parse, element_path=None, element_text=u''):
""""""
Assigns a string value to the parsed parent element and then returns it.
If element_path is provided and doesn't exist, it is inserted with element_text.
:see: get_element(parent_to_parse, element_path)
""""""
return _set_element_property(parent_to_parse, element_path, _ELEM_TEXT, element_text)"
4038,"def _set_element_property(parent_to_parse, element_path, prop_name, value):
"""""" Assigns the value to the parsed parent element and then returns it """"""
element = get_element(parent_to_parse)
if element is None:
return None
if element_path and not element_exists(element, element_path):
element = insert_element(element, 0, element_path)
if not isinstance(value, string_types):
value = u''
setattr(element, prop_name, value)
return element"
4039,"def set_elements_tail(parent_to_parse, element_path=None, tail_values=None):
""""""
Assigns an array of tail values to each of the elements parsed from the parent. The
tail values are assigned in the same order they are provided.
If there are less values then elements, the remaining elements are skipped; but if
there are more, new elements will be inserted for each with the remaining tail values.
""""""
if tail_values is None:
tail_values = []
return _set_elements_property(parent_to_parse, element_path, _ELEM_TAIL, tail_values)"
4040,"def set_elements_text(parent_to_parse, element_path=None, text_values=None):
""""""
Assigns an array of text values to each of the elements parsed from the parent. The
text values are assigned in the same order they are provided.