Skip to content

Commit 25669da

Browse files
authored
Update triples interface (#173)
1 parent 0d8c7b8 commit 25669da

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

src/curies/triples.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pathlib import Path
1010
from typing import TextIO
1111

12-
from pydantic import BaseModel
12+
from pydantic import BaseModel, ConfigDict
1313
from typing_extensions import Self
1414

1515
from .api import Reference
@@ -24,10 +24,16 @@
2424
class Triple(BaseModel):
2525
"""A model for a triple of subject-predicate-object triple."""
2626

27+
model_config = ConfigDict(frozen=True)
28+
2729
subject: Reference
2830
predicate: Reference
2931
object: Reference
3032

33+
def as_curies(self) -> tuple[str, str, str]:
34+
"""Get a three-tuple of strings representing this triple."""
35+
return self.subject.curie, self.predicate.curie, self.object.curie
36+
3137
@classmethod
3238
def from_curies(
3339
cls,

tests/test_triples.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,33 @@
55
import unittest
66
from pathlib import Path
77

8-
from curies import Triple
8+
import pydantic
9+
10+
from curies import Reference, Triple
911
from curies.triples import read_triples, write_triples
1012

13+
T1 = Triple.from_curies("a:1", "a:2", "a:3")
14+
T2 = Triple.from_curies("a:1", "a:2", "a:4")
15+
1116

1217
class TestTriples(unittest.TestCase):
1318
"""Test triples."""
1419

20+
def test_immutable(self) -> None:
21+
"""Test immutable."""
22+
with self.assertRaises(pydantic.ValidationError):
23+
T1.subject = Reference.from_curie("b:1") # type:ignore
24+
25+
def test_as_curies(self) -> None:
26+
"""Test stringifying."""
27+
self.assertEqual(
28+
("a:1", "a:2", "a:3"),
29+
T1.as_curies(),
30+
)
31+
1532
def test_roundtrip(self) -> None:
1633
"""Test roundtrip."""
17-
triples = [
18-
Triple.from_curies("a:1", "a:2", "a:3"),
19-
Triple.from_curies("a:1", "a:2", "a:4"),
20-
]
34+
triples = [T1, T2]
2135
with tempfile.TemporaryDirectory() as directory:
2236
paths = [
2337
Path(directory).joinpath("test.tsv.gz"),

0 commit comments

Comments
 (0)