Skip to main content

This package is for parsing the SPARQL Queries in order to save the world

Project description

🧠 super_sparql

super_sparql is a lightweight Python package for parsing and analyzing SPARQL queries in a structured, programmable way. It extracts components like prefixes, triple patterns, filters, limits, and even infers types from the query structure.

🚀 Features

  • Parses SPARQL SELECT, CONSTRUCT, ASK, DESCRIBE queries
  • Extracts:
    • Prefixes
    • Triple patterns
    • SELECT variables
    • Filters
    • LIMIT / OFFSET / ORDER BY
  • Infers variable roles and types
  • Returns a structured dataclass representation of the query
  • Handles partial or loosely formatted SPARQL

📦 Installation

pip install super_sparql

Or if you're using it locally (after building your wheel):

pip install dist/super_sparql-0.1.0-py3-none-any.whl

✨ Quick Start

from super_sparql import parse_my_SPARQL

query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcm: <http://example.org/ns/dcm#>

SELECT ?x ?label
WHERE {
  ?x rdf:type dcm:Image .
  ?x dcm:label ?label .
  FILTER(lang(?label) = "en")
}
ORDER BY ?label
LIMIT 10
OFFSET 5
"""

parser = parse_my_SPARQL(query)
parsed = parser.parse()
print(parsed)

Output:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dcm: <http://example.org/ns/dcm#>

SELECT ?x ?label
WHERE {
  ?x rdf:type dcm:Image.
  ?x dcm:label ?label.
  FILTER(lang(?label) = "en")
}
ORDER BY ?label
LIMIT 10
OFFSET 5

🔍 Use Cases

  1. Get SELECT Variables
parser.get_select_variables()
# ➞ ['?x', '?label']
  1. Extract Triple Patterns
for triple in parser.get_triple_patterns():
    print(triple)
# ➞ ?x rdf:type dcm:Image
# ➞ ?x dcm:label ?label
  1. Get Variable Types
parser.get_select_variable_types()
# ➞ {'?x': ['dcm:Image'], '?label': ['Range of dcm:label']}
  1. Full Query Analysis
analysis = parser.analyze_query()
print(analysis)

Sample Output:

{
  "query_type": "SELECT",
  "select_variables": ["?x", "?label"],
  "variable_types": {
    "?x": ["dcm:Image"],
    "?label": ["Range of dcm:label"]
  },
  "variables_details": {
    "?x": {
      "in_select": true,
      "occurrences": {
        "as_subject": [{"triple_index": 0, "triple": "?x rdf:type dcm:Image"}, {"triple_index": 1, "triple": "?x dcm:label ?label"}],
        "as_predicate": [],
        "as_object": []
      }
    },
    ...
  },
  "triple_count": 2,
  "filter_count": 1,
  "has_order_by": true,
  "has_limit": true,
  "has_offset": true
}

📚 Supported SPARQL Clauses

Clause Supported Notes
PREFIX Auto-completes common prefixes
SELECT Supports variables and wildcard *
WHERE Parses triple patterns and filters
FILTER Basic extraction supported
ORDER BY Supports ASC/DESC
LIMIT Extracts integer limit
OFFSET Extracts integer offset
CONSTRUCT ⚠️ Detected, parsed like SELECT
ASK, DESCRIBE ⚠️ Detected, parsed like SELECT

📝 License

MIT License — see LICENSE for full text.

🙌 Acknowledgements

Built with sarcasm and care to make SPARQL parsing easier, faster, and less soul-crushing.

🧪 Example Query Playground

Try with queries like:

SELECT * 
WHERE {
  ?book rdf:type dcm:Book .
  ?book dcm:title ?title .
  FILTER regex(?title, "SPARQL", "i")
}
ORDER BY DESC(?title)
LIMIT 5

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

super_sparql-0.1.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

super_sparql-0.1.0-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: super_sparql-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for super_sparql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 af3c2add5463c48e024a447e253586ef65ab6a78f3feb8e4669fab5f9bb7685c
MD5 8a24fe5ae59dc71e20f46c004bb1a137
BLAKE2b-256 3e2a2282a473c2b7605f0ba4cefcb2eceb2260d832b04ad094cef69c5d3e7440

See more details on using hashes here.

File details

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

File metadata

  • Download URL: super_sparql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for super_sparql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 913571ebccff626a0719815ba14cc5c224c4ef637ed7cb68a412725ea74899f9
MD5 65df90015dccf9c147983c259dd0054a
BLAKE2b-256 dd4291ec28b0cc330218d6e3e22c69e5bbc92fdf80bfed4cd0538311b58b344d

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