finalize adding bets, printing them out in a pretty way

This commit is contained in:
Michael Pilosov 2022-11-26 21:45:09 -07:00
parent ff665a5e95
commit 91ba00e6ea
3 changed files with 30 additions and 13 deletions

2
app.py
View File

@ -8,7 +8,7 @@ from pyroulette import (
Strategy, Strategy,
expected, expected,
generate_players, generate_players,
init_bet, init_spread,
interpret_bet, interpret_bet,
place_bet, place_bet,
simulate_games, simulate_games,

View File

@ -11,7 +11,7 @@ import json
# Bet = Dict[int, float] # Bet = Dict[int, float]
def init_bet() -> Dict[int, float]: def init_spread() -> Dict[int, float]:
""" """
Initializes a bet with all individual placements set to 0. Initializes a bet with all individual placements set to 0.
@ -43,24 +43,30 @@ def combine_bets(bet_1: Bet, bet_2: Bet) -> Bet:
The combined bet. The combined bet.
""" """
# TODO: can remove the default 0. # TODO: can remove the default 0.
return {k: bet_1.get(k, 0) + bet_2.get(k, 0) for k in set(bet_1) | set(bet_2)} return Bet({k: bet_1[k] + bet_2[k] for k in set(bet_1) | set(bet_2)})
@dataclass @dataclass
class Bet: class Bet:
"""A class for representing a bet.""" """A class for representing a bet."""
spread: Dict[int, float] = field(default_factory=init_bet) spread: Dict[int, float] = field(default_factory=init_spread)
@property
def __nonzeros__(self) -> Dict[int, float]:
nonzeros = dict(filter(lambda x: x[1] > 0, self.spread.items()))
nonzeros = {k: round(v, 2) for k, v in nonzeros.items()}
if -1 in nonzeros:
nonzeros["00"] = nonzeros.pop(-1)
return nonzeros
def __repr__(self) -> str: def __repr__(self) -> str:
"""Return a string representation of the bet.""" """Return a string representation of the bet."""
nonzeros = dict(filter(lambda x: x[1] > 0, self.spread.items())) return f"Bet({self.__nonzeros__})"
return f"Bet({nonzeros})"
# define the appearance when using print # define the appearance when using print
def __str__(self) -> str: def __str__(self) -> str:
nonzeros = dict(filter(lambda x: x[1] > 0, self.spread.items())) return json.dumps(self.__nonzeros__, indent=4)
return json.dumps(nonzeros, indent=4)
def __dict__(self) -> Dict[int, float]: def __dict__(self) -> Dict[int, float]:
"""Return the bet as a dictionary.""" """Return the bet as a dictionary."""
@ -68,7 +74,7 @@ class Bet:
def __add__(self, other: Bet) -> Bet: def __add__(self, other: Bet) -> Bet:
"""Combine two bets.""" """Combine two bets."""
return Bet(combine_bets(self.spread, other.spread)) return combine_bets(self, other)
def __setitem__(self, __name: int, __value: float) -> None: def __setitem__(self, __name: int, __value: float) -> None:
"""Set the value of a placement.""" """Set the value of a placement."""
@ -82,6 +88,12 @@ class Bet:
"""Return a copy of the bet.""" """Return a copy of the bet."""
return Bet(self.spread.copy()) return Bet(self.spread.copy())
def __iter__(self):
return iter(self.spread.keys())
def get(self, __name: int) -> float:
return self.spread.get(__name, 0)
SINGLE_BETS = {str(i) for i in range(-1, 37)} SINGLE_BETS = {str(i) for i in range(-1, 37)}
@ -191,7 +203,7 @@ def interpret_bet(on="red", amount=0, bet=Optional[Bet]) -> Bet:
on in ALIASES on in ALIASES
), f"Bet `{on}` not understood. Choose from feasible moves:\n {FEASIBLE_MOVES}" ), f"Bet `{on}` not understood. Choose from feasible moves:\n {FEASIBLE_MOVES}"
if bet is None: if bet is None:
bet = init_bet() bet = Bet()
else: else:
bet = bet.copy() bet = bet.copy()
REDS = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36} REDS = {1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36}
@ -403,7 +415,7 @@ class Strategy:
return reduce( return reduce(
lambda bet, placement: combine_bets(bet, placement.bet()), lambda bet, placement: combine_bets(bet, placement.bet()),
placements, placements,
{}, Bet(),
) )

View File

@ -1,7 +1,12 @@
from pyroulette.roulette import Placement, Strategy, Bet from pyroulette.roulette import Placement, Strategy, Bet
if __name__ == "__main__": if __name__ == "__main__":
print(Bet({-1: 1}).__repr__()) # this should print with 00
print('get test')
print(Bet({0: 1})[21])
print(Bet({21: 1})[21])
print('sum test')
print(Bet({0: 1}) + Bet({0: 1})) print(Bet({0: 1}) + Bet({0: 1}))
print(Bet({0: 1}).__repr__())
print(Bet({0: 1}) + Bet()) print(Bet({0: 1}) + Bet())
print(Strategy(placements=[Placement(1, 10, "red"), Placement(2, 10, "black")]).get_bet()) # this should return 1's in everything but 0 and -1.
print(Strategy(placements=[Placement(18, 1, "red"), Placement(18, 1, "black")]).get_bet())