Skip to main content

Utility library for the work with ontologies.

Project description

Ontolutils - Utilities for work with ontologies

Tests pyvers

Utility library for the work with ontologies. Provides two features:

  • namespaces
  • ontology wrapper classes

Namespaces

You may know namespaces from rdflib, which implements namespaces like PROV or FOAF:

import rdflib

print(rdflib.PROV.Agent)
# rdflib.URIRef('http://www.w3.org/ns/prov#Agent')

But not all. The package ontoutils let's you generate namespaces from other resources. Some of them are part of this package, like CODEMETA or M4I. Thus, you can do:

from ontolutils import M4I

print(M4I.Tool)
# rdflib.URIRef('http://w3id.org/nfdi4ing/metadata4ing#Tool')

Ontology wrapper classes

With the ontology wrapper classes, you can create objects of ontology classes, write them to a JSON-LD file and query them again. Below, an example is made with the prov ontology:

from pydantic import EmailStr

from ontolutils import Thing, namespaces, urirefs


@namespaces(prov="https://www.w3.org/ns/prov#",
            foaf="http://xmlns.com/foaf/0.1/")
@urirefs(Agent='prov:Agent',
         mbox='foaf:mbox')
class Agent(Thing):
    """Implementation of https://www.w3.org/ns/prov#Agent

    Parameters
    ----------
    mbox: EmailStr = None
        Email address (foaf:mbox)
    """
    mbox: EmailStr = None  # foaf:mbox


# usage:
agent = Agent(mbox="a@email.com")
print(agent.mbox)
# Agent(id=None, label=None, mbox='m@email.com')

with open("agent.json", "w") as f:
    f.write(agent.dump_jsonld())

with open("agent.json", "r") as f:
    found_agents = Agent.load_jsonld(f.read())
found_agent = found_agents[0]
print(found_agent.mbox)
# Agent(id=..., label=None, mbox='m@email.com')

Limitations

Some namespaces may be incomplete due to vocabularies with names that are reserved words in Python. E.g. for schema.org, "yield" or "True" will not be available like this:

from ontolutils import SCHEMA

SCHEMA.
yield  # AttributeError: 'SchemaOrg' object has no attribute 'yield'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ontolutils-0.1.0.tar.gz (618.6 kB view hashes)

Uploaded Source

Built Distribution

ontolutils-0.1.0-py3-none-any.whl (652.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page