Utility library for the work with ontologies.
Project description
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 ontolutils import Thing, urirefs, namespaces
@namespaces(prov="http://www.w3.org/ns/prov#",
foaf="http://xmlns.com/foaf/0.1/")
@urirefs(Person='prov:Person',
firstName='foaf:firstName',
last_name='foaf:lastName',
mbox='foaf:mbox')
class Person(Thing):
firstName: str
last_name: str = Field(default=None, alias="lastName") # you may provide an alias
mbox: EmailStr = None
p = Person(id="cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
firstName='John', last_name='Doe')
# as we have set an alias, we can also use "lastName":
p = Person(id="cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
firstName='John', lastName='Doe')
# The jsonld representation of the object will be the same in both cases:
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": "http://www.w3.org/2002/07/owl#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"lastName": "http://xmlns.com/foaf/0.1/",
"prov": "http://www.w3.org/ns/prov#",
"foaf": "http://xmlns.com/foaf/0.1/"
},
"@id": "cde4c79c-21f2-4ab7-b01d-28de6e4aade4",
"@type": "prov:Person",
"foaf:firstName": "John",
"lastName": "Doe"
}
Documentation
Please visit the documentation for more information.
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
ontolutils-0.4.2.tar.gz
(98.9 kB
view hashes)
Built Distribution
ontolutils-0.4.2-py3-none-any.whl
(97.3 kB
view hashes)
Close
Hashes for ontolutils-0.4.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6cd3989ff5e4e0b3410d291906c9f506d4fc5eacbd4987d47963d499391345a3 |
|
MD5 | 6ab99fd77fbb42c82daf30243be429c8 |
|
BLAKE2b-256 | 81fa0065cb01380d7963d94936822fd6ce94f682be1bc4fa8aa297f0292dfc30 |