Skip to main content

Simple SPARQL in Python

Project description

SPARQL is code. Write it, version control it, comment it, and run it using files. Writing your SPARQL code in Python programs as strings doesn’t allow you to easily reuse them in SPARQL GUIs or CLI tools. With ouisparql you can organize your SPARQL statements in .sparql files, load them into your python application as methods to call without losing the ability to use them as you would any other SPARQL file.

This module is an implementation of Kris Jenkins’ yesql Clojure library to the Python ecosystem, adapted from aiosql for SPARQL.

Badges

Usage

Install from pypi, for instance by running pip install ouisparql.

Install with pip from gitlab:

pip install  git+https://gitlab.com/sortion/ouisparql.git@main

Then write parametric SPARQL queries in a file and execute it from Python methods, e.g., this predicates.sparql file:

# name: get_predicates()
# Get the first two predicates in the database
SELECT DISTINCT ?predicate
WHERE {
      ?subject ?predicate ?object .
}

# name: get_subject_by_predicate(predicate)
# Get the first two subjects from a given predicate using a named parameter
SELECT DISTINCT ?subject
WHERE {
      ?subject :predicate ?object .
}

This example has an imaginary SPARQL endpoint with RDF triplets. It displays all predicates in the first example, and all subjects associated with a given predicate.

OuiSPARQL main feature is to be able to load queries from a SPARQL file and call them by name in python code. Query parameter declarations (e.g., (predicate)) are optional, and enforced when provided.

You can use ouisparql to load the queries in this file for use in your Python application:

import ouisparql
from SPARQLWrapper import SPARLQWrapper
sparql = SPARQLWrapper("https://query.wikidata.org/sparql", agent="OuiSPARQL documentation")
queries = ouisparql.from_path("queries.rq", "sparql_wrapper")
queries.get_all_predicate()
queries.get_subjects_by_predicate(predicate)

Why you might want to use this

  • You think SPARQL is pretty good, and writing SPARQL is an important part of your applications.

  • You don’t want to write your SPARQL in strings intermixed with your python code.

  • You want to be able to reuse your SPARQL in other contexts, e.g., loading it into a SPARQL endpoint or other tools.

Why you might NOT want to use this

  • You aren’t comfortable writing SPARQL code.

  • Dynamically loaded objects built at runtime really bother you.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ouisparql-0.1.0.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ouisparql-0.1.0-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file ouisparql-0.1.0.tar.gz.

File metadata

  • Download URL: ouisparql-0.1.0.tar.gz
  • Upload date:
  • Size: 34.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for ouisparql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3559aa42ad33beee822423b4abc7da452acb0bc8b568e2d35eb01987a92fb278
MD5 03563b16dab9c75bbcdceed80071a48e
BLAKE2b-256 c5548ec8453668822665e4d81a670637148aa45b40c67410c2a1f02f4861d3d4

See more details on using hashes here.

File details

Details for the file ouisparql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ouisparql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for ouisparql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c87e933017205a1db1ee2842e8b50136eecd714b8442475efaebc6f6f171fd8b
MD5 2cf60859d6df2f123e029cb82977974b
BLAKE2b-256 b6d9f1c5ac15d9270020cfb0f24f3a5d8c1430b8a7021e370e8df71e168a5c69

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page