From f5d7e7dedf49b20a6dcc0acaf8ffc5c621e8cc25 Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Mon, 13 Sep 2021 02:29:02 +0200 Subject: [PATCH] eafp principle --- fftcgtool/carddb.py | 17 +++++++++++------ fftcgtool/opus.py | 5 ++++- fftcgtool/ttsdeck.py | 8 +++++--- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/fftcgtool/carddb.py b/fftcgtool/carddb.py index 9caee37..951887a 100644 --- a/fftcgtool/carddb.py +++ b/fftcgtool/carddb.py @@ -31,11 +31,16 @@ class CardDB: def __init__(self, db_url: str = None): if db_url is not None: - res = requests.get(db_url, stream=True) - if not res.ok: - raise ValueError("Invalid URL given to CardDB!") + try: + with open(db_url, "rb") as db_file: + self._load(db_file) - self._load(io.BytesIO(res.content)) + except FileNotFoundError: + res = requests.get(db_url, stream=True) + if not res.ok: + raise ValueError("Invalid URL given to CardDB!") + + self._load(io.BytesIO(res.content)) def _load(self, db: str | PathLike[str] | IO[bytes]): try: @@ -60,9 +65,9 @@ class CardDB: return self._cards[code] def get_face_url(self, face: str) -> str: - if face in self._face_to_url: + try: return self._face_to_url[face] - else: + except KeyError: return face def save(self) -> None: diff --git a/fftcgtool/opus.py b/fftcgtool/opus.py index 3d27078..534bcca 100644 --- a/fftcgtool/opus.py +++ b/fftcgtool/opus.py @@ -73,10 +73,13 @@ class Opus(Cards): self.sort(key=lambda x: x.code.opus) for card in self: - if card.code in carddb: + try: for lang in API_LANGS: card[lang] = replace(card[lang], face=carddb[card.code][lang].face) + except KeyError: + pass + logger.debug(f"imported card {card}") @property diff --git a/fftcgtool/ttsdeck.py b/fftcgtool/ttsdeck.py index fd04448..f864b30 100644 --- a/fftcgtool/ttsdeck.py +++ b/fftcgtool/ttsdeck.py @@ -77,9 +77,9 @@ class TTSDeck(Cards): "Backup": 5, } - if data["type"] in key_prios: + try: return key_prios[data["type"]] - else: + except KeyError: return 4 def by_cost(data: dict[str, str | int]) -> int: @@ -125,8 +125,10 @@ class TTSDeck(Cards): # replace with normal-art cards for card in deck_cards: - if card["code"] in replace_full_arts: + try: card["code"] = replace_full_arts[card["code"]] + except KeyError: + pass codes = [ # create list of code objects