pymantic.primitives

This module provides a Python implementation of the RDF Interfaces defined by the W3C. Also extends that API in places to allow for Datasets and Quads. The goal is to provide a simple API for working directly with Triples, and RDF Terms.

Data Structures

Warning

Currently Pyamntic does NOT restrict data structures to the RDF data model. For example Literals are allowed in the subject and predicate position, seralizing these is imposible

class pymantic.primitives.Triple(subject, predicate, object)[source]

The Triple interface represents an RDF Triple. The stringification of a Triple results in an N-Triples.

class pymantic.primitives.Graph(graph_uri=None)[source]

A Graph holds a set of one or more Triple. Implements the Python set/sequence API for in, for, and len

add(triple)[source]

Adds the specified Triple to the graph. This method returns the graph instance it was called on.

addAll(graph_or_triples)[source]

Imports the graph or set of triples in to this graph. This method returns the graph instance it was called on.

match(subject=None, predicate=None, object=None)[source]

This method returns a new sequence of triples which is comprised of all those triples in the current instance which match the given arguments, that is, for each triple in this graph, it is included in the output graph, if:

  • calling triple.subject.equals with the specified subject as an argument returns true, or the subject argument is null, AND
  • calling triple.property.equals with the specified property as an argument returns true, or the property argument is null, AND
  • calling triple.object.equals with the specified object as an argument returns true, or the object argument is null

This method implements AND functionality, so only triples matching all of the given non-null arguments will be included in the result.

merge(graph)[source]

Returns a new Graph which is a concatenation of this graph and the graph given as an argument.

objects()[source]

Returns an iterator over objects in the graph.

predicates()[source]

Returns an iterator over predicates in the graph.

remove(triple)[source]

Removes the specified Triple from the graph. This method returns the graph instance it was called on.

removeMatches(subject, predicate, object)[source]

This method removes those triples in the current graph which match the given arguments.

subjects()[source]

Returns an iterator over subjects in the graph.

toArray()[source]

Return the set of Triple within the Graph

uri

URI name of the graph, if it has been given a name

Non-RDF Interfaces Classes

class pymantic.primitives.Quad(subject, predicate, object, graph)[source]
class pymantic.primitives.Dataset[source]
addAll(dataset_or_quads)[source]

Imports the graph or set of triples in to this graph. This method returns the graph instance it was called on.

removeMatches(subject=None, predicate=None, object=None, graph=None)[source]

This method removes those triples in the current graph which match the given arguments.

RDF Terms

class pymantic.primitives.Literal(`value`, `language`, `datatype`)[source]

Literals represent values such as numbers, dates and strings in RDF data. A Literal is comprised of three attributes:

  • a lexical representation of the nominalValue
  • an optional language represented by a string token
  • an optional datatype specified by a NamedNode

Literals representing plain text in a natural language may have a language attribute specified by a text string token, as specified in [BCP47], normalized to lowercase (e.g., ‘en’, ‘fr’, ‘en-gb’).

Literals may not have both a datatype and a language.

class pymantic.primitives.NamedNode[source]

A node identified by an IRI.

class pymantic.primitives.BlankNode[source]

A BlankNode is a reference to an unnamed resource (one for which an IRI is not known), and may be used in a Triple as a unique reference to that unnamed resource.

BlankNodes are stringified by prepending “_:” to a unique value, for instance _:b142 or _:me, this stringified form is referred to as a “blank node identifier”.

RDF Enviroment Interfaces

class pymantic.primitives.RDFEnvironment(prefixes=None, terms=None)[source]

Bases: pymantic.primitives.Profile

The RDF Environment is an interface which exposes a high level API for working with RDF in a programming environment.

createAction(test, action)[source]
createBlankNode()[source]

Creates a new BlankNode.

createDataset(quads=())[source]
createGraph(triples=())[source]

Creates a new Graph, an optional sequence of Triple to include within the graph may be specified, this allows easy transition between native sequences and Graphs and is the counterpart for Graph.toArray().

createLiteral(value, language=None, datatype=None)[source]

Creates a Literal given a value, an optional language and/or an optional datatype.

createNamedNode(value)[source]

Creates a new NamedNode.

createPrefixMap(empty=False)[source]
createProfile(empty=False)[source]
createQuad(subject, predicate, object, graph)[source]
createTermMap(empty=False)[source]
createTriple(subject, predicate, object)[source]

Creates a Triple given a subject, predicate and object.

class pymantic.primitives.PrefixMap(**kwds)[source]

A map of prefixes to IRIs, and provides methods to turn one in to the other.

Example Usage:

>>> prefixes = PrefixMap()

Create a new prefix mapping for the prefix “rdfs”

>>> prefixes['rdfs'] = "http://www.w3.org/2000/01/rdf-schema#"

Resolve a known CURIE

>>> prefixes.resolve("rdfs:label")
u"http://www.w3.org/2000/01/rdf-schema#label"

Shrink an IRI for a known CURIE in to a CURIE

>>> prefixes.shrink("http://www.w3.org/2000/01/rdf-schema#label")
u"rdfs:label"

