Idiomatic conversion between URIs and compact URIs (CURIEs)
The `curies` Python package provides efficient, faultless, and idiomatic utilities for converting between Uniform Resource Identifiers (URIs) and Compact URIs (CURIEs). It is a crucial low-level tool for linked data applications, enabling data scientists, curators, and software developers to handle prefix maps, expand CURIEs to URIs, and compress URIs to CURIEs. The library is actively developed, currently at version 0.13.3, with ongoing enhancements for robustness and usability.
Common errors
-
NameError: name 'curies' is not defined
cause The `curies` module was used without being imported first.fixAdd `import curies` at the top of your Python script or interactive session before using any functions or classes from the library. -
AttributeError: 'NoneType' object has no attribute 'compress'
cause A converter object (e.g., returned by `curies.load_prefix_map()` or `curies.get_obo_converter()`) was `None`, indicating that it failed to initialize properly, and subsequently, a method like `compress()` was called on this `None` object.fixVerify that your prefix map input is valid, or that the predefined converter could be loaded successfully. Always check that the `converter` object is not `None` before attempting to use its methods.
Warnings
- breaking As a library in the 0.x.x version range, `curies` is not yet considered stable. Breaking changes to the API may occur between minor versions (e.g., from 0.12 to 0.13).
- gotcha When loading prefix maps, especially with overlapping URI prefixes (e.g., 'http://purl.obolibrary.org/obo/' and 'http://purl.obolibrary.org/obo/CHEBI_'), `curies` prioritizes the first matching prefix encountered. This behavior ensures 'faultless' conversion but may differ from expectations if not explicitly considered.
Install
-
pip install curies
Imports
- curies
import curies
- Converter
from curies import Converter
Quickstart
import curies
# Option 1: Load a predefined converter (e.g., OBO Foundry)
converter = curies.get_obo_converter()
# Option 2: Load a custom prefix map
# prefix_map = {"CHEBI": "http://purl.obolibrary.org/obo/CHEBI_"}
# converter = curies.load_prefix_map(prefix_map)
# Expand a CURIE to a URI
curie_example = "CHEBI:1"
expanded_uri = converter.expand(curie_example)
print(f"Expanded '{curie_example}' to '{expanded_uri}'")
# Expected: Expanded 'CHEBI:1' to 'http://purl.obolibrary.org/obo/CHEBI_1'
# Compress a URI to a CURIE
uri_example = "http://purl.obolibrary.org/obo/CHEBI_2"
compressed_curie = converter.compress(uri_example)
print(f"Compressed '{uri_example}' to '{compressed_curie}'")
# Expected: Compressed 'http://purl.obolibrary.org/obo/CHEBI_2' to 'CHEBI:2'