Skip to main content

Utility library for the work with ontologies.

Project description

Ontolutils - Object-oriented "Things"

Tests Status Coverage Documentation Status pyvers Status

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")  # 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",
           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",
           firstName='Matthias', lastName='Probst')
# 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": "https://www.w3.org/2002/07/owl#",
    "rdfs": "https://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.

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

Uploaded Source

Built Distribution

ontolutils-0.10.0-py3-none-any.whl (98.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ontolutils-0.10.0.tar.gz
  • Upload date:
  • Size: 101.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for ontolutils-0.10.0.tar.gz
Algorithm Hash digest
SHA256 17d74af0d74dfa0b760c18f61bb4e0b8aa8af4cd6819487a13a044daee321f31
MD5 4abb1ec246f78fd7b26ec58fa1c3398d
BLAKE2b-256 69325de88718ffb0ebd7217580bf8ed1fd7bd2c0ba519372e2796a42e25a3d16

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ontolutils-0.10.0-py3-none-any.whl
  • Upload date:
  • Size: 98.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.19

File hashes

Hashes for ontolutils-0.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e703090b7f3ed31f43bd6404cdf07c451a4b79c9b7afd71fbb80f852b23ddf61
MD5 1f930001b48c6272887d0495395c555d
BLAKE2b-256 33ce93f71114b5994e330d246fa776275de7931c656031ce23917755b96fb9e1

See more details on using hashes here.

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