Skip to content

utils

extract_xml_tags(xml_tag_name, node, *, allow_none=True)

extract_xml_tags(xml_tag_name: str, node: Mapping[str, Any], *, allow_none: Literal[True] = ...) -> Any | None
extract_xml_tags(xml_tag_name: str, node: Mapping[str, Any], *, allow_none: Literal[False]) -> Any

Helper to extract xml tags from xmltodict.

Parameters:

Name Type Description Default
xml_tag_name str

Name of the xml tag to extract from the node.

required
node Mapping[str, Any]

Node object returned by xmltodict from which xml_tag_name should be extracted.

required
allow_none bool

If False, the tag needs to exist in the node. Will raise a ValueError if it does not.

True

Returns:

Type Description
object
Source code in openml/utils.py
def extract_xml_tags(
    xml_tag_name: str,
    node: Mapping[str, Any],
    *,
    allow_none: bool = True,
) -> Any | None:
    """Helper to extract xml tags from xmltodict.

    Parameters
    ----------
    xml_tag_name : str
        Name of the xml tag to extract from the node.

    node : Mapping[str, Any]
        Node object returned by ``xmltodict`` from which ``xml_tag_name``
        should be extracted.

    allow_none : bool
        If ``False``, the tag needs to exist in the node. Will raise a
        ``ValueError`` if it does not.

    Returns
    -------
    object
    """
    if xml_tag_name in node and node[xml_tag_name] is not None:
        if isinstance(node[xml_tag_name], (dict, str)):
            return [node[xml_tag_name]]
        if isinstance(node[xml_tag_name], list):
            return node[xml_tag_name]

        raise ValueError("Received not string and non list as tag item")

    if allow_none:
        return None

    raise ValueError(f"Could not find tag '{xml_tag_name}' in node '{node!s}'")