From ff665a5e95a72fab1fbea8ac7b5fe68d0a8db0b1 Mon Sep 17 00:00:00 2001 From: Michael Pilosov Date: Sat, 26 Nov 2022 21:25:38 -0700 Subject: [PATCH] checkpoint for combining bets --- pyroulette/roulette.py | 63 +++++++++++++++++++++++++----------------- strategy.py | 3 +- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/pyroulette/roulette.py b/pyroulette/roulette.py index a7fa3f3..8c3fee1 100644 --- a/pyroulette/roulette.py +++ b/pyroulette/roulette.py @@ -5,14 +5,13 @@ from functools import reduce from random import choice, randint from statistics import mean, stdev from typing import Dict, List, Optional - - +import json # define the class Bet which inherits from Dict[int, float] and define it's __add__ method as calling combine_bets # Bet = Dict[int, float] -def init_bet() -> Bet: +def init_bet() -> Dict[int, float]: """ Initializes a bet with all individual placements set to 0. @@ -25,18 +24,51 @@ def init_bet() -> Bet: return D + +# for two bets of structure Dict[int, float], iterate through all the keys and add up the values, returning a new dict. +def combine_bets(bet_1: Bet, bet_2: Bet) -> Bet: + """ + Combines two bets into a single bet. + + Parameters + ---------- + bet_1 : Bet + The first bet to combine. + bet_2 : Bet + The second bet to combine. + + Returns + ------- + Bet + The combined bet. + """ + # 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)} + + + @dataclass class Bet: """A class for representing a bet.""" spread: Dict[int, float] = field(default_factory=init_bet) + def __repr__(self) -> str: + """Return a string representation of the bet.""" + nonzeros = dict(filter(lambda x: x[1] > 0, self.spread.items())) + return f"Bet({nonzeros})" + + # define the appearance when using print + def __str__(self) -> str: + nonzeros = dict(filter(lambda x: x[1] > 0, self.spread.items())) + return json.dumps(nonzeros, indent=4) + def __dict__(self) -> Dict[int, float]: """Return the bet as a dictionary.""" return self.spread def __add__(self, other: Bet) -> Bet: """Combine two bets.""" - return combine_bets(self.spread, other.spread) + return Bet(combine_bets(self.spread, other.spread)) def __setitem__(self, __name: int, __value: float) -> None: """Set the value of a placement.""" @@ -44,7 +76,7 @@ class Bet: def __getitem__(self, __name: int) -> float: """Get the value of a placement.""" - return self.spread[__name] + return self.spread.get(__name, 0) def copy(self) -> Bet: """Return a copy of the bet.""" @@ -272,27 +304,6 @@ class Placement: """ return interpret_bet(self.on, self.num * self.amt, bet) - -# for two bets of structure Dict[int, float], iterate through all the keys and add up the values, returning a new dict. -def combine_bets(bet_1: Bet, bet_2: Bet) -> Bet: - """ - Combines two bets into a single bet. - - Parameters - ---------- - bet_1 : Bet - The first bet to combine. - bet_2 : Bet - The second bet to combine. - - Returns - ------- - Bet - The combined bet. - """ - return {k: bet_1.get(k, 0) + bet_2.get(k, 0) for k in set(bet_1) | set(bet_2)} - - # for a list of Placements, call the place_bet method on each one and combine the results using reduce and combine_bets, starting with an empty dictionary as the initial argument diff --git a/strategy.py b/strategy.py index 14abc8b..c21236d 100644 --- a/strategy.py +++ b/strategy.py @@ -1,6 +1,7 @@ from pyroulette.roulette import Placement, Strategy, Bet if __name__ == "__main__": - + print(Bet({0: 1}) + Bet({0: 1})) + print(Bet({0: 1}).__repr__()) print(Bet({0: 1}) + Bet()) print(Strategy(placements=[Placement(1, 10, "red"), Placement(2, 10, "black")]).get_bet()) \ No newline at end of file