rdflib icon indicating copy to clipboard operation
rdflib copied to clipboard

Implement bind_namespaces strategy for Prefix.cc

Open cthoyt opened this issue 2 years ago • 7 comments

Summary of changes

This PR implements a web-dependent loader for prefix-namespace definitions from Prefix.cc by parsing its context document (https://prefix.cc/context.jsonld)

Demo

The Gene Ontology is a biomedical ontology describing biological processes, cellular locations, and cellular components. It is typically abbreviated with the prefix "go" and uses PURLs issued by the Open Biological and Biomedical Ontologies (OBO) Foundry. Prefix.cc has an entry for GO that uses its preferred OBO PURL.

import rdflib

graph = rdflib.Graph(bind_namespaces="cc")

prefix_map = {prefix: str(ns) for prefix, ns in graph.namespaces()}
assert "go" in prefix_map
assert prefix_map["go"] == "http://purl.obolibrary.org/obo/GO_"
assert graph.qname("http://purl.obolibrary.org/obo/GO_0032571") == "go:0032571"

Checklist

  • [x] Checked that there aren't other open pull requests for the same change.
  • [x] Added tests for any changes that have a runtime impact.
  • [x] Checked that all tests and type checking passes.
  • For changes that have a potential impact on users of this project:
    • [x] Updated relevant documentation to avoid inaccuracies.
    • [x] Considered adding additional documentation.
    • [x] Considered adding an example in ./examples for new features.
    • [ ] Considered updating our changelog (CHANGELOG.md).
  • [x] Considered granting push permissions to the PR branch, so maintainers can fix minor issues and keep your PR up to date.

cthoyt avatar Feb 27 '23 14:02 cthoyt

Will try to get this into the next release, I will make any changes needed directly to your branch.

aucampia avatar Mar 11 '23 14:03 aucampia

@aucampia thank you for the feedback! Feel free to make any edits you think are appropriate or request I make some updates.

cthoyt avatar Mar 11 '23 14:03 cthoyt

@cthoyt I will likely make another patch release soon, so will hold back a bit on merging this.

aucampia avatar Mar 19 '23 18:03 aucampia

Since we had this in the docstring, I'm going to merge it (possibly with some minor changes), though possibly it would have been better in rdflib._contrib, in future I will channel functionality like this there instead.

aucampia avatar Apr 07 '23 18:04 aucampia

Since we had this in the docstring, I'm going to merge it (possibly with some minor changes), though possibly it would have been better in rdflib._contrib, in future I will channel functionality like this there instead.

Actually, I think it would be better to just move this into rdflib._contrib, some ways in which we can implement this is:

  • Change type of bind_namespaces to also accept a mapping of prefix to string (similar to rdflib.namespace._NAMESPACE_PREFIXES_RDFLIB), add a function to rdflib._contrib that returns such a mapping for prefix.cc, this would then be used as follow:

    from rdflib._contrib import get_prefixcc_mapping
    from rdflib import Graph
    
    g = Graph(bind_namespaces=rdflib._contrib.get_prefixcc_mapping())
    
  • Change type of bind_namespaces to also accept a function that returns a mapping of prefix to string

    from rdflib._contrib.namespace_providers import prefix_cc
    from rdflib import Graph
    
    g = Graph(bind_namespaces=prefix_cc)
    

    rdflib._contrib.namespace_providers.prefix_cc would then return the mapping when called.

Maybe something better can also be done, but I would rather have it in rdflib._contrib than explain in the doc string that we cannot cover this with normal guarantees for our public interface stability.

aucampia avatar Apr 07 '23 19:04 aucampia

PRs to V6 is closed until further notice. See this for more details:

  • https://github.com/RDFLib/rdflib/discussions/2395

aucampia avatar May 21 '23 18:05 aucampia

PRs to V6 is closed until further notice. See this for more details:

We will be open for PRs again once this is resolved:

  • https://github.com/RDFLib/rdflib/pull/2402

aucampia avatar May 22 '23 22:05 aucampia