Skip to content

Instantly share code, notes, and snippets.

@lawlesst
Created September 21, 2012 20:09
Show Gist options
  • Save lawlesst/3763611 to your computer and use it in GitHub Desktop.
Save lawlesst/3763611 to your computer and use it in GitHub Desktop.
Helper to create a random identifier for a resource via Sparql.
from SPARQLWrapper import SPARQLWrapper, JSON, N3
exists = """
SELECT *
WHERE {
OPTIONAL{<%(next_uri)s> ?p ?o}.
OPTIONAL{?s2 ?p2 <%(next_uri)s>}.
}
"""
def get_next_uri(namespace, endpoint, used_uris=[], max=99999, test=False):
"""
Get the nextURI for the given namespace.
SPARQL interpretation of getNextURI here:
http://svn.code.sf.net/p/vivo/vitro/code/branches/dev-sdb/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/jena/JenaIngestUtils.java
"""
import random
if not namespace.endswith('n'):
namespace += 'n'
count = 0
while True:
next_uri = namespace + str(random.randint(1000, max))
q = exists % {'next_uri': next_uri}
sparql = SPARQLWrapper(endpoint)
sparql.setQuery(q)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
if results['results']['bindings'][0] == {}:
break
count += 1
if count == 10:
raise Exception("""
10 random uris were tried and none returned
a unique uri. Verify or increase max random uri.""")
return next_uri
if __name__ == "__main__":
from vivo_ingest.settings import NAMESPACE, VIVO_SPARQL
print VIVO_SPARQL
next = get_next_uri(NAMESPACE,
VIVO_SPARQL)
print next
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment