Skip to main content

Python bindings for the OntoEnv Rust library. Manages ontology-based environments for building knowledge graphs.

Project description

OntoEnv Python Bindings

Installation

pip install ontoenv

Usage

from ontoenv import OntoEnv
from rdflib import Graph

# creates a new environment in the current directory, or loads
# an existing one. To use a different directory, pass the 'path'
# argument: OntoEnv(path="/path/to/env")
# OntoEnv() will discover ontologies in the current directory and
# its subdirectories
env = OntoEnv()

# add an ontology from a file path.
# env.add returns the name of the ontology, which is its URI
# e.g. "https://brickschema.org/schema/1.4-rc1/Brick"
brick_name = env.add("../brick/Brick.ttl")
print(f"Added ontology {brick_name}")

# When you add from a URL whose declared ontology name differs (for example a
# versioned IRI served at a versionless URL), ontoenv records that alias. You
# can later refer to the ontology by either the canonical name or the original
# URL when resolving imports or querying.

# get the graph of the ontology we just added
# env.get_graph returns an rdflib.Graph
brick_graph = env.get_graph(brick_name)
print(f"Brick graph has {len(brick_graph)} triples")

# get the full closure of the ontology, including all of its imports
# returns a tuple (rdflib.Graph, list[str])
brick_closure_graph, _ = env.get_closure(brick_name)
print(f"Brick closure has {len(brick_closure_graph)} triples")

# you can also add ontologies from a URL
rec_name = env.add("https://w3id.org/rec/rec.ttl")
rec_graph = env.get_graph(rec_name)
print(f"REC graph has {len(rec_graph)} triples")

# you can add an in-memory rdflib.Graph directly
in_memory = Graph()
in_memory.parse(data="""
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<http://example.com/in-memory> a owl:Ontology .
""", format="turtle")
in_memory_name = env.add(in_memory)
print(f"Added in-memory ontology {in_memory_name}")

# if you have an rdflib.Graph with an owl:Ontology declaration,
# you can transitively import its dependencies into the graph
g = Graph()
# this graph just has one triple: the ontology declaration for Brick
g.parse(data="""
@prefix owl: <http://www.w3.org/2002/07/owl#> .
<https://brickschema.org/schema/1.4-rc1/Brick> a owl:Ontology .
""")
# this will load all of the owl:imports of the Brick ontology into 'g'
env.import_dependencies(g)
print(f"Graph with imported dependencies has {len(g)} triples")

Namespace prefixes

OntoEnv can extract namespace prefix mappings from ontology source files. Prefixes come from both parser-level declarations (@prefix in Turtle, PREFIX in SPARQL-style syntaxes) and SHACL sh:declare entries.

# Get all namespaces across the entire environment
all_ns = env.get_namespaces()
# {'owl': 'http://www.w3.org/2002/07/owl#', 'brick': 'https://brickschema.org/schema/Brick#', ...}

# Get namespaces for a single ontology
ns = env.get_namespaces("https://brickschema.org/schema/1.4-rc1/Brick")

# Include namespaces from transitive owl:imports
ns_with_imports = env.get_namespaces("https://brickschema.org/schema/1.4-rc1/Brick", include_closure=True)

From the CLI:

ontoenv namespaces                                     # all namespaces
ontoenv namespaces https://example.org/my-ontology     # single ontology
ontoenv namespaces https://example.org/my-ontology --closure   # with imports
ontoenv namespaces --json                              # JSON output

Custom graph store

If you want OntoEnv to write graphs into an existing Python-backed store, pass a graph_store object that implements a small protocol:

class GraphStore:
    def add_graph(self, iri: str, graph: Graph, overwrite: bool = False) -> None: ...
    def get_graph(self, iri: str) -> Graph: ...
    def remove_graph(self, iri: str) -> None: ...
    def graph_ids(self) -> list[str]: ...
    def size(self) -> dict[str, int]: ...  # optional, returns {"num_graphs": ..., "num_triples": ...}

Example:

store = DictGraphStore()
env = OntoEnv(graph_store=store, temporary=True)

