pymantic.rdf

Provides common classes and functions for modelling an RDF graph using Python objects.

class pymantic.rdf.List(graph, subject)[source]

Convenience class for dealing with RDF lists.

Requires considerable use of as_, due to the utter lack of type information on said lists.

classmethod is_list(node, graph)[source]

Determine if a given node is plausibly the subject of a list element.

class pymantic.rdf.MetaResource[source]

Aggregates Prefix and scalar information.

class pymantic.rdf.Resource(graph, subject)[source]

Provides necessary context and utility methods for accessing a Resource in an RDF graph. Resources can be used as-is, but are likely somewhat unwieldy, since all predicate access must be by complete URL and produces sets. By subclassing Resource, you can take advantage of a number of quality-of-life features:

  1. Bind prefixes to prefixes, and refer to them using CURIEs when accessing predicates or explicitly resolving CURIEs. Store a dictionary mapping prefixes to URLs in the ‘prefixes’ attribute of your subclass. The prefixes dictionaries on all parents are merged with this dictionary, and those at the bottom are prioritized. The values in the dictionaries will automatically be turned into rdflib Prefix objects.

  2. Define predicates as scalars. This asserts that a given predicate on this resource will only have zero or one value for a given language or data-type, or one reference to another resource. This is done using the ‘scalars’ set, which is processed and merged just like prefixes.

  3. Automatically classify certain RDF types as certain Resource subclasses. Decorate your class with the pymantic.RDF.register_class decorator, and provide it with the corresponding RDF type. Whenever this type is encountered when retrieving objects from a predicate it will automatically be instantiated as your class rather than a generic Resource.

    RDF allows for resources to have multiple types. When a resource is encountered with two or more types that have different python classes registered for them, a new python class is created. This new class subclasses all applicable registered classes.

    If you want to perform this classification manually (to, for example, instantiate the correct class for an arbitrary URI), you can do so by calling Resource.classify. You can also create a new instance of a Resource by calling .new on a subclass.

Automatic retrieval of resources with no type information is currently implemented here, but is likely to be refactored into a separate persistence layer in the near future.

bare_literals(predicate)[source]

Objects for a predicate that are language-less, datatype-less Literals.

classmethod classify(graph, obj)[source]

Classify an object into an appropriate registered class, or Resource.

May create a new class if necessary that is a subclass of two or more registered Resource classes.

copy(target_subject)[source]

Create copies of all triples with this resource as their subject with the target subject as their subject. Returns a classified version of the target subject.

erase()[source]

Erase all tripes for this resource from the graph.

get_scalar(key)[source]

As __getitem__ access, but pretend the key is a scalar even if it isn’t.

Expect random exceptions if using this carelessly.

classmethod in_graph(graph)[source]

Iterate through all instances of this Resource in the graph.

is_a()[source]

Test to see if the subject of this resource has all the necessary RDF classes applied to it.

classmethod new(graph, subject=None)[source]

Add type information to the graph for a new instance of this Resource.

object_of(predicate=None)[source]

All subjects for which this resource is an object for the given predicate.

objects(predicate)[source]

All objects for a predicate.

objects_by_datatype(predicate, datatype=None)[source]

Objects for a predicate that match a specified datatype or, if datatype is None, have a datatype specified.

objects_by_lang(predicate, lang=None)[source]

Objects for a predicate that match a specified language or, if language is None, have a language specified.

objects_by_type(predicate, resource_class=None)[source]

Objects for a predicate that are instances of a particular Resource subclass or, if resource_class is none, are Resources.

classmethod resolve(key)[source]

Use this class’s prefixes to resolve a curie

exception pymantic.rdf.URLRetrievalError[source]

Raised when an attempt to retrieve a resource returns a status other than 200 OK.

pymantic.rdf.check_objects(graph, value, lang, datatype, rdf_class)[source]

Determine that value or the things in values are appropriate for the specified explicit object access key.

pymantic.rdf.literalize(graph, value, lang, datatype)[source]

Convert either a value or a sequence of values to either a Literal or a Resource.

pymantic.rdf.objectify_value(graph, value, lang=None, datatype=None)[source]

Convert a single value into either a Literal or a Resource.

pymantic.rdf.register_class(rdf_type)[source]

Register a class for automatic instantiation VIA Resource.classify.