Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Baseball Math Module
- # by JayMo
- OUTS_PER_INNING = 3
- class BaseballStat:
- def __init__(self, stat=0):
- self._stat = stat
- @property
- def stat(self):
- return self._stat
- @property
- def decimal(self):
- innings = int(self._stat)
- outs = round((self._stat - innings) * 100)/10
- return int(innings*OUTS_PER_INNING + outs)
- @decimal.setter
- def decimal(self, value):
- i, o = divmod(value,OUTS_PER_INNING)
- self._stat = i + o*0.1
- @property
- def innings(self):
- return int(self._stat)
- @property
- def outs(self):
- o = round((self._stat - self.innings) * 100)/10
- return int(o)
- def addInnings(self, stat):
- if type(stat) == int:
- self._stat += stat
- def addOuts(self, stat):
- if type(stat) == int:
- if stat >= OUTS_PER_INNING:
- innings, outs = divmod(stat,OUTS_PER_INNING)
- else:
- innings = 0
- outs = stat
- outs *= 0.1
- stat = innings + outs
- self._stat = self.__add__(stat)._stat
- def __add__(self, other):
- if type(other) == BaseballStat:
- stat = self._stat + other.stat
- else:
- stat = self._stat + other
- innings = int(stat)
- outs = (stat - innings) * 10
- i, o = divmod(outs,OUTS_PER_INNING)
- innings += i
- stat = innings + int(o)/10
- return BaseballStat(stat)
- def __iadd__(self, other):
- self._stat = self.__add__(other)._stat
- return self
- def __eq__(self, other):
- if type(other) == BaseballStat:
- return self._stat == other._stat
- def __lt__(self, other):
- if type(other) == BaseballStat:
- return self._stat < other._stat
- def __gt__(self, other):
- if type(other) == BaseballStat:
- return self._stat > other._stat
- def __le__(self, other):
- if type(other) == BaseballStat:
- return self._stat <= other._stat
- def __ge__(self, other):
- if type(other) == BaseballStat:
- return self._stat >= other._stat
- def __repr__(self):
- return f"{self.__class__.__name__}: {self.stat}"
- if __name__ == "__main__":
- s1 = BaseballStat(3.2)
- s2 = BaseballStat(1.2)
- print(f"{s1=}")
- print(f"{s2=}")
- s3 = s1 + s2
- print(f"{s3=}")
- print(f"{s1.innings=}")
- print(f"{s1.outs=}")
- s1.addOuts(4)
- s1.addInnings(5)
- s2 = BaseballStat()
- s2.decimal = 26
- s1 += s2
- print(s1)
- print(s2)
- print(f"{s2.decimal=}")
- print("Finished...")
Advertisement
Add Comment
Please, Sign In to add comment