Skip to main content

No project description provided

Project description

RDFProxy

tests coverage docs License: GPL v3 Ruff uv

Functionality for mapping SPARQL result sets to Pydantic models.

SPARQLModelAdapter

The rdfproxy.SPARQLModelAdapter class allows to run a query against an endpoint and map a flat SPARQL query result set to a potentially nested Pydantic model. The result is returned as a Page object.

The following example query

select *
where {
    values (?gnd ?authorName ?educatedAt ?workName ?work ?viaf) {
        (119359464 'Schindel' UNDEF 'Gebürtig' <http://www.wikidata.org/entity/Q1497409> UNDEF)
        (115612815 'Geiger' 'University of Vienna' 'Der alte König in seinem Exil' <http://www.wikidata.org/entity/Q15805238> 299260555)
        (115612815 'Geiger' 'University of Vienna' 'Der alte König in seinem Exil' <http://www.wikidata.org/entity/Q15805238> 6762154387354230970008)
        (115612815 'Geiger' 'University of Vienna' 'Unter der Drachenwand' <http://www.wikidata.org/entity/Q58038819> 2277151717053313900002)
        (1136992030 'Edelbauer' 'University of Vienna' 'Das flüssige Land' <http://www.wikidata.org/entity/Q100266054> UNDEF)
        (1136992030 'Edelbauer' 'University of Applied Arts Vienna' 'Das flüssige Land' <http://www.wikidata.org/entity/Q100266054> UNDEF)
    }
}

retrieves the result set:

gnd nameLabel educated_atLabel work_name work viaf
119359464 Schindel Gebürtig http://www.wikidata.org/entity/Q1497409
115612815 Geiger University of Vienna Der alte König in seinem Exil http://www.wikidata.org/entity/Q15805238 299260555
115612815 Geiger University of Vienna Der alte König in seinem Exil http://www.wikidata.org/entity/Q15805238 6762154387354230970008
115612815 Geiger University of Vienna Unter der Drachenwand http://www.wikidata.org/entity/Q58038819 2277151717053313900002
1136992030 Edelbauer University of Vienna Das flüssige Land http://www.wikidata.org/entity/Q100266054
1136992030 Edelbauer University of Applied Arts Vienna Das flüssige Land http://www.wikidata.org/entity/Q100266054

The result set can be mapped to a nested Pydantic model like so:

from typing import Annotated

from fastapi import FastAPI, Query
from pydantic import BaseModel
from rdfproxy import ConfigDict, Page, QueryParameters, SPARQLBinding, SPARQLModelAdapter

class Work(BaseModel):
    model_config = ConfigDict(group_by="name")

    name: Annotated[str, SPARQLBinding("workName")]
    viafs: Annotated[list[str], SPARQLBinding("viaf")]

class Author(BaseModel):
    model_config = ConfigDict(group_by="surname")

    gnd: str
    surname: Annotated[str, SPARQLBinding("authorName")]
    works: list[Work]
    education: Annotated[list[str], SPARQLBinding("educatedAt")]

adapter = SPARQLModelAdapter(
    target="https://query.wikidata.org/bigdata/namespace/wdq/sparql",
    query=query,
    model=Author,
)

The SPARQLModelAdapter.query method runs the query and constructs a Page object which can then be served over a FastAPI route:

app = FastAPI()

@app.get("/")
def base_route(query_parameters: Annotated[QueryParameters, Query()]) -> Page[Author]:
    return adapter.query(query_parameters)

This results in the following JSON output:

{
   "items":[
      {
         "gnd":"119359464",
         "surname":"Schindel",
         "works":[
            {
               "name":"Gebürtig",
               "viafs":[
                  
               ]
            }
         ],
         "education":[
            
         ]
      },
      {
         "gnd":"115612815",
         "surname":"Geiger",
         "works":[
            {
               "name":"Der alte König in seinem Exil",
               "viafs":[
                  "299260555",
                  "6762154387354230970008"
               ]
            },
            {
               "name":"Unter der Drachenwand",
               "viafs":[
                  "2277151717053313900002"
               ]
            }
         ],
         "education":[
            "University of Vienna"
         ]
      },
      {
         "gnd":"1136992030",
         "surname":"Edelbauer",
         "works":[
            {
               "name":"Das flüssige Land",
               "viafs":[
                  
               ]
            }
         ],
         "education":[
            "University of Vienna",
            "University of Applied Arts Vienna"
         ]
      }
   ],
   "page":1,
   "size":100,
   "total":3,
   "pages":1
}

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

rdfproxy-0.6.0.tar.gz (111.9 kB view details)

Uploaded Source

Built Distribution

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

rdfproxy-0.6.0-py3-none-any.whl (34.1 kB view details)

Uploaded Python 3

File details

Details for the file rdfproxy-0.6.0.tar.gz.

File metadata

  • Download URL: rdfproxy-0.6.0.tar.gz
  • Upload date:
  • Size: 111.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rdfproxy-0.6.0.tar.gz
Algorithm Hash digest
SHA256 3cd511acfa4499282d4ab83893df2f92b66efcae7a2305d2e00bbf47b70ae91e
MD5 dfde4948c9bdee31e208d03663175454
BLAKE2b-256 d1a90f7502ef869440fcf1c560c7bf7685bf02c2be6cb57a28b6777b67faa1cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for rdfproxy-0.6.0.tar.gz:

Publisher: publish.yml on acdh-oeaw/rdfproxy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rdfproxy-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: rdfproxy-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 34.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for rdfproxy-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 669553992e2b5d0289ce96cf924e5b6f5258335682ea15aa23959c744195e4e6
MD5 3cc075b3a47c8fafcfc51955eac0aab4
BLAKE2b-256 70656846cf4c74c7a8ad322434a3b369f9ff56c9a274ec4b7bb66bbe212e66de

See more details on using hashes here.

Provenance

The following attestation bundles were made for rdfproxy-0.6.0-py3-none-any.whl:

Publisher: publish.yml on acdh-oeaw/rdfproxy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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