From: Kyle Bowman Date: Sat, 25 Jan 2025 19:45:32 +0000 (-0500) Subject: feat: implement label updating X-Git-Url: https://git.rocketbowman.com/?a=commitdiff_plain;h=3442d0cad004933832609054e56f85702029dcae;p=nom.git feat: implement label updating --- diff --git a/src/nom/base.py b/src/nom/base.py index ef30a97..5cd0591 100644 --- a/src/nom/base.py +++ b/src/nom/base.py @@ -11,7 +11,7 @@ Predicate = Callable[..., bool] class NomListItem(BaseModel): - label: Optional[int] = -1 + label: Optional[int] = None @abstractmethod def __hash__(self): @@ -42,19 +42,22 @@ class NomList(set): else: nlitems = [self.nlitem().from_dict(item.to_dict()) for item in items] super().__init__(nlitems) - - # Number items and persist via label attribute - for i, nlitem in enumerate(self, start=1): - if not nlitem.label: - nlitem.__setattr__('label',i) + self.update_labels() @abstractclassmethod def nlitem(cls): msg = "You must specify what type of NomListItem the list contains." raise NotImplementedError(msg) + def update_labels(self, force=False): + for i, nlitem in enumerate(self, start=1): + if not nlitem.label or force: + nlitem.__setattr__('label',i) + def __add__(self, other): - return self.__class__(self.union(other)) + sum_ = self.__class__(self.union(other)) + sum_.update_labels(force=True) + return sum_ def select(self, predicate: Predicate): items = {item for item in self if predicate(item)} diff --git a/src/nom/main.py b/src/nom/main.py index 6b62e68..2b55385 100644 --- a/src/nom/main.py +++ b/src/nom/main.py @@ -46,6 +46,7 @@ def main(args=['nom'].append(sys.argv)): elist = EntryList.from_csv(ENTRY_LIST) elist.to_stdout() elif args.command == "feed" and args.feed_command == "update": + feedlist.update_labels() feedlist.fetch_feeds(FEED_CACHE) elif args.command == "feed" and args.feed_command == "show": feedlist.to_stdout() diff --git a/tests/test_base.py b/tests/test_base.py deleted file mode 100644 index 492a89c..0000000 --- a/tests/test_base.py +++ /dev/null @@ -1,16 +0,0 @@ -# Note: The base module isn't meant to be used directly, but instead is meant -# to be subclassed from. In general, it makes more sense to test the subclasses -# than it does to test the base class. - -import pytest -from pathlib import Path -from nom.base import NomList, NomListItem - - - -def test_from_csv_error(): - class DummyList(NomList): - pass - path = Path(__file__).parent / "data" / "entry_single.csv" - with pytest.raises(NotImplementedError): - nlist=DummyList.from_csv(path, NomListItem) diff --git a/tests/test_entry.py b/tests/test_entry.py index 30b307f..42ee964 100644 --- a/tests/test_entry.py +++ b/tests/test_entry.py @@ -41,6 +41,11 @@ def test_elist_addition(elist_multi, elist_single): assert len(sum_) == len(elist_multi) + len(elist_single) assert isinstance(sum_,EntryList) +def test_elist_addition_unique_labels(elist_multi,elist_single): + sum_ = elist_multi + elist_single + labels = {item.label for item in sum_} + assert len(labels) == len(sum_) + def test_elist_select(elist_multi): viewed = elist_multi.select(lambda e: e.viewed) assert len(viewed) < len(elist_multi)