Skip to main content

ouisparql - Simple SPARQL in Python

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.

Metadata

Release files for ouisparql 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ouisparql 0.2.1
File Size Uploaded Provenance
ouisparql-0.2.1.tar.gz 203.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ouisparql 0.2.1
File Interpreter ABI Platform
ouisparql-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 224.8 kB

Release files / ouisparql-0.2.1.tar.gz

Download URL ouisparql-0.2.1.tar.gz
Size 203.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d65df771902beae4b9d61b87d5e6f10cda2646c4345fb14e7d6506e33c231b85
BLAKE2b-256 checksum
How to use checksums
cc8209ea75950af3a558cfde401933204f6e624d31e7b98db59562018d6e5415
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / ouisparql-0.2.1-py3-none-any.whl

Download URL ouisparql-0.2.1-py3-none-any.whl
Size 21.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
49027c63e4eda8ce346ecea0efa23fd363c9e72c52bcb53e2cecb23899e788fa
BLAKE2b-256 checksum
How to use checksums
b8b73d83fcf18988e039abc880630e240d98de98b0c4132bc8f1f33596cd74fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.2.0 CPython/3.9.25

Security for ouisparql 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page