From 8f398849edb8587d1063280db0cc992abf58c0cf Mon Sep 17 00:00:00 2001 From: Kyle Bowman Date: Sat, 25 Jan 2025 14:04:57 -0500 Subject: [PATCH] refactor: add nlitem to NomList --- src/nom/base.py | 32 +++++++++++++++++--------------- src/nom/entry.py | 4 ++-- src/nom/feed.py | 4 ++-- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/nom/base.py b/src/nom/base.py index ad13ae4..ef30a97 100644 --- a/src/nom/base.py +++ b/src/nom/base.py @@ -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) diff --git a/src/nom/entry.py b/src/nom/entry.py index 4fa8b94..a9fe39b 100644 --- a/src/nom/entry.py +++ b/src/nom/entry.py @@ -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 diff --git a/src/nom/feed.py b/src/nom/feed.py index 446af4f..be2b931 100644 --- a/src/nom/feed.py +++ b/src/nom/feed.py @@ -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): -- 2.39.5