Skip to main content

An ORM-like API for Python's RDFLib.

Project description

rdflib-orm

A Python ORM for working with RDF.

Usage

from rdflib import Graph
from rdflib.namespace import DCTERMS, SKOS, OWL, DCAT, RDFS, RDF

from rdflib_orm.db import Database
from rdflib_orm import models


class Common(models.Model):
   provenance = models.CharField(predicate=DCTERMS.provenance, lang='en', required=True)

   class Meta:
       mixin = True


class ConceptScheme(Common):
   class_type = models.IRIField(predicate=RDF.type, value=SKOS.ConceptScheme)
   title = models.CharField(predicate=DCTERMS.title, lang='en', required=True)
   description = models.CharField(predicate=SKOS.definition, lang='en', required=True)
   created = models.DateTimeField(DCTERMS.created, auto_now_add=True)
   modified = models.DateTimeField(DCTERMS.modified, auto_now=True)
   creator = models.IRIField(predicate=DCTERMS.creator, required=True)
   publisher = models.IRIField(predicate=DCTERMS.publisher, required=True)
   version = models.CharField(predicate=OWL.versionInfo, required=True)
   custodian = models.CharField(predicate=DCAT.contactPoint)
   see_also = models.IRIField(predicate=RDFS.seeAlso, many=True)

   def __repr__(self):
       return f'<{self.__uri__}>'


class Concept(Common):
   class_type = models.IRIField(predicate=RDF.type, value=SKOS.Concept)
   pref_label = models.CharField(predicate=SKOS.prefLabel, lang='en', required=True)
   alt_labels = models.CharField(predicate=SKOS.altLabel, lang='en', many=True)
   definition = models.CharField(predicate=SKOS.definition, lang='en', required=True)
   children = models.IRIField(predicate=SKOS.narrower, many=True, inverse=SKOS.broader)
   other_ids = models.CharField(predicate=SKOS.notation, many=True)
   home_vocab_uri = models.IRIField(predicate=RDFS.isDefinedBy)

   def __repr__(self):
       return f'<{self.__uri__}>'


