]> git.rocketbowman.com Git - nom.git/commitdiff
feat: implement label updating
authorKyle Bowman <kyle+github@rocketbowman.com>
Sat, 25 Jan 2025 19:45:32 +0000 (14:45 -0500)
committerKyle Bowman <kyle+github@rocketbowman.com>
Sat, 25 Jan 2025 19:45:32 +0000 (14:45 -0500)
src/nom/base.py
src/nom/main.py
tests/test_base.py [deleted file]
tests/test_entry.py

index ef30a972d3bed918e4bc97a75a60093f85ba47b1..5cd0591741a739f5ce8022b19e6fe59f5b6cc492 100644 (file)
@@ -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)}
index 6b62e68a2892de3dd443913dc0e4d3dffbfa48ef..2b55385f00fc2550f9756f019792905eda094c23 100644 (file)
@@ -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 (file)
index 492a89c..0000000
+++ /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)
index 30b307f4d81c694b18bfd6bbf8bb9a85e65cc0d0..42ee964e12a9534f2da07aa16d089e1be8f17840 100644 (file)
@@ -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)