]> git.rocketbowman.com Git - nom.git/commitdiff
refactor: add nlitem to NomList
authorKyle Bowman <kyle+github@rocketbowman.com>
Sat, 25 Jan 2025 19:04:57 +0000 (14:04 -0500)
committerKyle Bowman <kyle+github@rocketbowman.com>
Sat, 25 Jan 2025 19:04:57 +0000 (14:04 -0500)
src/nom/base.py
src/nom/entry.py
src/nom/feed.py

index ad13ae4cd61213494ac9bcf4bda7b970b7bca6fd..ef30a972d3bed918e4bc97a75a60093f85ba47b1 100644 (file)
@@ -1,4 +1,4 @@
-from abc import abstractmethod
+from abc import abstractmethod, abstractclassmethod
 from csv import DictReader, DictWriter, excel_tab
 from copy import copy
 from pathlib import Path
@@ -15,8 +15,8 @@ class NomListItem(BaseModel):
 
     @abstractmethod
     def __hash__(self):
-        # Needed because line items are contained in sets, not lists.
-        raise NotImplementedError
+        msg = f"Define hash so {self.__class__} can determine uniqueness."
+        raise NotImplementedError(msg)
     
     @classmethod
     def get_fieldnames(cls):
@@ -36,16 +36,22 @@ class NomListItem(BaseModel):
 
 class NomList(set):
 
-    def __init__(self, elements: Optional[Iterable[NomListItem]]=None):
-        if not elements:
+    def __init__(self, items: Optional[Iterable]=None):
+        if not items:
             super().__init__()
         else:
-            super().__init__(elements)
+            nlitems = [self.nlitem().from_dict(item.to_dict()) for item in items]
+            super().__init__(nlitems)
 
         # Number items and persist via label attribute
-        for i, item in enumerate(self, start=1):
-            if not item.label:
-                item.__setattr__('label',i)
+        for i, nlitem in enumerate(self, start=1):
+            if not nlitem.label:
+                nlitem.__setattr__('label',i)
+
+    @abstractclassmethod
+    def nlitem(cls):
+        msg = "You must specify what type of NomListItem the list contains."
+        raise NotImplementedError(msg)
 
     def __add__(self, other):
         return self.__class__(self.union(other))
@@ -58,19 +64,15 @@ class NomList(set):
         for item in self.items:
             print(item.to_str(delimeter="\t"))
 
-
     @classmethod
-    def from_csv(cls, file: Path, nlitem: NomListItem, delimiter="|"):
-        # To parse NomListItems in a file, specify a specific NomListItem
-        if nlitem == NomListItem or type(nlitem) == NomListItem:
-            raise NotImplementedError(f"Override this method with super().__init__(file, {str(cls()).replace('()','')}Item) to enforce item parsing.")
+    def from_csv(cls, file: Path, delimiter="|"):
         items = []
         dialect = excel_tab
         dialect.delimiter=delimiter
         with open(file, "r") as f:
             reader = DictReader(f,dialect=dialect)
             for row in reader:
-                item = nlitem.from_dict(row)
+                item = cls.nlitem().from_dict(row)
                 items.append(item) 
         return cls(items)
 
index 4fa8b94d936a7a929fb5ee94e01a3c4e5691cf3f..a9fe39bb5eadf4ef1b06c347aa5baa264d8163eb 100644 (file)
@@ -25,5 +25,5 @@ class EntryListItem(NomListItem):
 class EntryList(NomList):
 
     @classmethod
-    def from_csv(cls, path: Path):
-        return super().from_csv(path, EntryListItem)
\ No newline at end of file
+    def nlitem(cls):
+        return EntryListItem
\ No newline at end of file
index 446af4f58b11209bbebf4ddcdc3e13b03eaee93a..be2b931d9da20578cd9b97330882faa5e538acc6 100644 (file)
@@ -58,8 +58,8 @@ class FeedListItem(NomListItem):
 class FeedList(NomList):
 
     @classmethod
-    def from_csv(cls, path: Path):
-        return super().from_csv(path, FeedListItem)
+    def nlitem(cls):
+        return FeedListItem
 
     @classmethod
     def from_file(cls, file: Path):