Skip to main content

Linked data class python package

Project description

DOI PyPI-Server Coveralls Project generated with PyScaffold

oold-python

Linked data class python package for object oriented linked data (OO-LD). This package aims to implemment this functionality independent from the osw-python package - work in progress.

Features

Code Generation

Generate Python data models from OO-LD Schemas (based on datamodel-code-generator):

from oold.generator import Generator
import importlib
import datamodel_code_generator
import oold.model.model as model

schemas = [
    {   # minimal example
        "id": "Foo",
        "title": "Foo",
        "type": "object",
        "properties": {
            "id": {"type": "string"},
        },
    },
]
g = Generator()
g.generate(schemas, main_schema="Foo.json", output_model_type=datamodel_code_generator.DataModelType.PydanticBaseModel)
importlib.reload(model)

# Now you can work with your generated model
f = model.Foo(id="ex:f")
print(f)

This example uses the built-in Generator to create a basic Pydantic model (v1 or v2) from JSON schemas.

More details see example code

Object Graph Mapping

Concept

Illustrative example how the object orient linked data (OO-LD) package provides an abstract knowledge graph (KG) interface. First (line 3) primary schemas (Foo) and their dependencies (Bar, Baz) are loaded from the KG and transformed into python dataclasses. Instantiation of foo is handled by loading the respective JSON(-LD) document from the KG and utilizing the type relation to the corresponding schema and dataclass (line 5). Because bar is not a dependent subobject of foo it is loaded on-demand on first access of the corresponding class attribute of foo (foo.bar in line 7), while id as dependent literal is loaded immediately in the same operation. In line 9 baz is constructed by an existing controller class subclassing Foo and finally stored as a new entity in the KG in line 11.

Represent your domain objects easily and reference them via IRIs or direct object instances. For instance, if you have a Foo model referencing a Bar model:

import oold.model.model as model

# Create a Foo object linked to Bar
f = model.Foo(
    id="ex:f",
    literal="test1",
    b=model.Bar(id="ex:b", prop1="test2"),
    b2=[model.Bar(id="ex:b1", prop1="test3"), model.Bar(id="ex:b2", prop1="test4")],
)

print(f.b.id)          # ex:b
print(f.b2[0].prop1)   # test3

You can also refer to objects by IRI:

# Assign IRI strings directly
f = model.Foo(
    id="ex:f",
    literal="test1",
    b="ex:b",  # automatically resolved to a Bar object
    b2=["ex:b1", "ex:b2"],
)

Thanks to the resolver mechanism, these IRIs turn into fully-fledged objects as soon as you need them.

More details see example code

RDF-Export

Easily convert your objects to RDF (JSON-LD) and integrate with SPARQL queries:

from rdflib import Graph
from typing import List, Optional

# Example: Convert Person objects to RDF
p1 = model.Person(name="Alice")
p2 = model.Person(name="Bob", knows=[p1])

# Export to JSON-LD
print(p2.to_jsonld())

# Load into RDFlib
g = Graph()
g.parse(data=p1.to_jsonld(), format="json-ld")
g.parse(data=p2.to_jsonld(), format="json-ld")

# Perform SPARQL queries
qres = g.query("""
    SELECT ?name
    WHERE {
        ?s <https://schema.org/knows> ?o .
        ?o <https://schema.org/name> ?name .
    }
""")
for row in qres:
    print("Bob knows", row.name)

The extended dataclass notation includes semantic annotations as JSON-LD context, giving you powerful tooling for knowledge graphs, semantic queries, and data interoperability.

More details see example code

Dev

git clone https://github.com/OpenSemanticWorld/oold-python
pip install -e .[dev]

Note

This project has been set up using PyScaffold 4.5. For details and usage information on PyScaffold see https://pyscaffold.org/.

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

oold-0.6.0.tar.gz (520.6 kB view details)

Uploaded Source

Built Distribution

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

oold-0.6.0-py3-none-any.whl (25.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for oold-0.6.0.tar.gz
Algorithm Hash digest
SHA256 f70abaa684520b1fd88aad04bbe57e3617948c6cef8c63c88035831e6aa72a7e
MD5 9ba21e5b8708525557e36f44cd8780a2
BLAKE2b-256 aa5b7f67c6c4d500da0067a765b4f8771c5bbe8de3411d4de4edd4cf56e1b6f4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for oold-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb1d3f7b97b7205d591619d42f36022825436c90a0d7853d848243fa2b028a5f
MD5 51236e4f476ed2385f50dc1fea5e3441
BLAKE2b-256 6dc3eb23aa8ca89e1b52b04b02554b37e4f5c2329d92a2f6c9000adf49cb2605

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