prov.serializers package

Module contents

class prov.serializers.Registry[source]

Bases: object

Registry of serializers.

static load_serializers() None[source]

Loads all available serializers into the registry.

serializers: dict[str, type[Serializer]] = None

Property caching all available serializers in a dict.

class prov.serializers.Serializer(document: ProvDocument | None = None)[source]

Bases: ABC

Serializer for PROV documents.

abstractmethod deserialize(stream: io.IOBase, **args: Any) ProvDocument[source]

Abstract method for deserializing.

Parameters:

stream – Stream object to deserialize the document from.

document = None

PROV document to serialise.

abstractmethod serialize(stream: IOBase, **args: Any) None[source]

Abstract method for serializing.

Parameters:

stream – Stream object to serialize the document into.

prov.serializers.get(format_name: str) type[Serializer][source]

Returns the serializer class for the specified format. Raises a DoNotExist

prov.serializers.provjson module

class prov.serializers.provjson.ProvJSONDecoder(*, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=None)[source]

Bases: JSONDecoder

decode(s: str, *args: Any, **kwargs: Any) Any[source]

Return the Python representation of s (a str instance containing a JSON document).

class prov.serializers.provjson.ProvJSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

default(o: Any) Any[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
exception prov.serializers.provjson.ProvJSONException[source]

Bases: Error

class prov.serializers.provjson.ProvJSONSerializer(document: ProvDocument | None = None)[source]

Bases: Serializer

PROV-JSON serializer for ProvDocument

deserialize(stream: IOBase, **args: Any) ProvDocument[source]

Deserialize from the PROV JSON representation to a ProvDocument instance.

Parameters:

stream – Input data.

serialize(stream: IOBase, **args: Any) None[source]

Serializes a ProvDocument instance to PROV-JSON.

Parameters:

stream – Where to save the output.

prov.serializers.provn module

class prov.serializers.provn.ProvNSerializer(document: ProvDocument | None = None)[source]

Bases: Serializer

PROV-N serializer for ProvDocument

deserialize(stream: IOBase, **args: Any) ProvDocument[source]

Abstract method for deserializing.

Parameters:

stream – Stream object to deserialize the document from.

serialize(stream: IOBase, **args: Any) None[source]

Serializes a prov.model.ProvDocument instance to a PROV-N.

Parameters:

stream – Where to save the output.

prov.serializers.provrdf module

PROV-RDF serializers for ProvDocument

exception prov.serializers.provrdf.ProvRDFException[source]

Bases: Error

class prov.serializers.provrdf.ProvRDFSerializer(document: ProvDocument | None = None)[source]

Bases: Serializer

PROV-O serializer for ProvDocument

deserialize(stream: ~io.IOBase, rdf_format: str = 'trig', relation_mapper: dict[~rdflib.term.URIRef, str] = {rdflib.term.URIRef('http://www.w3.org/ns/prov#actedOnBehalfOf'): 'delegation', rdflib.term.URIRef('http://www.w3.org/ns/prov#alternateOf'): 'alternate', rdflib.term.URIRef('http://www.w3.org/ns/prov#hadMember'): 'membership', rdflib.term.URIRef('http://www.w3.org/ns/prov#mentionOf'): 'mention', rdflib.term.URIRef('http://www.w3.org/ns/prov#specializationOf'): 'specialization', rdflib.term.URIRef('http://www.w3.org/ns/prov#used'): 'usage', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasAssociatedWith'): 'association', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasAttributedTo'): 'attribution', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasDerivedFrom'): 'derivation', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasEndedBy'): 'end', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasGeneratedBy'): 'generation', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasInfluencedBy'): 'influence', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasInformedBy'): 'communication', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasInvalidatedBy'): 'invalidation', rdflib.term.URIRef('http://www.w3.org/ns/prov#wasStartedBy'): 'start'}, predicate_mapper: dict[~rdflib.term.URIRef, ~prov.identifier.QualifiedName] = {rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#label'): <QualifiedName: prov:label>, rdflib.term.URIRef('http://www.w3.org/ns/prov#atLocation'): <QualifiedName: prov:location>, rdflib.term.URIRef('http://www.w3.org/ns/prov#atTime'): <QualifiedName: prov:time>, rdflib.term.URIRef('http://www.w3.org/ns/prov#endedAtTime'): <QualifiedName: prov:endTime>, rdflib.term.URIRef('http://www.w3.org/ns/prov#hadActivity'): <QualifiedName: prov:activity>, rdflib.term.URIRef('http://www.w3.org/ns/prov#hadGeneration'): <QualifiedName: prov:generation>, rdflib.term.URIRef('http://www.w3.org/ns/prov#hadPlan'): <QualifiedName: prov:plan>, rdflib.term.URIRef('http://www.w3.org/ns/prov#hadRole'): <QualifiedName: prov:role>, rdflib.term.URIRef('http://www.w3.org/ns/prov#hadUsage'): <QualifiedName: prov:usage>, rdflib.term.URIRef('http://www.w3.org/ns/prov#startedAtTime'): <QualifiedName: prov:startTime>}, **kwargs: ~typing.Any) ProvDocument[source]

Deserialize from the PROV-O representation to a ProvDocument instance.

Parameters:
  • stream – Input data.

  • rdf_format – The RDF format of the input data, default: TRiG.

serialize(stream: ~io.IOBase, rdf_format: str = 'trig', PROV_N_MAP: dict[~prov.identifier.QualifiedName, str] = {<QualifiedName: prov:Activity>: 'activity', <QualifiedName: prov:Agent>: 'agent', <QualifiedName: prov:Alternate>: 'alternateOf', <QualifiedName: prov:Association>: 'wasAssociatedWith', <QualifiedName: prov:Attribution>: 'wasAttributedTo', <QualifiedName: prov:Bundle>: 'bundle', <QualifiedName: prov:Communication>: 'wasInformedBy', <QualifiedName: prov:Delegation>: 'actedOnBehalfOf', <QualifiedName: prov:Derivation>: 'wasDerivedFrom', <QualifiedName: prov:End>: 'wasEndedBy', <QualifiedName: prov:Entity>: 'entity', <QualifiedName: prov:Generation>: 'wasGeneratedBy', <QualifiedName: prov:Influence>: 'wasInfluencedBy', <QualifiedName: prov:Invalidation>: 'wasInvalidatedBy', <QualifiedName: prov:Membership>: 'hadMember', <QualifiedName: prov:Mention>: 'mentionOf', <QualifiedName: prov:Specialization>: 'specializationOf', <QualifiedName: prov:Start>: 'wasStartedBy', <QualifiedName: prov:Usage>: 'used'}, **kwargs: ~typing.Any) None[source]

Serializes a ProvDocument instance to PROV-O.

Parameters:
  • stream – Where to save the output.

  • rdf_format – The RDF format of the output, default to TRiG.

prov.serializers.provrdf.walk(children: list, level: int = 0, path: dict = None, usename: bool = True) Generator[dict, None, None][source]

Generate all the full paths in a tree, as a dict.

Example:

>>> from prov.serializers.provrdf import walk
>>> iterables = [('a', lambda: [1, 2]), ('b', lambda: [3, 4])]
>>> [val['a'] for val in walk(iterables)]
[1, 1, 2, 2]
>>> [val['b'] for val in walk(iterables)]
[3, 4, 3, 4]

prov.serializers.provxml module

exception prov.serializers.provxml.ProvXMLException[source]

Bases: Error

class prov.serializers.provxml.ProvXMLSerializer(document: ProvDocument | None = None)[source]

Bases: Serializer

PROV-XML serializer for ProvDocument

deserialize(stream: IOBase, **kwargs: Any) ProvDocument[source]

Deserialize from PROV-XML representation to a ProvDocument instance.

Parameters:

stream – Input data.

deserialize_subtree(xml_doc: _Element, bundle: ProvDocument | ProvBundle) ProvDocument | ProvBundle[source]

Deserialize an etree element containing a PROV document or a bundle and write it to the provided internal object.

Parameters:
  • xml_doc – An etree element containing the information to read.

  • bundle – The bundle object to write to.

serialize(stream: IOBase, force_types: bool = False, **kwargs: Any) None[source]

Serializes a ProvDocument instance to PROV-XML.

Parameters:
  • stream – Where to save the output.

  • force_types (boolean, optional) – Will force xsd:types to be written for most attributes mainly PROV-“attributes”, e.g. tags not in the PROV namespace. Off by default meaning xsd:type attributes will only be set for prov:type, prov:location, and prov:value as is done in the official PROV-XML specification. Furthermore the types will always be set if the Python type requires it. False is a good default and it should rarely require changing.

serialize_bundle(bundle: ProvBundle, element: _Element | None = None, force_types: bool = False) _Element[source]

Serializes a bundle or document to PROV XML.

Parameters:
  • bundle – The bundle or document.

  • element – The XML element to write to. Will be created if None.

  • force_types (boolean, optional) – Will force xsd:types to be written for most attributes mainly PROV-“attributes”, e.g. tags not in the PROV namespace. Off by default meaning xsd:type attributes will only be set for prov:type, prov:location, and prov:value as is done in the official PROV-XML specification. Furthermore the types will always be set if the Python type requires it. False is a good default and it should rarely require changing.