Attempt to resolve a CURIE with an empty prefix

>>> prefixes.resolve(":me")
":me"

Set the default prefix and attempt to resolve a CURIE with an empty prefix

>>> prefixes.setDefault("http://example.org/bob#")
>>> prefixes.resolve(":me")
u"http://example.org/bob#me"
resolve(curie)[source]

Given a valid CURIE for which a prefix is known (for example “rdfs:label”), this method will return the resulting IRI (for example “http://www.w3.org/2000/01/rdf-schema#label”)

setDefault(iri)[source]

Set the iri to be used when resolving CURIEs without a prefix, for example “:this”.

shrink(iri)[source]

Given an IRI for which a prefix is known (for example “http://www.w3.org/2000/01/rdf-schema#label”) this method returns a CURIE (for example “rdfs:label”), if no prefix is known the original IRI is returned.

class pymantic.primitives.TermMap[source]
A map of simple string terms to IRIs, and provides methods to turn one
in to the other.

Example usage:

>>> terms = TermMap()

Create a new term mapping for the term “member”

>>> terms['member'] = "http://www.w3.org/ns/org#member"

Resolve a known term to an IRI

>>> terms.resolve("member")
u"http://www.w3.org/ns/org#member"

Shrink an IRI for a known term to a term

>>> terms.shrink("http://www.w3.org/ns/org#member")
u"member"

Attempt to resolve an unknown term

>>> terms.resolve("label")
None

Set the default term vocabulary and then attempt to resolve an unknown term

>>> terms.setDefault("http://www.w3.org/2000/01/rdf-schema#")
>>> terms.resolve("label")
u"http://www.w3.org/2000/01/rdf-schema#label"
resolve(term)[source]

Given a valid term for which an IRI is known (for example “label”), this method will return the resulting IRI (for example “http://www.w3.org/2000/01/rdf-schema#label”).

If no term is known and a default has been set, the IRI is obtained by concatenating the term and the default iri.

If no term is known and no default is set, then this method returns null.

setDefault(iri)[source]

The default iri to be used when an term cannot be resolved, the resulting IRI is obtained by concatenating this iri with the term being resolved.

shrink(iri)[source]

Given an IRI for which an term is known (for example “http://www.w3.org/2000/01/rdf-schema#label”) this method returns a term (for example “label”), if no term is known the original IRI is returned.

class pymantic.primitives.Profile(prefixes=None, terms=None)[source]

Profiles provide an easy to use context for negotiating between CURIEs, Terms and IRIs.

importProfile(profile, override=False)[source]

This method functions the same as calling prefixes.addAll(profile.prefixes, override) and terms.addAll(profile.terms, override), and allows easy updating and merging of different profiles.

This method returns the instance on which it was called.

resolve(toresolve)[source]

Given an Term or CURIE this method will return an IRI, or null if it cannot be resolved.

If toresolve contains a : (colon) then this method returns the result of calling prefixes.resolve(toresolve)

otherwise this method returns the result of calling terms.resolve(toresolve)

setDefaultPrefix(iri)[source]

This method sets the default prefix for use when resolving CURIEs without a prefix, for example “:me”, it is identical to calling the setDefault method on prefixes.

setDefaultVocabulary(iri)[source]

This method sets the default vocabulary for use when resolving unknown terms, it is identical to calling the setDefault method on terms.

setPrefix(prefix, iri)[source]

This method associates an IRI with a prefix, it is identical to calling the set method on prefixes.

setTerm(term, iri)[source]

This method associates an IRI with a term, it is identical to calling the set method on term.

Helper Functions

pymantic.primitives.is_language(lang)[source]

Is something a valid XML language?

pymantic.primitives.lang_match(lang1, lang2)[source]

Determines if two languages are, in fact, the same language.

Eg: en is the same as en-us and en-uk.

pymantic.primitives.parse_curie(curie, prefixes)[source]

Parses a CURIE within the context of the given namespaces. Will also accept explicit URIs and wrap them in an rdflib URIRef.

Specifically:

  1. If the CURIE is not of the form [stuff] and the prefix is in the list of standard URIs, it is wrapped in a URIRef and returned unchanged.
  2. Otherwise, the CURIE is parsed by the rules of CURIE Syntax 1.0: http://www.w3.org/TR/2007/WD-curie-20070307/ The default namespace is the namespace keyed by the empty string in the namespaces dictionary.
  3. If the CURIE’s namespace cannot be resolved, a ValueError is raised.
pymantic.primitives.parse_curies(curies, namespaces)[source]

Parse multiple CURIEs at once.

pymantic.primitives.to_curie(uri, namespaces, seperator=':', explicit=False)[source]

Converts a URI to a CURIE using the prefixes defined in namespaces. If there is no matching prefix, return the URI unchanged.

namespaces - a dictionary of prefix -> namespace mappings.

separator - the character to use as the separator between the prefix and
the local name.
explicit - if True and the URI can be abbreviated, wrap the abbreviated
form in []s to indicate that it is definitely a CURIE.