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):
# 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)
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
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)
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"
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)