Skip to content

Commit

Permalink
feat(FCL-533): getting scored or preferred identifiers can now be don…
Browse files Browse the repository at this point in the history
…e by type
  • Loading branch information
jacksonj04 committed Dec 18, 2024
1 parent 22cc54d commit 7cf2d3c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/caselawclient/models/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _initialise_identifiers(self) -> None:
@property
def best_human_identifier(self) -> Optional[Identifier]:
"""Return the preferred identifier for the document, providing that it is considered human readable."""
preferred_identifier = self.identifiers.preferred
preferred_identifier = self.identifiers.preferred()
if preferred_identifier and preferred_identifier.schema.human_readable:
return preferred_identifier
return None
Expand Down
23 changes: 15 additions & 8 deletions src/caselawclient/models/identifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,21 @@ def as_etree(self) -> etree._Element:

return identifiers_root

@property
def by_score(self) -> list[Identifier]:
"""Return a list of identifiers, sorted by their score in descending order."""
return sorted(self.values(), key=lambda v: v.score, reverse=True)
def by_score(self, type: Optional[type[Identifier]] = None) -> list[Identifier]:
"""
:param type: Optionally, an identifier type to constrain this list to.
@property
def preferred(self) -> Optional[Identifier]:
"""If this document has identifiers, return the preferred one."""
:return: Return a list of identifiers, sorted by their score in descending order.
"""
identifiers = self.of_type(type) if type else list(self.values())
return sorted(identifiers, key=lambda v: v.score, reverse=True)

def preferred(self, type: Optional[type[Identifier]] = None) -> Optional[Identifier]:
"""
:param type: Optionally, an identifier type to constrain the results to.
:return: Return the highest scoring identifier of the given type (or of any type, if none is specified). Returns `None` if no identifier is available.
"""
if len(self) == 0:
return None
return self.by_score[0]
return self.by_score(type)[0]
10 changes: 8 additions & 2 deletions tests/models/identifiers/test_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ def test_base_scoring(self):
assert identifier.score == 2.5

def test_sorting(self, mixed_identifiers):
assert mixed_identifiers.by_score == [TEST_IDENTIFIER_999, TEST_NCN_1701, TEST_NCN_1234]
assert mixed_identifiers.by_score() == [TEST_IDENTIFIER_999, TEST_NCN_1701, TEST_NCN_1234]

def test_preferred_identifier(self, mixed_identifiers):
assert mixed_identifiers.preferred == TEST_IDENTIFIER_999
assert mixed_identifiers.preferred() == TEST_IDENTIFIER_999

def test_sorting_with_type(self, mixed_identifiers):
assert mixed_identifiers.by_score(type=NeutralCitationNumber) == [TEST_NCN_1701, TEST_NCN_1234]

def test_preferred_identifier_with_type(self, mixed_identifiers):
assert mixed_identifiers.preferred(type=NeutralCitationNumber) == TEST_NCN_1701

0 comments on commit 7cf2d3c

Please sign in to comment.