API¶
Module contents¶
-
class
mappet.Mappet(xml)[source]¶ Bases:
mappet.mappet.NodeA node that may have children.
-
assign_literal(element, tag_name, value)[source]¶ Assigns a literal.
If a given node doesn’t exist, it will be created.
Parameters: - element (etree.Element) – element to which we assign.
- tag_name (string) – element name, if it itself doesn’t exists.
- value – the value to assign
-
children(key=None, exact=False)[source]¶ Returns node’s children.
Parameters: - key – A key for filtering children by tagname.
- exact – A flag to disable searching among aliases.
-
create(tag, value)[source]¶ 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`.
-
iter_children(key=None, exact=False)[source]¶ Iterates over children.
Parameters: - key – A key for filtering children by tagname.
- exact – A flag to disable searching among aliases.
-
set(name, value)[source]¶ 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’] >>> 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’
-
sget(path, default=NoneNode)[source]¶ 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’) NoneNode
Accessing nonexistent path returns None-like object with mocked converting functions which returns None: >>> m.sget(‘reply.fake_node’).to_dict() is None True
-
mappet.mappet module¶
Module for dynamic mapping of XML trees to Python objects.
-
class
mappet.mappet.Literal(xml)[source]¶ Bases:
mappet.mappet.NodeRepresents a leaf in an XML tree.
-
class
mappet.mappet.Mappet(xml)[source]¶ Bases:
mappet.mappet.NodeA node that may have children.
-
assign_literal(element, tag_name, value)[source]¶ Assigns a literal.
If a given node doesn’t exist, it will be created.
Parameters: - element (etree.Element) – element to which we assign.
- tag_name (string) – element name, if it itself doesn’t exists.
- value – the value to assign
-
children(key=None, exact=False)[source]¶ Returns node’s children.
Parameters: - key – A key for filtering children by tagname.
- exact – A flag to disable searching among aliases.
-
create(tag, value)[source]¶ 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`.
-
iter_children(key=None, exact=False)[source]¶ Iterates over children.
Parameters: - key – A key for filtering children by tagname.
- exact – A flag to disable searching among aliases.
-
set(name, value)[source]¶ 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’] >>> 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’
-
sget(path, default=NoneNode)[source]¶ 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’) NoneNode
Accessing nonexistent path returns None-like object with mocked converting functions which returns None: >>> m.sget(‘reply.fake_node’).to_dict() is None True
-
-
class
mappet.mappet.Node(xml)[source]¶ Bases:
objectBase class representing an XML node.
-
getattr(key, default=None, callback=None)[source]¶ Getting the attribute of an element.
>>> xml = etree.Element('root') >>> xml.text = 'text' >>> Node(xml).getattr('text') 'text' >>> Node(xml).getattr('text', callback=str.upper) 'TEXT' >>> Node(xml).getattr('wrong_attr', default='default') 'default'
-
mappet.helpers module¶
Helper functions.
-
mappet.helpers.normalize_tag(tag)[source]¶ Normalizes tag name.
>>> normalize_tag('tag-NaMe') 'tag_name'
-
mappet.helpers.etree_to_dict(t)[source]¶ Converts an lxml.etree object to Python dict.
>>> etree_to_dict(etree.Element('root')) {'root': None}
Parameters: t (etree.Element) – lxml tree to convert Returns d: a dict representing the lxml tree tReturn type: dict
-
mappet.helpers.dict_to_etree(d, root)[source]¶ Converts a dict to lxml.etree object.
>>> dict_to_etree({'root': {'#text': 'node_text', '@attr': 'val'}}, etree.Element('root')) '<root><root attr="val">node_text</root></root>'
Parameters: - d (dict) – dict representing the XML tree
- root (etree.Element) – XML node which will be assigned the resulting tree
Returns: Textual representation of the XML tree
Return type: