checkpoint for combining bets

This commit is contained in:
Michael Pilosov 2022-11-26 21:25:38 -07:00
parent afa12da0d8
commit ff665a5e95
2 changed files with 39 additions and 27 deletions

View File

@ -5,14 +5,13 @@ from functools import reduce
from random import choice, randint from random import choice, randint
from statistics import mean, stdev from statistics import mean, stdev
from typing import Dict, List, Optional 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 # define the class Bet which inherits from Dict[int, float] and define it's __add__ method as calling combine_bets
# Bet = Dict[int, float] # Bet = Dict[int, float]
def init_bet() -> Bet: def init_bet() -> Dict[int, float]:
""" """
Initializes a bet with all individual placements set to 0. Initializes a bet with all individual placements set to 0.
@ -25,18 +24,51 @@ def init_bet() -> Bet:
return D 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 @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_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]: def __dict__(self) -> Dict[int, float]:
"""Return the bet as a dictionary.""" """Return the bet as a dictionary."""
return self.spread return self.spread
def __add__(self, other: Bet) -> Bet: def __add__(self, other: Bet) -> Bet:
"""Combine two bets.""" """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: def __setitem__(self, __name: int, __value: float) -> None:
"""Set the value of a placement.""" """Set the value of a placement."""
@ -44,7 +76,7 @@ class Bet:
def __getitem__(self, __name: int) -> float: def __getitem__(self, __name: int) -> float:
"""Get the value of a placement.""" """Get the value of a placement."""
return self.spread[__name] return self.spread.get(__name, 0)
def copy(self) -> Bet: def copy(self) -> Bet:
"""Return a copy of the bet.""" """Return a copy of the bet."""
@ -272,27 +304,6 @@ class Placement:
""" """
return interpret_bet(self.on, self.num * self.amt, bet) 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 # 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

View File

@ -1,6 +1,7 @@
from pyroulette.roulette import Placement, Strategy, Bet from pyroulette.roulette import Placement, Strategy, Bet
if __name__ == "__main__": if __name__ == "__main__":
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()) print(Strategy(placements=[Placement(1, 10, "red"), Placement(2, 10, "black")]).get_bet())