kculculate / utils.py
Kpenciler's picture
Upload 5 files
1058ca8
raw
history blame
2.98 kB
import math
from typing import TypedDict
import pandas as pd
# Typing -----------------------------------------------------------------------
class OddsInfo(TypedDict):
index: str
name: str
odds: float
OdddsList = list[OddsInfo]
class Info(TypedDict):
sum: int
num_kind: int
refound_mean: int
profit_mean: int
rate_min: float
rate_max: float
rate_mean: float
# text -> odds_list ------------------------------------------------------------
def _text2oddslist_pc(text: str) -> OdddsList:
odds_list: OdddsList = []
tmp_list: list[str] = []
lines: list[str] = text.split("\n")
for text in lines:
if "." in text:
odds_list.append(
{
"index": "-".join(tmp_list),
"name": "",
"odds": float(text),
}
)
tmp_list = []
else:
if text.strip():
tmp_list.append(text.strip())
return odds_list
def _text2oddslist_mobile(text: str) -> OdddsList:
odds_list: OdddsList = []
lines: list[str] = text.split("\n")
if "選択" in lines:
lines = lines[lines.index("選択") + 1 :]
if "すべてを選択" in lines:
lines = lines[: lines.index("すべてを選択")]
for i in range(len(lines) // 3):
# oddsが1つ目にある
if "." in lines[3 * i + 1]:
idx_odds = 1
idx_name = 2
else:
idx_odds = 2
idx_name = 1
odds_list.append(
{
"index": "-".join(lines[3 * i].split()),
"name": "-".join(lines[3 * i + idx_name].split()),
"odds": float(lines[3 * i + idx_odds]),
}
)
return odds_list
def text2oddslist(text: str) -> OdddsList:
# pcの時
if set("".join(text.split())) <= set("0123456789.-"):
odds_list = _text2oddslist_pc(text)
# moduleの時
else:
odds_list = _text2oddslist_mobile(text)
return odds_list
# odds_list -> df ---------------------------------------------------------------
def get_calculated_df(amount: int, odds_list: OdddsList) -> pd.DataFrame:
df = pd.DataFrame(odds_list)
df["weight"] = (1 / df["odds"]) / (1 / df["odds"]).sum()
df["buy"] = (df["weight"] * amount / 100).map(
lambda x: max(math.floor(x) * 100, 100)
)
df["refound"] = (df["buy"] * df["odds"]).astype(int)
df["profit"] = df["refound"] - amount
df = df
return df
def get_info(df: pd.DataFrame) -> Info:
info: Info = {
"sum": df["buy"].sum(),
"num_kind": df.shape[0],
"refound_mean": int(df["refound"].mean()),
"profit_mean": int(df["profit"].mean()),
"rate_min": df["refound"].min() / df["buy"].sum(),
"rate_max": df["refound"].max() / df["buy"].sum(),
"rate_mean": df["refound"].mean() / df["buy"].sum(),
}
return info