From: Kyle Bowman Date: Mon, 20 Jan 2025 21:33:48 +0000 (-0500) Subject: convert dataclasses to pydantic BaseModels X-Git-Url: https://git.rocketbowman.com/?a=commitdiff_plain;h=341ceb0444967fddff8efe216e48ec348a86a81e;p=nom.git convert dataclasses to pydantic BaseModels --- diff --git a/requirements.txt b/requirements.txt index cff1edf..4cd8502 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,7 +1,18 @@ +annotated-types==0.7.0 certifi==2024.12.14 charset-normalizer==3.4.1 -feedparser==6.0.11 +feedparser==6.0.0 idna==3.10 +iniconfig==2.0.0 +# Editable Git install (nom==0.0.1) with either a deleted local remote or invalid URI: +# 'git@goddard.local:/srv/git/nom.git' +-e /home/kyle/projects/nom +packaging==24.2 +pluggy==1.5.0 +pydantic==2.10.5 +pydantic_core==2.27.2 +pytest==8.3.4 requests==2.32.3 sgmllib3k==1.0.0 +typing_extensions==4.12.2 urllib3==2.3.0 diff --git a/src/nom/base.py b/src/nom/base.py index bcbbc41..c8b3c14 100644 --- a/src/nom/base.py +++ b/src/nom/base.py @@ -2,11 +2,12 @@ from abc import abstractmethod from csv import DictReader, DictWriter, excel_tab from copy import copy from pathlib import Path +from pydantic import BaseModel from nom.utils import NomError -class NomListItem: +class NomListItem(BaseModel): @abstractmethod def __hash__(self): @@ -15,7 +16,7 @@ class NomListItem: @classmethod def get_fieldnames(cls): - return cls.__dataclass_fields__.keys() + return cls.__fields__.keys() # TODO: What if there's a pipe in one of the fields? def to_str(self, delimiter: str ='|'): diff --git a/src/nom/entry.py b/src/nom/entry.py index 3ef20d8..7116944 100644 --- a/src/nom/entry.py +++ b/src/nom/entry.py @@ -1,4 +1,3 @@ -from dataclasses import dataclass from pathlib import Path from typing import Optional @@ -7,7 +6,6 @@ from nom.base import NomList, NomListItem # TODO: Use proper types, not strings. (Pydantic?) -@dataclass class EntryListItem(NomListItem): title: str url: str diff --git a/src/nom/feed.py b/src/nom/feed.py index 923f9de..b4324d2 100644 --- a/src/nom/feed.py +++ b/src/nom/feed.py @@ -1,7 +1,6 @@ import os from pathlib import Path from typing import Optional -from dataclasses import dataclass import feedparser import requests @@ -16,20 +15,24 @@ class Feed: def __init__(self, feedparsable): d = feedparser.parse(feedparsable) self.name = d.feed.title - self.url = d.feed.link # how is this different from d.feed.link? + self.url = d.feed.link self.entries = d.entries def to_entrylist(self)->EntryList: items = [] for e in self.entries: entry = EntryListItem( - e.title, e.link, e.updated, - self.url, "no alias", "False", "no summary") + title=e.title, + url=e.link, + date=e.updated, + feed_url=self.url, + feed_alias="no alias", + viewed="False", + summary="no summary") items.append(entry) return EntryList(items=items) -@dataclass class FeedListItem(NomListItem): url: str