if __name__ == '__main__':
   g = Graph()
   g.bind('dcterms', DCTERMS)
   g.bind('skos', SKOS)
   g.bind('dcat', DCAT)
   g.bind('owl', OWL)
   Database.set_db(g, base_uri='http://example.com/')

   concept_scheme = ConceptScheme(
       'https://linked.data.gov.au/def/concept_scheme',
       title='A concept scheme',
       description='A description of this concept scheme.',
       creator='https://linked.data.gov.au/org/cgi',  # Accepts a URIRef or a string since the field is an IRIField.
       publisher='https://linked.data.gov.au/org/ga',
       version='0.1.0',
       provenance='Generated using Python',
       custodian='A custodian name',
       see_also=['http://example.com', 'http://another.example.com']  # Accepts a Python list since Field's many=True
   )

   concept_scheme.save()  # Save to store - currently memory store, but works also for remote triplestore.

   concept_scheme.title = 'Modified concept scheme title'
   concept_scheme.save()  # Save changes - we changed the title field.

   concept_a = Concept(
       uri='https://linked.data.gov.au/def/concept_a',
       pref_label='A concept',
       alt_labels=['An alt label', 'another alt label'],
       definition='Definition of this concept.',
       # children=  # Optional field, no children on this concept.
       other_ids=['123', '456'],  # No language tag here :)
       home_vocab_uri=concept_scheme,  # Reference the Concept Scheme Python object directly.
       provenance = 'Generated using RDFLib',
   )

   concept_a.save()

   concept_b = Concept(
       uri='https://linked.data.gov.au/def/concept_b',
       pref_label='Another concept',
       # alt_labels=  # Alt labels are optional.
       definition='Definition is not optional.',
       children=[concept_a],  # Reference the previous concept Python object directly. Notice it will also add the inverse property :)
       # other_ids=  # Optional field again.
       home_vocab_uri=concept_scheme,
       provenance='Generated using rdflib-orm',
   )

   concept_b.save()

   # Let's do some queries.
   queryset = Concept.objects.all()
   print(queryset)
   # <QuerySet [<https://linked.data.gov.au/def/concept_b>, <https://linked.data.gov.au/def/concept_a>]>

   # Get object by URI.
   concept_result = Concept.objects.get('https://linked.data.gov.au/def/concept_a')
   print(concept_result)
   # <https://linked.data.gov.au/def/concept_a>
   print(concept_result.pref_label)
   # A concept
   print(concept_result.definition)
   # Definition of this concept.

   # Not implemented yet. Something planned for the future.
   # Get object by any property, e.g. notation.
   # concept_result = Concept.objects.get(other_ids=123)

   print(len(g))  # 28 triples

   g.serialize('output.ttl', format='turtle')
   """ # Output of Graph.serialize()
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<https://linked.data.gov.au/def/concept_a> a skos:Concept ;
   dcterms:provenance "Generated using RDFLib"@en ;
   rdfs:isDefinedBy <https://linked.data.gov.au/def/concept_scheme> ;
   skos:altLabel "An alt label"@en,
       "another alt label"@en ;
   skos:broader <https://linked.data.gov.au/def/concept_b> ;
   skos:definition "Definition of this concept."@en ;
   skos:notation "123",
       "456" ;
   skos:prefLabel "A concept"@en .

<https://linked.data.gov.au/def/concept_b> a skos:Concept ;
   dcterms:provenance "Generated using rdflib-orm"@en ;
   rdfs:isDefinedBy <https://linked.data.gov.au/def/concept_scheme> ;
   skos:definition "Definition is not optional."@en ;
   skos:narrower <https://linked.data.gov.au/def/concept_a> ;
   skos:prefLabel "Another concept"@en .

<https://linked.data.gov.au/def/concept_scheme> a skos:ConceptScheme ;
   dcterms:created "2021-11-28T09:52:06.626338"^^xsd:dateTime ;
   dcterms:creator <https://linked.data.gov.au/org/cgi> ;
   dcterms:modified "2021-11-28T09:52:06.630338"^^xsd:dateTime ;
   dcterms:provenance "Generated using Python"@en ;
   dcterms:publisher <https://linked.data.gov.au/org/ga> ;
   dcterms:title "Modified concept scheme title"@en ;
   rdfs:seeAlso <http://another.example.com>,
       <http://example.com> ;
   owl:versionInfo "0.1.0" ;
   skos:definition "A description of this concept scheme."@en ;
   dcat:contactPoint "A custodian name" .


   """

Running tests

pytest --cov=rdflib_orm --cov-report html

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

rdflib-orm-0.2.1.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

rdflib_orm-0.2.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file rdflib-orm-0.2.1.tar.gz.

File metadata

  • Download URL: rdflib-orm-0.2.1.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for rdflib-orm-0.2.1.tar.gz
Algorithm Hash digest
SHA256 a7049d0ef6c439350e88d945ec2bbd02106e7da73bf77e563ffca763f478cb19
MD5 3fb3750e61afef3e132be68db0977955
BLAKE2b-256 bd6f5e091e932984ff68ef55a8869b1cdaab69d286b38a2ca040a5f831cb53c2

See more details on using hashes here.

Provenance

File details

Details for the file rdflib_orm-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: rdflib_orm-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.8.2 pkginfo/1.8.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for rdflib_orm-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b4280550361f865a0e8827e11efec18d6e5e673ec777553acc009356b0cec79
MD5 b514a1eeb41b5ad040f417584ec4f3ed
BLAKE2b-256 3cc3a378d7167aa8aab89f6549b0374123a356ccb80c7ea922f0746e2038a67b

See more details on using hashes here.

Provenance

Supported by

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