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.2

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.2.tar.gz (1.0 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.2-cp311-abi3-win_amd64.whl (5.6 MB view details)

Uploaded CPython 3.11+Windows x86-64

ontoenv-0.5.2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (87.7 MB view details)

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

ontoenv-0.5.2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (86.7 MB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

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

Uploaded CPython 3.11+macOS 11.0+ ARM64

ontoenv-0.5.2-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.2-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl (12.8 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.2.tar.gz.

File metadata

  • Download URL: ontoenv-0.5.2.tar.gz
  • Upload date:
  • Size: 1.0 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.2.tar.gz
Algorithm Hash digest
SHA256 ceb9264be7feb606d38b74983dad2ec59019dba27a605e4627f15ac9c1a8ebfb
MD5 384ea240c2380eaad7da35a0d6f0067e
BLAKE2b-256 421929775fa1452aeee3967327e2c9a04dacc3454bcb9760095e3be3b35c6780

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ontoenv-0.5.2-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.2-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 42ed9d989c13c692fe18aca784c972b440dcae575c2cd6f8692a8fbc850811de
MD5 6b9b58a97f7d77ae8ae57561278dbe67
BLAKE2b-256 36077f1b8aefa3ff39360142d0116449cb91c43f2d063cc701cf026a5d3cfba1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.5.2-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7fbbd4745d2f156b890c3c95452b9e8095dfd48b5606d869920b525743358a6d
MD5 71800cdc822a8bf2a6c31883bdebd3eb
BLAKE2b-256 5553a14984ea79c0574bfe9c62fc4c6a62cf7af6e4db852e0a69e19b40d5c32a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.5.2-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 133f4d3837433da927df246095a54d6e13dfb4698e614f64c14fbfba35bdc10b
MD5 585254d433fec241eab4b6d7da34352a
BLAKE2b-256 8ff639c89382d13e5589bf38d21dd7452179d849585ad8c280d986769efa1bec

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.5.2-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 87b298234e9eb199ef49083f67559b04f114124407630bf5e7542b049ac50d00
MD5 7b7b9c89a25a3febd50bfa01120a56ce
BLAKE2b-256 8271b4b8bbb363642b25b2d4901c538d9e6aa67aa4e2a0329ff26a09efc2bba6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for ontoenv-0.5.2-cp311-abi3-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 9fa9b040f88bd3dd47f5aba81172e129b966fcdd02f27ed315249b73261afa22
MD5 010fbbbb4ba84dc3a83586e8b3989c70
BLAKE2b-256 1f0983aaf10ffd189015cdfb08bbc023f6ebf4ba21f3e8c9384e39473f3f5c25

See more details on using hashes here.

File details

Details for the file ontoenv-0.5.2-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.2-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl
Algorithm Hash digest
SHA256 acf9a21b5e3a34143620eec39974059fc4b6333bd1e12c15cef33ad37242fc9d
MD5 8d6dc41ba20e7f8855b2bd041bbd4b42
BLAKE2b-256 24667347b7030efb7ffa3bceb8c3a4661263ca07471c75d4b0c4ac8f93039303

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