Skip to main content

Utility library for the work with ontologies.

Project description

Ontolutils - Object-oriented "Things"

Tests Status codecov Documentation Status pyvers Status

This package helps you in generating ontology-related objects and lets 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:

import rdflib
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',
         lastName='foaf:lastName',
         mbox='foaf:mbox',
         orcidId='m4i:orcidId')
class Person(Thing):
    firstName: str
    lastName: str = Field(default=None, alias="last_name")  # you may provide an alias
    mbox: EmailStr = Field(default=None, alias="email")
    orcidId: HttpUrl = Field(default=None, alias="orcid_id")

    # the following will ensure, that if orcidId is set, it will be used as the id
    @model_validator(mode="before")
    def _change_id(self):
        return as_id(self, "orcidId")


p = Person(
    id="https://orcid.org/0000-0001-8729-0482",
    label=rdflib.Literal("The creator of this package", lang="en"),
    firstName='Matthias',
    last_name='Probst'
)
# as we have set an alias, we can also use "lastName":
p = Person(
    id="https://orcid.org/0000-0001-8729-0482",
    label=rdflib.Literal("The creator of this package", lang="en"),
    firstName='Matthias',
    lastName='Probst'
)
# The jsonld representation of the object will be the same in both cases:
json_ld_serialization = p.model_dump_jsonld()
# Alternatively use
serialized_str = p.serialize(format="json-ld")  # or "ttl", "n3", "nt", "xml"

The result of the serialization is shown below:

{
  "@context": {
    "owl": "http://www.w3.org/2002/07/owl#",
    "rdfs": "http://www.w3.org/2000/01/rdf-schema#",
    "schema": "http://schema.org/",
    "prov": "https://www.w3.org/ns/prov#",
    "foaf": "https://xmlns.com/foaf/0.1/",
    "m4i": "http://w3id.org/nfdi4ing/metadata4ing#"
  },
  "@id": "https://orcid.org/0000-0001-8729-0482",
  "@type": "prov:Person",
  "foaf:firstName": "Matthias",
  "foaf:lastName": "Probst"
}

Define an ontology class dynamically:

If you cannot define the class statically as above, you can also define it dynamically:

from typing import List, Union

from ontolutils import build, Property, Thing

Event = build(
    namespace="https://schema.org/",
    namespace_prefix="schema",
    class_name="Event",
    properties=[Property(
        name="about",
        default=None,
        property_type=Union[Thing, List[Thing]]
    )]
)
conference = Event(label="my conference", about=[Thing(label='The thing it is about')])
ttl = conference.serialize(format="ttl")

The serialization in turtle format looks like this:

@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <https://schema.org/> .

[] a schema:Event ;
    rdfs:label "my conference" ;
    schema:about [ a owl:Thing ;
            rdfs:label "The thing it is about" ] .

Add a property at runtime

Say, we forgot to add the location to the above Event class. No problem, we can add it at runtime:

Event.add_property(
    name="location",
    property_type=Union[Thing, List[Thing]],
    default=None,
    namespace="https://schema.org/",
    namespace_prefix="schema"
)
conference = Event(
    label="my conference",
    location=Thing(label="The location")
)
ttl = conference.serialize(format="ttl")

The above fives us this Turtle serialization:

@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix schema: <https://schema.org/> .

[] a schema:Event ;
    rdfs:label "my conference" ;
    schema:location [ a owl:Thing ;
            rdfs:label "The location" ] .

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.26.0.tar.gz (141.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ontolutils-0.26.0-py3-none-any.whl (128.8 kB view details)

Uploaded Python 3

File details

Details for the file ontolutils-0.26.0.tar.gz.

File metadata

  • Download URL: ontolutils-0.26.0.tar.gz
  • Upload date:
  • Size: 141.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for ontolutils-0.26.0.tar.gz
Algorithm Hash digest
SHA256 50c8bcc1c98a40f92893483dc1ef360a42cacc2a2b629c9841156f22b5082061
MD5 1b2c6250d7f9bbffb2139e96ff85ca19
BLAKE2b-256 9cb4fb5dbc6c3b36602a91363d233f6e86a7ec88211c477f83323b0feff56746

See more details on using hashes here.

File details

Details for the file ontolutils-0.26.0-py3-none-any.whl.

File metadata

  • Download URL: ontolutils-0.26.0-py3-none-any.whl
  • Upload date:
  • Size: 128.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for ontolutils-0.26.0-py3-none-any.whl
Algorithm Hash digest
SHA256 482c1aa8602fd7203510b39439aefeab74ff9b501c43519ef43058d2e528a8da
MD5 6557e132bcbd23f111cd0062724a4f41
BLAKE2b-256 a008861cc8b7f6c5ea461a9b0786b5310bd73656202df41c45c42ca3d3623054

See more details on using hashes here.

Supported by

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