From 2275fb3530c6836ffdb9051696cd804e561f1452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ola=20Sj=C3=B6lander?= Date: Sun, 9 Jul 2023 15:20:41 +0200 Subject: [PATCH] fixed type annotations --- treys/card.py | 14 +++++++------- treys/deck.py | 7 ++++--- treys/evaluator.py | 8 ++++---- treys/lookup.py | 14 +++++++------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/treys/card.py b/treys/card.py index 7956fbf..3c9335c 100644 --- a/treys/card.py +++ b/treys/card.py @@ -1,4 +1,4 @@ -from typing import Sequence +from typing import Sequence, List, Dict class Card: """ @@ -31,11 +31,11 @@ class Card: STR_RANKS: str = '23456789TJQKA' STR_SUITS: str = 'shdc' INT_RANKS: range = range(13) - PRIMES: list[int] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41] + PRIMES: List[int] = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41] # conversion from string => int - CHAR_RANK_TO_INT_RANK: dict[str, int] = dict(zip(list(STR_RANKS), INT_RANKS)) - CHAR_SUIT_TO_INT_SUIT: dict[str, int] = { + CHAR_RANK_TO_INT_RANK: Dict[str, int] = dict(zip(list(STR_RANKS), INT_RANKS)) + CHAR_SUIT_TO_INT_SUIT: Dict[str, int] = { 's': 1, # spades 'h': 2, # hearts 'd': 4, # diamonds @@ -48,14 +48,14 @@ class Card: INT_SUIT_TO_CHAR_SUIT: str = 'xshxdxxxc' # for pretty printing - PRETTY_SUITS: dict[int, str] = { + PRETTY_SUITS: Dict[int, str] = { 1: chr(9824), # spades 2: chr(9829), # hearts 4: chr(9830), # diamonds 8: chr(9827) # clubs } - SUIT_COLORS: dict[int, str] = { + SUIT_COLORS: Dict[int, str] = { 2: "red", 4: "blue", 8: "green" @@ -104,7 +104,7 @@ def get_prime(card_int: int) -> int: return card_int & 0x3F @staticmethod - def hand_to_binary(card_strs: Sequence[str]) -> list[int]: + def hand_to_binary(card_strs: Sequence[str]) -> List[int]: """ Expects a list of cards as strings and returns a list of integers of same length corresponding to those strings. diff --git a/treys/deck.py b/treys/deck.py index 7743fca..159a206 100644 --- a/treys/deck.py +++ b/treys/deck.py @@ -1,4 +1,5 @@ from random import Random +from typing import List from .card import Card @@ -9,7 +10,7 @@ class Deck: deck with the list of unique card integers. Each object instantiated simply makes a copy of this object and shuffles it. """ - _FULL_DECK: list[int] = [] + _FULL_DECK: List[int] = [] def __init__(self, seed: int = None) -> None: self._random = Random(seed) @@ -20,7 +21,7 @@ def shuffle(self) -> None: self.cards = Deck.GetFullDeck() self._random.shuffle(self.cards) - def draw(self, n: int = 1) -> list[int]: + def draw(self, n: int = 1) -> List[int]: cards = [] for _ in range(n): cards.append(self.cards.pop()) @@ -30,7 +31,7 @@ def __str__(self) -> str: return Card.ints_to_pretty_str(self.cards) @staticmethod - def GetFullDeck() -> list[int]: + def GetFullDeck() -> List[int]: if Deck._FULL_DECK: return list(Deck._FULL_DECK) diff --git a/treys/evaluator.py b/treys/evaluator.py index df8fc2f..451693d 100644 --- a/treys/evaluator.py +++ b/treys/evaluator.py @@ -1,5 +1,5 @@ import itertools -from typing import Sequence +from typing import Sequence, List from .card import Card from .lookup import LookupTable @@ -29,7 +29,7 @@ def __init__(self) -> None: 7: self._seven } - def evaluate(self, hand: list[int], board: list[int]) -> int: + def evaluate(self, hand: List[int], board: List[int]) -> int: """ This is the function that the user calls to get a hand rank. @@ -129,7 +129,7 @@ def get_five_card_rank_percentage(self, hand_rank: int) -> float: """ return float(hand_rank) / float(LookupTable.MAX_HIGH_CARD) - def hand_summary(self, board: list[int], hands: list[list[int]]) -> None: + def hand_summary(self, board: List[int], hands: List[List[int]]) -> None: """ Gives a sumamry of the hand with ranks as time proceeds. @@ -189,7 +189,7 @@ class PLOEvaluator(Evaluator): HAND_LENGTH = 4 - def evaluate(self, hand: list[int], board: list[int]) -> int: + def evaluate(self, hand: List[int], board: List[int]) -> int: minimum = LookupTable.MAX_HIGH_CARD for hand_combo in itertools.combinations(hand, 2): diff --git a/treys/lookup.py b/treys/lookup.py index dacd332..b730f44 100644 --- a/treys/lookup.py +++ b/treys/lookup.py @@ -1,6 +1,6 @@ from collections.abc import Iterator import itertools -from typing import Sequence +from typing import Sequence, Dict from .card import Card @@ -39,7 +39,7 @@ class LookupTable: MAX_PAIR: int = 6185 MAX_HIGH_CARD: int = 7462 - MAX_TO_RANK_CLASS: dict[int, int] = { + MAX_TO_RANK_CLASS: Dict[int, int] = { MAX_ROYAL_FLUSH: 0, MAX_STRAIGHT_FLUSH: 1, MAX_FOUR_OF_A_KIND: 2, @@ -52,7 +52,7 @@ class LookupTable: MAX_HIGH_CARD: 9 } - RANK_CLASS_TO_STRING: dict[int, str] = { + RANK_CLASS_TO_STRING: Dict[int, str] = { 0: "Royal Flush", 1: "Straight Flush", 2: "Four of a Kind", @@ -70,8 +70,8 @@ def __init__(self) -> None: Calculates lookup tables """ # create dictionaries - self.flush_lookup: dict[int, int] = {} - self.unsuited_lookup: dict[int, int] = {} + self.flush_lookup: Dict[int, int] = {} + self.unsuited_lookup: Dict[int, int] = {} # create the lookup table in piecewise fashion # this will call straights and high cards method, @@ -255,7 +255,7 @@ def multiples(self) -> None: self.unsuited_lookup[product] = rank rank += 1 - def write_table_to_disk(self, table: dict[int, int], filepath: str) -> None: + def write_table_to_disk(self, table: Dict[int, int], filepath: str) -> None: """ Writes lookup table to disk """ @@ -263,7 +263,7 @@ def write_table_to_disk(self, table: dict[int, int], filepath: str) -> None: for prime_prod, rank in table.items(): f.write(str(prime_prod) + "," + str(rank) + '\n') - def get_lexographically_next_bit_sequence(self, bits: int) -> Iterator[int]: + def get_lexographically_next_bit_sequence(self, bits: int) -> Sequence[int]: """ Bit hack from here: http://www-graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation