text stringlengths 0 828 |
|---|
yield self.__class__(child) |
else: |
yield Literal(child)" |
4243,"def update(self, **kwargs): |
u""""""Updating or creation of new simple nodes. |
Each dict key is used as a tagname and value as text. |
"""""" |
for key, value in kwargs.items(): |
helper = helpers.CAST_DICT.get(type(value), str) |
tag = self._get_aliases().get(key, key) |
elements = list(self._xml.iterchildren(tag=tag)) |
if elements: |
for element in elements: |
element.text = helper(value) |
else: |
element = etree.Element(key) |
element.text = helper(value) |
self._xml.append(element) |
self._aliases = None" |
4244,"def sget(self, path, default=NONE_NODE): |
u""""""Enables access to nodes if one or more of them don't exist. |
Example: |
>>> m = Mappet('<root><tag attr1=""attr text"">text value</tag></root>') |
>>> m.sget('tag') |
text value |
>>> m.sget('tag.@attr1') |
'attr text' |
>>> m.sget('tag.#text') |
'text value' |
>>> m.sget('reply.vms_model_cars.car.0.params.doors') |
NONE_NODE |
Accessing nonexistent path returns None-like object with mocked |
converting functions which returns None: |
>>> m.sget('reply.fake_node').to_dict() is None |
True |
"""""" |
attrs = str(path).split(""."") |
text_or_attr = None |
last_attr = attrs[-1] |
# Case of getting text or attribute |
if last_attr == '#text' or last_attr.startswith('@'): |
# #text => text, @attr => attr |
text_or_attr = last_attr[1:] |
attrs = attrs[:-1] |
# When getting #text and @attr we want default value to be None. |
if default is NONE_NODE: |
default = None |
my_object = self |
for attr in attrs: |
try: |
if isinstance(my_object, (list, tuple)) and re.match('^\-?\d+$', attr): |
my_object_next = my_object[int(attr)] |
else: |
my_object_next = getattr(my_object, attr) |
my_object = my_object_next |
except (AttributeError, KeyError, IndexError): |
return default |
# Return #text or @attr |
if text_or_attr: |
try: |
return my_object.getattr(text_or_attr) |
except AttributeError: |
# myObject can be a list. |
return None |
else: |
return my_object" |
4245,"def create(self, tag, value): |
u""""""Creates a node, if it doesn't exist yet. |
Unlike attribute access, this allows to pass a node's name with hyphens. |
Those hyphens will be normalized automatically. |
In case the required element already exists, raises an exception. |
Updating/overwriting should be done using `update``. |
"""""" |
child_tags = {child.tag for child in self._xml} |
if tag in child_tags: |
raise KeyError('Node {} already exists in XML tree.'.format(tag)) |
self.set(tag, value)" |
4246,"def set(self, name, value): |
u""""""Assigns a new XML structure to the node. |
A literal value, dict or list can be passed in. Works for all nested levels. |
Dictionary: |
>>> m = Mappet('<root/>') |
>>> m.head = {'a': 'A', 'b': {'#text': 'B', '@attr': 'val'}} |
>>> m.head.to_str() |
'<head><a>A</a><b attr=""val"">B</b></head>' |
List: |
>>> m.head = [{'a': i} for i in 'ABC'] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.