text
stringlengths
0
828
>>> m.head.to_str()
'<head><a>A</a><a>B</a><a>C</a></head>'
Literals:
>>> m.head.leaf = 'A'
>>> m.head.leaf.get()
'A'
""""""
try:
# Searches for a node to assign to.
element = next(self._xml.iterchildren(tag=name))
except StopIteration:
# There is no such node in the XML tree. We create a new one
# with current root as parent (self._xml).
element = etree.SubElement(self._xml, name)
if isinstance(value, dict):
self.assign_dict(element, value)
elif isinstance(value, (list, tuple, set)):
self.assign_sequence_or_set(element, value)
else:
# Literal value.
self.assign_literal(element, value)
# Clear the aliases.
self._aliases = None"
4247,"def assign_dict(self, node, xml_dict):
""""""Assigns a Python dict to a ``lxml`` node.
:param node: A node to assign the dict to.
:param xml_dict: The dict with attributes/children to use.
""""""
new_node = etree.Element(node.tag)
# Replaces the previous node with the new one
self._xml.replace(node, new_node)
# Copies #text and @attrs from the xml_dict
helpers.dict_to_etree(xml_dict, new_node)"
4248,"def assign_literal(element, value):
u""""""Assigns a literal.
If a given node doesn't exist, it will be created.
:param etree.Element element: element to which we assign.
:param value: the value to assign
""""""
# Searches for a conversion method specific to the type of value.
helper = helpers.CAST_DICT.get(type(value), str)
# Removes all children and attributes.
element.clear()
element.text = helper(value)"
4249,"def to_dict(self, **kw):
u""""""Converts the lxml object to a dict.
possible kwargs:
without_comments: bool
""""""
_, value = helpers.etree_to_dict(self._xml, **kw).popitem()
return value"
4250,"def _get_aliases(self):
u""""""Creates a dict with aliases.
The key is a normalized tagname, value the original tagname.
""""""
if self._aliases is None:
self._aliases = {}
if self._xml is not None:
for child in self._xml.iterchildren():
self._aliases[helpers.normalize_tag(child.tag)] = child.tag
return self._aliases"
4251,"def xpath(
self,
path,
namespaces=None,
regexp=False,
smart_strings=True,
single_use=False,
):
u""""""Executes XPath query on the ``lxml`` object and returns a correct object.
:param str path: XPath string e.g., 'cars'/'car'
:param str/dict namespaces: e.g., 'exslt', 're' or
``{'re': ""http://exslt.org/regular-expressions""}``
:param bool regexp: if ``True`` and no namespaces is provided, it will use
``exslt`` namespace
:param bool smart_strings:
:param bool single_use: faster method for using only once. Does not
create ``XPathEvaluator`` instance.
>>> root = mappet.Mappet(""<root><a>aB</a><b>aBc</b></root>"")
>>> root.XPath(
""//*[re:test(., '^abc$', 'i')]"",
namespaces='exslt',
regexp=True,
)