Skip to content

Commit

Permalink
Add intermediate named reference class (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt authored Feb 5, 2025
1 parent c4fb747 commit 4a42b1a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/curies/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
DuplicatePrefixes,
DuplicateURIPrefixes,
DuplicateValueError,
NamableReference,
NamedReference,
Prefix,
PrefixMap,
Expand Down Expand Up @@ -39,6 +40,7 @@
"DuplicatePrefixes",
"DuplicateURIPrefixes",
"DuplicateValueError",
"NamableReference",
"NamedReference",
"Prefix",
"PrefixMap",
Expand Down
42 changes: 41 additions & 1 deletion src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"DuplicatePrefixes",
"DuplicateURIPrefixes",
"DuplicateValueError",
"NamableReference",
"NamedReference",
"Prefix",
"PrefixMap",
Expand Down Expand Up @@ -458,7 +459,46 @@ def from_curie(
return cls.model_validate({"prefix": prefix, "identifier": identifier}, context=converter)


class NamedReference(Reference):
class NamableReference(Reference):
"""A reference, maybe with a name."""

name: str | None = Field(
None,
description="The name of the entity referenced by this object's prefix and identifier, if exists.",
)

model_config = ConfigDict(frozen=True)

@classmethod
def from_curie( # type:ignore
cls,
curie: str,
name: str | None = None,
*,
sep: str = ":",
converter: Converter | None = None,
) -> NamableReference:
"""Parse a CURIE string and populate a reference.
:param curie: A string representation of a compact URI (CURIE)
:param name: The optional name of the reference
:param sep: The separator
:param converter: The converter to use as context when parsing
:return: A reference object
>>> NamableReference.from_curie("chebi:1234")
NamableReference(prefix='chebi', identifier='1234', name=None)
>>> NamableReference.from_curie("chebi:1234", "6-methoxy-2-octaprenyl-1,4-benzoquinone")
NamableReference(prefix='chebi', identifier='1234', name='6-methoxy-2-octaprenyl-1,4-benzoquinone')
"""
prefix, identifier = _split(curie, sep=sep)
return cls.model_validate(
{"prefix": prefix, "identifier": identifier, "name": name}, context=converter
)


class NamedReference(NamableReference):
"""A reference with a name."""

name: str = Field(
Expand Down
4 changes: 4 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DuplicatePrefixes,
DuplicateURIPrefixes,
ExpansionError,
NamableReference,
NamedReference,
NoCURIEDelimiterError,
PrefixStandardizationError,
Expand Down Expand Up @@ -108,7 +109,9 @@ def test_named_set_membership(self) -> None:
NamedReference.from_curie("a:2", "name2"),
}
self.assertIn(Reference.from_curie("a:1"), references)
self.assertIn(NamableReference.from_curie("a:1"), references)
self.assertIn(NamedReference.from_curie("a:1", "name1"), references)
self.assertIn(NamableReference.from_curie("a:1", "name1"), references)
# the following is a weird case, but shows how this works
self.assertIn(NamedReference.from_curie("a:1", "name2"), references)

Expand All @@ -117,6 +120,7 @@ def test_named_set_membership(self) -> None:
Reference.from_curie("a:2"),
}
self.assertIn(Reference.from_curie("a:1"), references_2)
self.assertIn(NamableReference.from_curie("a:1", "name1"), references_2)
self.assertIn(NamedReference.from_curie("a:1", "name1"), references_2)


Expand Down

0 comments on commit 4a42b1a

Please sign in to comment.