feat(utils): add label_with_details helper
Builds a "Name (detail, detail)" label from a name plus optional details, dropping falsy parts and omitting the parentheses entirely when none remain. Extracted to deduplicate the "filter present parts, join, wrap in parens" idiom that several model display properties share. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -114,6 +114,17 @@ def format_float_or_int(number: int | float):
|
||||
return int(number) if float(number).is_integer() else f"{number:03.2f}"
|
||||
|
||||
|
||||
def label_with_details(name: str, *details: object, separator: str = ", ") -> str:
|
||||
"""Build a ``"Name (detail, detail)"`` label from a name and optional details.
|
||||
|
||||
Falsy details (``None``, ``""``, ``0``) are dropped; the rest are stringified
|
||||
and joined with ``separator`` inside parentheses. With no details remaining,
|
||||
the bare ``name`` is returned without parentheses.
|
||||
"""
|
||||
present = [str(detail) for detail in details if detail]
|
||||
return f"{name} ({separator.join(present)})" if present else name
|
||||
|
||||
|
||||
OperatorType = Literal["|", "&"]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
|
||||
from common.utils import label_with_details
|
||||
|
||||
|
||||
class LabelWithDetailsTest(unittest.TestCase):
|
||||
def test_all_parts_present(self):
|
||||
self.assertEqual(
|
||||
label_with_details("Mario", "Steam", 2020), "Mario (Steam, 2020)"
|
||||
)
|
||||
|
||||
def test_some_parts_falsy(self):
|
||||
self.assertEqual(label_with_details("Mario", None, 2020), "Mario (2020)")
|
||||
self.assertEqual(label_with_details("Mario", "Steam", None), "Mario (Steam)")
|
||||
|
||||
def test_all_parts_falsy(self):
|
||||
self.assertEqual(label_with_details("Mario", None, "", 0), "Mario")
|
||||
|
||||
def test_no_details(self):
|
||||
self.assertEqual(label_with_details("Mario"), "Mario")
|
||||
|
||||
def test_custom_separator(self):
|
||||
self.assertEqual(
|
||||
label_with_details("Mario", "a", "b", separator=" / "), "Mario (a / b)"
|
||||
)
|
||||
Reference in New Issue
Block a user