]> git.rocketbowman.com Git - nom.git/commitdiff
add simple filter functionality
authorKyle Bowman <kylebowman14@gmail.com>
Tue, 21 Jan 2025 01:05:29 +0000 (20:05 -0500)
committerKyle Bowman <kylebowman14@gmail.com>
Tue, 21 Jan 2025 01:05:29 +0000 (20:05 -0500)
src/nom/base.py
src/nom/entry.py
src/nom/feed.py
tests/test_entry.py

index 831a7bb5245d4de865c17b389d91aacd1e9eb4a9..8b79976e7ae89bfca16ed29a9877370d143f1562 100644 (file)
@@ -3,9 +3,12 @@ from csv import DictReader, DictWriter, excel_tab
 from copy import copy
 from pathlib import Path
 from pydantic import BaseModel
+from typing import Callable
 
 from nom.utils import NomError
 
+Predicate = Callable[..., bool]
+
 
 class NomListItem(BaseModel):
 
@@ -62,23 +65,22 @@ class NomList:
 
 
     # NOTE: To get the interface that I want (i.e `from_csv(path)`)
-    # each subclass must override from_csv and pass in the constructor
-    # of the corresponding ListItem. For example, for EntryList:
+    # each subclass must override from_csv and pass in the NomListItem
+    # that has a to_dict() method. For example, for EntryList:
     # ```
     # @classmethod
     # def from_csv(cls, path):
     #     return super().from_csv(path, EntryListItem)
     # ````
     @classmethod
-    def from_csv(cls, file: Path, constructor: NomListItem, delimiter="|"):
+    def from_csv(cls, file: Path, nlitem: NomListItem, delimiter="|"):
         items = []
         dialect = excel_tab
         dialect.delimiter=delimiter
         with open(file, "r") as f:
             reader = DictReader(f,dialect=dialect)
             for row in reader:
-                # TODO: This should be from_dict()
-                item = constructor(**row)
+                item = nlitem.from_dict(row)
                 items.append(item) 
         return cls(items=set(items), delimiter=delimiter)
 
@@ -100,4 +102,8 @@ class NomList:
         if not self.items:
             raise NomError("There are no entries to write.")
         for item in self.items:
-            print(item.to_str())
\ No newline at end of file
+            print(item.to_str())
+    
+def filter(nlist: NomList, predicate: Predicate):
+    items = {item for item in nlist.items if predicate(item)}
+    return nlist.__class__(items, delimiter=nlist.delimiter)
\ No newline at end of file
index 7116944adebb40e9074e1a4aca238de623341785..4fa8b94d936a7a929fb5ee94e01a3c4e5691cf3f 100644 (file)
@@ -12,9 +12,12 @@ class EntryListItem(NomListItem):
     date: str  
     feed_url: Optional[str] = ""
     feed_alias: Optional[str] = ""
-    viewed: Optional[bool] = "False"
+    viewed: Optional[bool] = False
     summary: Optional[str] = "" # TODO: Add this when you feel like stripping HTML
 
+    def __eq__(self, other):
+        return self.url == other.url
+
     def __hash__(self):
         return hash(self.url)
 
index b4324d2ed4161bc0521cca5e258f21961d8f8c48..7868ba2517fc68c5e6195d94258309fa7ac6c2a5 100644 (file)
@@ -27,7 +27,7 @@ class Feed:
                 date=e.updated, 
                 feed_url=self.url, 
                 feed_alias="no alias", 
-                viewed="False"
+                #viewed=False
                 summary="no summary")
             items.append(entry)
         return EntryList(items=items)
index 0317c04b6a84adff699dce02b80e928e00a618df..97f8f5db4ae723598e0b3a2adaaf79b595f70375 100644 (file)
@@ -4,9 +4,11 @@ from copy import copy
 import pytest
 
 from nom.entry import EntryList, EntryListItem
+from nom.filter import is_viewed
 from test_feed import feedlist
 
 
+
 @pytest.fixture
 def elist_single():
     path = Path(__file__).parent / "data" / "entry_single.csv"
@@ -44,3 +46,9 @@ def test_elist_addition(elist_multi, elist_single):
     sum_ = elist_multi + elist_single
     assert len(sum_) == len(elist_multi) + len(elist_single)
     assert isinstance(sum_,EntryList)
+
+def test_elist_filter(elist_multi):
+    #viewed = filter(is_viewed, elist_multi)
+    from nom.base import filter
+    viewed=filter(elist_multi, lambda e: e.viewed)
+    assert len(viewed) < len(elist_multi)