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
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ontoenv-0.5.4.tar.gz.
File metadata
- Download URL: ontoenv-0.5.4.tar.gz
- Upload date:
- Size: 1.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a17462e7a50a3fb2babaaaef192562f3153c613da1ac6421f1e24b62dfd1f85
|
|
| MD5 |
54a3e5aa192656e442bc517f2838828c
|
|
| BLAKE2b-256 |
cefdc4f98f4361f93aebba9393f39674e13b069b0490c23645b2edc8e041aece
|
File details
Details for the file ontoenv-0.5.4-cp311-abi3-win_amd64.whl.
File metadata
- Download URL: ontoenv-0.5.4-cp311-abi3-win_amd64.whl
- Upload date:
- Size: 5.0 MB
- Tags: CPython 3.11+, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
657dbd1fc3bd5a2b4d80e4f3a63973715bd28c099b39a6a2b9f70e02176b9074
|
|
| MD5 |
28743b250341de95db00973a10506ed4
|
|
| BLAKE2b-256 |
85e3a6d6b6a38b6382c1ec4f4cedcc6309c5495536a8b0621af1895b569908b7
|
File details
Details for the file ontoenv-0.5.4-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: ontoenv-0.5.4-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 54.6 MB
- Tags: CPython 3.11+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac2d299809a5fd779bdcc86851a4d310ee5bda4b3951905f239e35420488192f
|
|
| MD5 |
e0612f4ab3522c0d7bbc2b4349b067ce
|
|
| BLAKE2b-256 |
f7f2bc3011cf68e9bb8d5c779251f975a8ba21751281aff41be52c9c1b7e5179
|
File details
Details for the file ontoenv-0.5.4-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: ontoenv-0.5.4-cp311-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 53.5 MB
- Tags: CPython 3.11+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
672a4551632585614debbf54b318eed50f8a32f8b830fc6c2584ae1e01329ba2
|
|
| MD5 |
93917e02679884de1f1c2fcc1aa70d76
|
|
| BLAKE2b-256 |
b6168980f353a5d90ba5b7e8bcf673fdf7f47c50f93586ca5d923f27440f908a
|
File details
Details for the file ontoenv-0.5.4-cp311-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: ontoenv-0.5.4-cp311-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 5.8 MB
- Tags: CPython 3.11+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f16c2d2a0f5da4d31f8c08f65bd3d63931bf718f8ca753ff3327f5bbd53f6838
|
|
| MD5 |
3d64537adc2e5f7e2765db2f218f1896
|
|
| BLAKE2b-256 |
7d418d6d490eef84a3bc50ede5021e09eaa2188a904d5c43784a818e53616610
|
File details
Details for the file ontoenv-0.5.4-cp311-abi3-macosx_10_14_x86_64.whl.
File metadata
- Download URL: ontoenv-0.5.4-cp311-abi3-macosx_10_14_x86_64.whl
- Upload date:
- Size: 6.0 MB
- Tags: CPython 3.11+, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5e3fef264d170b3225fdd46ff7393448ff770b121fa92af05b039bceb6670ddf
|
|
| MD5 |
f5d4eddc3ba603ec4e58f4166e1fef9e
|
|
| BLAKE2b-256 |
a71ecbaf4b5e7b4aba4f469a7c9796981ee04b98981d358597856c69d374295f
|
File details
Details for the file ontoenv-0.5.4-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl.
File metadata
- Download URL: ontoenv-0.5.4-cp311-abi3-macosx_10_14_x86_64.macosx_11_0_arm64.macosx_10_14_universal2.whl
- Upload date:
- Size: 11.8 MB
- Tags: CPython 3.11+, macOS 10.14+ universal2 (ARM64, x86-64), macOS 10.14+ x86-64, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
947587e7e10f5cc223f533e837a55e0ced40d6ce60d439c79064c621de98e2e4
|
|
| MD5 |
809ab0f5d18e697731dfc2859239b906
|
|
| BLAKE2b-256 |
0953054bb193821c1b9a96f542614e1c9c633bb1d9018d3e5379979cc8308eb5
|