Ontolutils - Object-oriented "Things"
This package helps you in generating ontology-related objects and let's you easily create JSON-LD files.
Quickstart
Installation
Install the package:
pip install ontolutils
Usage
Imagine you want to describe a prov:Person
with a first name, last name and an email address but writing
the JSON-LD file yourself is too cumbersome and you want validation of the parsed parameters. The package
lets you design classes, which describe ontology classes like this:
from pydantic import EmailStr, Field
from pydantic import HttpUrl, model_validator
from ontolutils import Thing, urirefs, namespaces, as_id
@namespaces(prov="https://www.w3.org/ns/prov#",
foaf="https://xmlns.com/foaf/0.1/",
m4i='http://w3id.org/nfdi4ing/metadata4ing#')
@urirefs(Person='prov:Person',
firstName='foaf:firstName',
last_name='foaf:lastName',
mbox='foaf:mbox',
orcidId='m4i:orcidId')
class Person(Thing):
firstName: str
lastName: str = Field(default=None, alias="last_name")
mbox: EmailStr = Field(default=None, alias="email")
orcidId: HttpUrl = Field(default=None, alias="orcid_id")
@model_validator(mode="before")
def _change_id(self):
return as_id(self, "orcidId")
p = Person(id="https://orcid.org/0000-0001-8729-0482",
firstName='Matthias', last_name='Probst')
p = Person(id="https://orcid.org/0000-0001-8729-0482",
firstName='Matthias', lastName='Probst')
p.model_dump_jsonld()
Now, you can instantiate the class and use the model_dump_jsonld()
method to get a JSON-LD string:
{
"@context": {
"owl": "https://www.w3.org/2002/07/owl#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"prov": "https://www.w3.org/ns/prov#",
"foaf": "https://xmlns.com/foaf/0.1/"
},
"@id": "https://orcid.org/0000-0001-8729-0482",
"@type": "prov:Person",
"foaf:firstName": "Matthias",
"foaf:lastName": "Probst"
}
Documentation
Please visit the documentation for more information.