Skip to content

Commit

Permalink
Refactor splitting code (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
cthoyt authored Oct 30, 2024
1 parent 18de915 commit bf166bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ def _get_field_validator_values(values, key: str): # type:ignore
return values.data[key]


def _split(curie: str, *, sep: str = ":") -> tuple[str, str]:
prefix, delimiter, identifier = curie.partition(sep)
if not delimiter:
raise NoCURIEDelimiterError(curie)
return prefix, identifier


class ReferenceTuple(NamedTuple):
"""A pair of a prefix (corresponding to a semantic space) and a local unique identifier in that semantic space.
Expand Down Expand Up @@ -129,7 +136,7 @@ def curie(self) -> str:
return f"{self.prefix}:{self.identifier}"

@classmethod
def from_curie(cls, curie: str, sep: str = ":") -> "ReferenceTuple":
def from_curie(cls, curie: str, *, sep: str = ":") -> "ReferenceTuple":
"""Parse a CURIE string and populate a reference tuple.
:param curie: A string representation of a compact URI (CURIE)
Expand All @@ -139,7 +146,7 @@ def from_curie(cls, curie: str, sep: str = ":") -> "ReferenceTuple":
>>> ReferenceTuple.from_curie("chebi:1234")
ReferenceTuple(prefix='chebi', identifier='1234')
"""
prefix, identifier = curie.split(sep, 1)
prefix, identifier = _split(curie, sep=sep)
return cls(prefix, identifier)


Expand Down Expand Up @@ -219,7 +226,7 @@ def pair(self) -> ReferenceTuple:
return ReferenceTuple(self.prefix, self.identifier)

@classmethod
def from_curie(cls, curie: str, sep: str = ":") -> "Reference":
def from_curie(cls, curie: str, *, sep: str = ":") -> "Reference":
"""Parse a CURIE string and populate a reference.
:param curie: A string representation of a compact URI (CURIE)
Expand All @@ -229,7 +236,7 @@ def from_curie(cls, curie: str, sep: str = ":") -> "Reference":
>>> Reference.from_curie("chebi:1234")
Reference(prefix='chebi', identifier='1234')
"""
prefix, identifier = curie.split(sep, 1)
prefix, identifier = _split(curie, sep=sep)
return cls(prefix=prefix, identifier=identifier)


Expand Down Expand Up @@ -317,6 +324,17 @@ class DuplicateSummary(NamedTuple):
prefix: str


class NoCURIEDelimiterError(ValueError):
"""An error thrown on a string with no delimiter."""

def __init__(self, curie: str):
"""Initialize the error."""
self.curie = curie

def __str__(self) -> str:
return f"{self.curie} does not appear to be a CURIE - missing a delimiter"


class DuplicateValueError(ValueError):
"""An error raised with constructing a converter with data containing duplicate values."""

Expand Down
7 changes: 7 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,
NoCURIEDelimiterError,
PrefixStandardizationError,
Record,
Records,
Expand All @@ -43,6 +44,12 @@
class TestStruct(unittest.TestCase):
"""Test the data structures."""

def test_not_curie(self):
"""Test a malformed CURIE."""
with self.assertRaises(NoCURIEDelimiterError) as e:
Reference.from_curie("not a curie")
self.assertIn("does not appear to be a CURIE", str(e.exception))

def test_default_prefix(self) -> None:
"""Test a default (empty) prefix."""
ref = Reference.from_curie(":something")
Expand Down

0 comments on commit bf166bb

Please sign in to comment.