Utility library for the work with ontologies.
Project description
Ontolutils - Utilities for work with ontologies
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for ontolutils-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d24957c09aa971c6cc6841046a7b06113f1ad58f20d9cac2bcc2aee7bb9aedc |
|
MD5 | 739afaab62f2de8c1fae8f56bc159743 |
|
BLAKE2b-256 | 1049975744fa386f06deb97dd7287f0cbafeb3d17a8e03c62de78d1785501f43 |