graph_store is currently incompatible with recreate and create_or_use_cached.

CLI Entrypoint

Installing ontoenv also provides the Rust-backed ontoenv command-line tool:

pip install ontoenv
ontoenv --help

The CLI is identical to the standalone ontoenv-cli binary; see the top-level README for usage.

Project details


Release history Release notifications | RSS feed

This version

0.5.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ontoenv-0.5.3.tar.gz (1.1 MB view details)

Uploaded Source

Built Distributions

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

ontoenv-0.5.3-cp311-abi3-win_amd64.whl (5.6 MB view details)

Uploaded CPython 3.11+Windows x86-64

ontoenv-0.5.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (87.9 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

ontoenv-0.5.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (86.9 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

ontoenv-0.5.3-cp311-abi3-macosx_11_0_arm64.whl (6.3 MB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

ontoenv-0.5.3-cp311-abi3-macosx_10_14_x86_64.whl (6.6 MB view details)

Uploaded CPython 3.11+macOS 10.14+ x86-64

ontoenv-0.5.3-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl (12.9 MB view details)

Uploaded CPython 3.11+macOS 10.14+ universal2 (ARM64, x86-64)macOS 10.14+ x86-64macOS 11.0+ ARM64

File details

Details for the file ontoenv-0.5.3.tar.gz.

File metadata

  • Download URL: ontoenv-0.5.3.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ontoenv-0.5.3.tar.gz
Algorithm Hash digest
SHA256 1e0b8991b1c8c240154d6c3536ada77534b6f97e5aeecff41137ccd11a3b6543
MD5 27dd06b3c998e94ea6eb1afaaf34914c
BLAKE2b-256 023fc7689cc36473bef894cc2822b0fb638fa569c5710256c6fd0612af0e991a

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.3-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: ontoenv-0.5.3-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 5.6 MB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ontoenv-0.5.3-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 b5b05231a16895d831f2fcae3a74070923f61b8430d81960d3340d1d9e3a5c4f
MD5 a6686e93b6dd34972ab26e9bb0195f94
BLAKE2b-256 545c33594a1881ddc7c29d1ab61245cfdabad0eaa4827688bad3fd1aeeb07888

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for ontoenv-0.5.3-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 775c46f6ca80a337292317c96ca91d53ede3e9122a68104e4ef90ca4977acf68
MD5 cecdadb32b98ed96e9dfcfb166820295
BLAKE2b-256 7f762499cfc36be11759d18b16e48d823df74bce771665547af49fdc7be02a42

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for ontoenv-0.5.3-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 dc281f561d4167147427cd355200849a06c156721022ac74d19a9027a88ceaf8
MD5 d23a3df1b9316e0b5f5558c1b2d66fb6
BLAKE2b-256 eacb369b1f455aeba64e0148b14c7f15f3eb5a9d8a2f0d94723030066f620511

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.3-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for ontoenv-0.5.3-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2481bdac8f077c3c58776e37766ee9b4adb170bd9ee97df5717e763f6c62ad54
MD5 bdb78c1151025ac8150dadb168baf3b4
BLAKE2b-256 5b0772e2fe6127ff4eb57e38fe5128caf9650c4622fe74320fd76577cff58e10

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.3-cp311-abi3-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for ontoenv-0.5.3-cp311-abi3-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 9032184fd396788d3a2657d323e169f28512a62aafbcb8c14a53c7ec1358730c
MD5 8f613a415c5fc5cc32cc3b568d8491eb
BLAKE2b-256 0c5ae2cce9cd305abc7c85478fc284e4c4400a2c7f5666d7f9c599946f94889f

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.3-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl.

File metadata

File hashes

Hashes for ontoenv-0.5.3-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 de93c22176086eb681f0009419d87c565f5275b5d1af67d8993e854fc277b113
MD5 71d443cb51e700237ed1225e19eb9e19
BLAKE2b-256 0ddf184f3696707253af5daa17a1f88f5dd96c804d55c0a6f5eb39872e92edd6

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