Example #1
0
def _addOptionalVars(rows, query):
    """augment the rows with 'x':None values for any query
    variable x that happened not to be bound in that row. This is
    for consistency with rdflib's sparql results, which always
    include all the vars in their result tuple.

    The parseJsonResults (and parseSparqlResults) version totally
    has the information to do this itself, but it seemed easier to
    write it here. This second pass might be a little bit slower.

    rows are edited in-place, and then returned.
    """
    vars = [v.strip('?') for v in sparqlSelection(query)]
    for row in rows:
        for v in vars:
            if v not in row:
                row[v] = None
    return rows
Example #2
0
def _addOptionalVars(rows, query):

    """augment the rows with 'x':None values for any query
    variable x that happened not to be bound in that row. This is
    for consistency with rdflib's sparql results, which always
    include all the vars in their result tuple.

    The parseJsonResults (and parseSparqlResults) version totally
    has the information to do this itself, but it seemed easier to
    write it here. This second pass might be a little bit slower.

    rows are edited in-place, and then returned.
    """
    vars = [v.strip('?') for v in sparqlSelection(query)]
    for row in rows:
        for v in vars:
            if v not in row:
                row[v] = None
    return rows
Example #3
0
def interpolateSparql(query, initBindings):
    """expand the bindings into the query string to make one
    standalone query. Very sloppy; probably gets quoting wrong.

    >>> interpolateSparql('SELECT ?x { ?x ?y ?z }', {Variable('?z') : Literal('hi')})
    u'SELECT ?x { ?x ?y "hi" }'
    >>> interpolateSparql('SELECT ?x { ?x <http://example/?z=1> ?z }', {Variable('?z') : Literal('hi')})
    u'SELECT ?x { ?x <http://example/?z=1> "hi" }'
    """
    prolog = query[:query.find('{')]
    text = query[query.find('{'):]
    selection = sparqlSelection(query)
    #    print "Sel is", selection
    for var, value in initBindings.items():
        # i can't seem to handle various versions of rdflib and
        # Variables that have or don't have leading ?
        var = var.lstrip('?')
        if '?' + var not in selection:
            # hopefully you don't have spaces in your urls, and you do
            # have spaces on both sides of all variable names
            text = text.replace(' ?%s ' % var, ' %s ' % value.n3())
    query = prolog + text
    #    print "Expand to", query
    return query
Example #4
0
def interpolateSparql(query, initBindings):
    """expand the bindings into the query string to make one
    standalone query. Very sloppy; probably gets quoting wrong.

    >>> interpolateSparql('SELECT ?x { ?x ?y ?z }', {Variable('?z') : Literal('hi')})
    u'SELECT ?x { ?x ?y "hi" }'
    >>> interpolateSparql('SELECT ?x { ?x <http://example/?z=1> ?z }', {Variable('?z') : Literal('hi')})
    u'SELECT ?x { ?x <http://example/?z=1> "hi" }'
    """
    prolog = query[:query.find('{')]
    text = query[query.find('{'):]
    selection = sparqlSelection(query)
#    print "Sel is", selection
    for var, value in initBindings.items():
        # i can't seem to handle various versions of rdflib and
        # Variables that have or don't have leading ?
        var = var.lstrip('?')
        if '?' + var not in selection:
            # hopefully you don't have spaces in your urls, and you do
            # have spaces on both sides of all variable names
            text = text.replace(' ?%s ' % var, ' %s ' % value.n3())
    query = prolog + text
#    print "Expand to", query
    return query