Prefixmaps Library
The `prefixmaps` library, currently at version 0.2.6, is a Python tool for managing and resolving semantic prefix maps, particularly for CURIEs (Compact URIs). It simplifies the expansion of short-form identifiers into full IRIs and vice versa, primarily by leveraging community-maintained prefix registries like those from OBO Foundry. Releases occur periodically to update prefix data and improve functionality.
Common errors
-
ModuleNotFoundError: No module named 'prefixmaps'
cause The `prefixmaps` library is not installed in the current Python environment.fixRun `pip install prefixmaps` to install the library. -
ValueError: Missing prefix for IRI: http://example.org/myterm
cause The `compress()` method was called with an IRI for which no matching prefix is defined in the current `PrefixMap` instance.fixEnsure that your `PrefixMap` instance includes a mapping for the base IRI you are trying to compress, or handle the `ValueError` gracefully. You might need to add a custom prefix map entry using `pm.add_prefix(prefix, iri_prefix)`. -
AttributeError: 'PrefixMap' object has no attribute 'compress_uri'
cause An outdated or incorrect method name was used. The correct method for compressing an IRI is `compress()`.fixRename the method call from `obj.compress_uri()` to `obj.compress()`.
Warnings
- breaking The method for loading the default registry has changed across minor versions. Directly calling `prefixmaps.io.get_and_parse_default_registry()` is now deprecated or removed.
- gotcha Initializing `PrefixMap()` without arguments attempts to load the default prefix registry from a remote web source (e.g., OBO Foundry). This operation will fail without an internet connection or if the remote source is temporarily unavailable.
- gotcha The `expand()` method returns `None` if a given CURIE's prefix is not found in the loaded `PrefixMap`, rather than raising an error. This requires explicit `None` checking in application code.
- deprecated Direct access to `prefixmaps.io.get_and_parse_default_registry()` is deprecated. This function was an internal detail that has been superseded by the `PrefixMap` constructor and class methods.
Install
-
pip install prefixmaps
Imports
- PrefixMap
from prefixmaps import PrefixMap
- load_jsonld_context
from prefixmaps.io import load_jsonld_context
Quickstart
from prefixmaps import PrefixMap
# Initialize with the default OBO Foundry prefix map (loaded from the web)
pm = PrefixMap()
# Expand a CURIE (Compact URI) to a full IRI
curie = "GO:0008150"
iri = pm.expand(curie)
print(f"Expanded {curie} to: {iri}")
# Compress a full IRI to a CURIE (if a matching prefix is found)
full_iri = "http://purl.obolibrary.org/obo/BFO_0000001"
compressed_curie = pm.compress(full_iri)
print(f"Compressed {full_iri} to: {compressed_curie}")
# Check if a prefix exists in the map
print(f"Has prefix 'GO': {pm.has_prefix('GO')}")
print(f"Has prefix 'UNKNOWN': {pm.has_prefix('UNKNOWN')}")