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()
# Alternatively use
serialized_str = p.serialize(format="ttl") # or "json-ld", "n3", "nt", "xml"

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"
}

Ontolutils does not implement your Ontology class? Well, build your own:

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')])

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.13.1.tar.gz (104.9 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.13.1-py3-none-any.whl (101.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ontolutils-0.13.1.tar.gz
Algorithm Hash digest
SHA256 edcc961ca1d1f8e1f93ff6fbd5728013c12b6e353a18cb58728f5ac7ddf361f3
MD5 39929ba9b83f069a22e5e675979b9cbc
BLAKE2b-256 d59d6f896966436ce9e0beef357e5fec5ebda6a4dccc0f2efa75f44b4672119f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ontolutils-0.13.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5173e9cd84eff3c935f0f0feed0f7a7e96b0fe7242c8292c1217f1d53defa311
MD5 9d6b5c358597bb77a49bf633cb4ab775
BLAKE2b-256 2417f9422c812556996057e6f62ac94dabc74a1125ac3452282eab03912a7fa8

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