.. automodule:: gemma
.. automodule:: gemma.extensions.xml
.. _extension-xml:
Extension: XML
==============
The xml extension supplies classes for interacting with data from
``xml.etree.ElementTree``
Examples will share a ``root`` data object. To load, copy and paste the following: ::
from xml.etree.ElementTree import fromstring
xml_raw = (
''
''
''
'one'
'two'
'three'
''
''
)
root = fromstring(xml_raw)
XElm Bearing Type
-----------------
.. autoclass:: XElm
:special-members: __init__
:members:
XCourse
-------
Simple subclass of :class:`Course` that adds :class:`XElm` to the list of auto-cast
functions.
Like :class:`Course` / ``gemma.PORT``, :class:`XCourse` has a shorter, initialized alias
called ``XPATH``.
.. autoclass:: XCourse
:members:
Example :func:`XCourse.fetch`
>>> from gemma.extensions.xml import XCourse, XPath
>>>
>>> to_fetch = XCourse() / "" / ""
>>> fetched = to_fetch.fetch(root)
>>> fetched, fetched.text
(, 'three')
Example :func:`XCourse.place`
>>> to_place = XPath / "" / "" / "text"
>>> to_place.place(root, "changed value")
>>> print(tostring(root))
b'......changed value'
XCompass
--------
.. autoclass:: XCompass
Restricted compass for ``xml.etree.ElementTree.Element`` objects.
Returns all child elements as (:class:`XElm`), value pairs.
**Returned** :class:`Attr` **bearings:** ``.text``, ``.attrib`` by default.
:members:
Example
>>> from gemma.extensions.xml import XCompass
>>>
>>> for this_bearing, value in XCompass().bearings_iter(root):
... print(repr(this_bearing), value)
...
None
{}
xsurveyor
---------
``xsurveyor`` is an instance of :class:`Surveyor` with :class:`XCompass` added to its
compasses and :class:`XCourse` as its course type.
Example
>>> from gemma.extensions.xml import xsurveyor
>>>
>>> for course, value in xsurveyor.chart_iter(root):
... print(repr(course), value)
...
> None
> {}
>
/ > None
/ > {'one': '1', 'two': '2'}
/ / > 1
/ / > 2
>
/ > None
/ > {'three': '3', 'four': '4'}
/ / > 3
/ / > 4
/ >
/ / > one
/ / > {}
/ >
/ / > two
/ / > {}
/ >
/ / > three
/ / > {}
Cartography
-----------
Mapping is done through the regular :class:`Cartographer` class.
Example
>>> from gemma import Cartographer, Coordinate
>>> from gemma.extensions.xml import XPATH
>>>
... def explode_listed(listed_elements, coord, cache):
... return [x.text for x in listed_elements]
>>>
>>> coords = [
... Coordinate(org=XPATH / "a" / "attrib" / "one", dst=XPATH / "a.one"),
... Coordinate(org=XPATH / "a" / "attrib" / "two", dst=XPATH / "a.two"),
... Coordinate(org=XPATH / "b" / "attrib" / "three", dst=XPATH / "b.three"),
... Coordinate(org=XPATH / "b" / "attrib" / "four", dst=XPATH / "b.four"),
... Coordinate(
... org=XPATH / "b" / "",
... dst=XPATH / "b.listed",
... clean_value=explode_listed
... ),
... ]
>>>
>>> destination = dict()
>>>
>>> Cartographer().map(root, destination, coords)
>>> for key, value in destination.items():
... print(f"{key}: {value}")
...
a.one: 1
a.two: 2
b.three: 3
b.four: 4
b.listed: ['one', 'two', 'three']