From 0f176f44ea93b0310989b9efc363a328eea4a5ee Mon Sep 17 00:00:00 2001 From: LDericher <40151420+ldericher@users.noreply.github.com> Date: Fri, 3 Sep 2021 07:34:12 +0200 Subject: [PATCH] type hints --- fftcg/card.py | 4 ++-- fftcg/grid.py | 6 +++--- fftcg/language.py | 4 ++-- fftcg/opus.py | 35 +++++++++++++++++++++++------------ fftcg/utils.py | 6 ++++-- 5 files changed, 34 insertions(+), 21 deletions(-) diff --git a/fftcg/card.py b/fftcg/card.py index f862506..5c6fe55 100644 --- a/fftcg/card.py +++ b/fftcg/card.py @@ -45,10 +45,10 @@ class Card: ) else: - def sub_encircle(match: re.Match): + def sub_encircle(match: re.Match) -> str: return encircle_symbol(match.group(1), False) - def sub_elements(match: re.Match): + def sub_elements(match: re.Match) -> str: return encircle_symbol(Card.__ELEMENTS_MAP[match.group(1)], False) def load_name(language: Language) -> str: diff --git a/fftcg/grid.py b/fftcg/grid.py index 10d7ac9..acfd61e 100644 --- a/fftcg/grid.py +++ b/fftcg/grid.py @@ -6,14 +6,14 @@ class Grid(tuple[int, int]): return Grid((self.x * other.x, self.y * other.y)) @property - def x(self): + def x(self) -> int: return self[0] @property - def y(self): + def y(self) -> int: return self[1] @property - def capacity(self): + def capacity(self) -> int: # capacity of grid (reserve last space for card back) return self.x * self.y - 1 diff --git a/fftcg/language.py b/fftcg/language.py index 3f6423f..182ee9c 100644 --- a/fftcg/language.py +++ b/fftcg/language.py @@ -19,7 +19,7 @@ class Language: object.__setattr__(self, "short", "en") @property - def image_suffix(self): + def image_suffix(self) -> str: # supported languages for face URLs if self.short in ["de", "es", "fr", "it"]: return self.short @@ -27,7 +27,7 @@ class Language: return "eg" @property - def key_suffix(self): + def key_suffix(self) -> str: # supported languages for Square API if self.short in ["de", "es", "fr", "it"]: return f"_{self.short.upper()}" diff --git a/fftcg/opus.py b/fftcg/opus.py index a35c72f..0bb2f64 100644 --- a/fftcg/opus.py +++ b/fftcg/opus.py @@ -1,5 +1,6 @@ import logging from dataclasses import replace +from typing import Callable, Iterator import requests import roman @@ -81,7 +82,7 @@ class Opus(Cards): return self.__number @property - def elemental_decks(self) -> list[TTSDeck]: + def elemental_decks(self) -> Iterator[TTSDeck]: if self.number in ["PR", "B"]: return [TTSDeck( codes=[ @@ -94,7 +95,7 @@ class Opus(Cards): )] else: - def element_filter(element: str): + def element_filter(element: str) -> Callable[[Card], list[str]]: return lambda card: card.elements == [element] # simple cases: create lambdas for base elemental decks @@ -116,13 +117,23 @@ class Opus(Cards): cards.sort(key=lambda x: x[self.__language].name) cards.sort(key=lambda x: "Multi" if len(x.elements) > 1 else x.elements[0]) - return [TTSDeck( - codes=[ - card.code - for card in cards - if f(card) - ], - name=f"{self.name} {elem}", - description=f"All {self.name} Cards with {elem} element in alphabetical order", - face_down=False, - ) for elem, f in filters.items()] + # generate decks + decks = ( + TTSDeck( + codes=[ + card.code + for card in cards + if f(card) + ], + name=f"{self.name} {elem}", + description=f"All {self.name} Cards with {elem} element in alphabetical order", + face_down=False, + ) for elem, f in filters.items() + ) + + # Ignore empty decks + return ( + deck + for deck in decks + if deck + ) diff --git a/fftcg/utils.py b/fftcg/utils.py index bda6858..849bc9b 100644 --- a/fftcg/utils.py +++ b/fftcg/utils.py @@ -1,3 +1,5 @@ +from typing import Iterator + from PIL import Image from .grid import Grid @@ -13,7 +15,7 @@ CARD_BACK_URL = "http://cloud-3.steamusercontent.com/ugc/948455238665576576/8506 # functions -def encircle_symbol(symbol: str, negative: bool): +def encircle_symbol(symbol: str, negative: bool) -> str: symbol = symbol[0].upper() base_symbols: tuple[str, str] = "", "" @@ -37,7 +39,7 @@ def encircle_symbol(symbol: str, negative: bool): return chr(ord(base_symbols[1]) + symbol_num) -def chunks(chunk_size: int, whole: list): +def chunks(chunk_size: int, whole: list) -> Iterator[list]: # while there are elements while whole: # get a chunk