text stringlengths 0 828 |
|---|
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 text values. |
"""""" |
if text_values is None: |
text_values = [] |
return _set_elements_property(parent_to_parse, element_path, _ELEM_TEXT, text_values)" |
4041,"def _set_elements_property(parent_to_parse, element_path, prop_name, values): |
"""""" |
Assigns an array of string values to each of the elements parsed from the parent. |
The values must be strings, and they are assigned in the same order they are provided. |
The operation stops when values run out; extra values will be inserted as new elements. |
:see: get_element(parent_to_parse, element_path) |
"""""" |
element = get_element(parent_to_parse) |
if element is None or not values: |
return [] |
if isinstance(values, string_types): |
values = [values] |
if not element_path: |
return [_set_element_property(element, None, prop_name, values[0])] |
elements = get_elements(element, element_path) |
affected = [] |
for idx, val in enumerate(values): |
if idx < len(elements): |
next_elem = elements[idx] |
else: |
next_elem = insert_element(element, idx, element_path) |
affected.append( |
_set_element_property(next_elem, None, prop_name, val) |
) |
return affected" |
4042,"def dict_to_element(element_as_dict): |
"""""" |
Converts a Dictionary object to an element. The Dictionary can |
include any of the following tags, only name is required: |
- name (required): the name of the element tag |
- text: the text contained by element |
- tail: text immediately following the element |
- attributes: a Dictionary containing element attributes |
- children: a List of converted child elements |
"""""" |
if element_as_dict is None: |
return None |
elif isinstance(element_as_dict, ElementTree): |
return element_as_dict.getroot() |
elif isinstance(element_as_dict, ElementType): |
return element_as_dict |
elif not isinstance(element_as_dict, dict): |
raise TypeError('Invalid element dict: {0}'.format(element_as_dict)) |
if len(element_as_dict) == 0: |
return None |
try: |
converted = Element( |
element_as_dict[_ELEM_NAME], |
element_as_dict.get(_ELEM_ATTRIBS, {}) |
) |
converted.tail = element_as_dict.get(_ELEM_TAIL, u'') |
converted.text = element_as_dict.get(_ELEM_TEXT, u'') |
for child in element_as_dict.get(_ELEM_CHILDREN, []): |
converted.append(dict_to_element(child)) |
except KeyError: |
raise SyntaxError('Invalid element dict: {0}'.format(element_as_dict)) |
return converted" |
4043,"def element_to_dict(elem_to_parse, element_path=None, recurse=True): |
"""""" |
:return: an element losslessly as a dictionary. If recurse is True, |
the element's children are included, otherwise they are omitted. |
The resulting Dictionary will have the following attributes: |
- name: the name of the element tag |
- text: the text contained by element |
- tail: text immediately following the element |
- attributes: a Dictionary containing element attributes |
- children: a List of converted child elements |
"""""" |
element = get_element(elem_to_parse, element_path) |
if element is not None: |
converted = { |
_ELEM_NAME: element.tag, |
_ELEM_TEXT: element.text, |
_ELEM_TAIL: element.tail, |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.