MivotUtils

class pyvo.mivot.utils.MivotUtils[source]

Bases: object

Some utilities making easier the transformation of Mivot elements into dictionary components. These dictionaries are used to generate MivotInstance objects

Methods Summary

as_literal(identifier)

Make sure the identifier will be interpreted as a literal (* prefix).

cast_type_value(value, dmtype)

Cast value to the Python type matching dmtype.

format_dmid(dmid)

Replace characters that could confuse XPath queries with '_'.

get_field_attributes(table, column_id)

get_ref_or_literal(value_or_ref)

Check if value_or_ref must be interpreted as a column reference or a literal.

populate_instance(property_instance, ...[, ...])

This function inserts in the property_instance all expected attributes.

xml_to_dict(element)

Recursively create a nested dictionary from the XML tree structure, preserving the hierarchy.

Methods Documentation

static as_literal(identifier)[source]

Make sure the identifier will be interpreted as a literal (* prefix). Literal are either non string values or strings starting with a *

Parameters:
identifier: str

column identifier or literal value

Returns:
str

identifier prefixes with a *

static cast_type_value(value, dmtype)[source]

Cast value to the Python type matching dmtype.

Parameters:
valuestr

value to cast

dmtypestr

model dmtype

Returns:
Union[bool, float, str, None]

The casted value or None

static format_dmid(dmid)[source]

Replace characters that could confuse XPath queries with ‘_’. This is not required by the MIVOT schema but this makes this API more robust

Returns:
str

formatted dmid

static get_field_attributes(table, column_id)[source]
Parameters:
tableastropy.table

Table (from parsed VOTable) of the mapped data

column_idstr

Identifier of the table column from which we want to get the unit

Returns:
unit, ref, literal
static get_ref_or_literal(value_or_ref)[source]

Check if value_or_ref must be interpreted as a column reference or a literal.

Returns:
(ref, literal)
static populate_instance(property_instance, class_name, mapping, table, dmtype, as_literals=False, package=None)[source]

This function inserts in the property_instance all expected attributes.

  • The structure of the class is supposed to be flat (only ATTRIBUTEs).

  • All attributes are meant to have the same dmtype.

  • The mapping is checked against the pyvo.mivot.glossary.Roles.

Parameters:
property_instancepyvo.mivot.writer.instance.MivotInstance

Mivot instance to populate with attributes

class_namestr

Name of the property_instance class (dmtype). Used to get all the attribute roles (given by the model) of the class

mappingdict

Dictionary associating model roles with their values.

tableastropy.table

Table (from parsed VOTable) of the mapped data

dmtypestring

common dmtype of object attributes

as_literalboolean, optional (default isTrue)

If True, all attribute are set with literal values (@value=”…”)

packagestr, optional (default as None)

Package name possibly prefixing dmroles

static xml_to_dict(element)[source]

Recursively create a nested dictionary from the XML tree structure, preserving the hierarchy. The processing of elements depends on the tag:

  • For INSTANCE, a new dictionary is created.

  • For COLLECTION, a list is created.

  • For ATTRIBUTE, a leaf structure is created in the tree structure with dmtype, dmrole, value, unit, and ref keys.

Parameters:
elementxml.etree.ElementTree.Element

The XML element to convert to a dictionary

Returns:
dict

The nested dictionary representing the XML tree structure.