-
Notifications
You must be signed in to change notification settings - Fork 0
/
_util_managedb.py
115 lines (84 loc) · 3.1 KB
/
_util_managedb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 8 22:51:45 2021
@author: bszym
"""
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker
import json
from _util_sqlalch_setup import UserStat, Probs
import Player
import os, sys
import pandas
if "poker" not in sys.path[0]:
sys.path.insert(0,os.getcwd())
db_path = os.path.abspath(sys.path[0])+"\\data\\pokerdb.db"
def load_lkp(name='lkp20210626'):
engine = create_engine('sqlite:///'+db_path, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
getprobs = session.query(Probs).filter_by(name=name).first()
session.close()
return json.loads(getprobs.dict)
def load_lkp2(name='lkp20210626'):
engine = create_engine('sqlite:///'+db_path, echo=False)
metadata = MetaData(engine)
metadata.reflect()
with engine.begin() as conn:
getrows=conn.execute(f"select dict from Probs where name='{name}'")
for row in getrows:
restxt=row['dict']
return json.loads(restxt)
def save_stats(player):
engine = create_engine('sqlite:///'+db_path, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
getstat = session.query(UserStat).\
filter(UserStat.name==player.name,
UserStat.type==player.type).first()
if not getstat:
newentry=UserStat(name=player.name, type=player.type,
strat=player.strat, bb100=player.bb100,
hands_played=player.hands_played, bb_won=player.bb_won)
session.add(newentry)
session.commit()
else:
getstat.bb100=player.bb100
getstat.bb_won=player.bb_won
getstat.hands_played=player.hands_played
session.commit()
session.close()
def load_stats(player):
engine = create_engine('sqlite:///'+db_path, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
getstat = session.query(UserStat).\
filter(UserStat.name==player.name,
UserStat.type==player.type).first()
if not getstat:
raise ValueError('Player with given name and type doesn\'t exist')
else:
player.bb100=getstat.bb100
player.hands_played=getstat.hands_played
player.bb_won=getstat.bb_won
player.strat=getstat.strat
player.type=getstat.type
session.commit()
session.close()
def recreate_player(name, type='comp'):
p_tmp=Player.Player({'name':name, 'type': type})
load_stats(p_tmp)
return p_tmp
def select_all(model=UserStat, limit=100):
engine = create_engine('sqlite:///data\\pokerdb.db', echo=False)
Session = sessionmaker(bind=engine)
session = Session()
for i in session.query(model)[:limit]:
print (i.__dict__)
session.close()
def save_log(df, ifexists='append'):
engine = create_engine('sqlite:///'+db_path, echo=False)
df.to_sql('game_log', con=engine, if_exists=ifexists)
def load_log():
engine = create_engine('sqlite:///'+db_path, echo=False)
return pandas.read_sql('game_log', con=engine)