Skip to content

lodkit.ttl could overload __or__ for chaining #32

@lu-pl

Description

@lu-pl
from collections.abc import Iterator
import itertools
from typing import Self

from lodkit import _Triple, ttl
from rdflib import Namespace, RDF, RDFS


ex = Namespace("https://example.com/")

class chainable_ttl(ttl):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._chain: list[ttl] = []

    def __iter__(self) -> Iterator[_Triple]:
        yield from super().__iter__()
        yield from itertools.chain.from_iterable(self._chain)

    def __or__(self, other: ttl) -> Self:
        self._chain.append(other)
        return self


t1 = chainable_ttl(ex.s, (RDF.type, ex["class"]))
t2 = chainable_ttl(ex.s, (RDFS.label, "label"))

triples: ttl = t1 | t2

graph = triples.to_graph()
print(graph.serialize())

Result:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

<https://example.com/s> a <https://example.com/class> ;
    rdfs:label "label" .

Metadata

Metadata

Assignees

Labels

deferredIssue resolution is deferred and no further work on this is currently planned.enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions