class iPrize(Base, DBBase): __tablename__ = 'prizes' db = db_init() prz_id = Column(VARCHAR(20), primary_key=True, autoincrement=False) lottery = Column(VARCHAR(20)) country = Column(VARCHAR(20)) state = Column(VARCHAR(20)) picks = Column(Integer) matches = Column(Integer) cost = Column(DECIMAL(5, 2)) prize = Column(DECIMAL(11, 2)) cost_x = Column(DECIMAL(5, 2)) prize_x = Column(DECIMAL(11, 2)) odds = Column(DECIMAL(5, 2)) odds_x = Column(DECIMAL(5, 2)) tickDict = {0: 0} def __init__(self, picks=0, matches=0): #CONSTANTS# self.lottery = "Hotspot" self.country = "USA" self.state = "CA" #load if (picks > 0): self.setup(picks, matches) def add_table(self): self.create_table() def derive_prize_id(self, picks, matches): self.picks = picks self.matches = matches self.prz_id = self.lottery + "_" + str(self.picks) + "_" + str( self.matches) print("PRIZE ID: ", self.prz_id) def setup(self, picks, matches): self.derive_prize_id(picks, matches) prz = self.db.session.query(iPrize).filter( iPrize.prz_id == self.prz_id).first() self.lottery = prz.lottery self.country = prz.country self.state = prz.state self.picks = prz.picks self.matches = prz.matches self.cost = prz.cost self.prize = prz.prize self.cost_x = prz.cost_x self.prize_x = prz.prize_x self.odds = prz.odds self.odds_x = prz.odds_x def to_string(self): return self.prz_id
class iStrategy(Base, DBBase): __tablename__ = 'strategies' db = db_init() strat_id = Column(VARCHAR(11), primary_key=True, autoincrement=False) strategy = Column(VARCHAR(4)) major_version = Column(SmallInteger) minor_version = Column(SmallInteger) strat_desc = Column(Text(1000)) tickets_total = Column(SmallInteger) tickets_claimed = Column(Integer) cost_total = Column(DECIMAL(11, 2)) prize_total = Column(DECIMAL(11, 2)) pnl = Column(DECIMAL(11, 2)) match_max = Column(Integer) prize_max = Column(DECIMAL(11, 2)) match_min = Column(Integer) prize_min = Column(DECIMAL(11, 2)) match_average = Column(Integer) prize_average = Column(DECIMAL(11, 2)) def __init__(self): super().setupDBBase(iStrategy, iStrategy.strat_id, self.strat_id) def add_table(self): self.create_table() def register(self, STRAT_ID, STRAT, MAJOR, MINOR, DESC): self.strat_id = STRAT_ID self.strategy = STRAT self.major_version = MAJOR self.minor_version = MINOR self.strat_desc = DESC self.db_save() def set_weights(self): print("Setting weights") pass def get_combs(self): pass def run_session(self): pass def validate_tickets(self): pass
class Prizes(iprize.iPrize): db = db_init() r_prizes = {} def __init__(self): self.lotto = "Hotspot" self.r_prizes = {} def setup_prize_list(self): results = self.db.session.query( iprize.iPrize).filter(iprize.iPrize.lottery == self.lotto).all() for result in results: self.r_prizes[result.prz_id] = result def get_prize(self, picks, matches): przID = "Hotspot" + "_" + str(picks) + "_" + str(matches) return self.r_prizes[przID]
class iDepth(Base, DBBase): __tablename__ = 'depths' db = db_init() draw_id = Column(Integer, primary_key=True, autoincrement=False) n1 = Column(Integer) n2 = Column(Integer) n3 = Column(Integer) n4 = Column(Integer) n5 = Column(Integer) n6 = Column(Integer) n7 = Column(Integer) n8 = Column(Integer) n9 = Column(Integer) n10 = Column(Integer) n11 = Column(Integer) n12 = Column(Integer) n13 = Column(Integer) n14 = Column(Integer) n15 = Column(Integer) n16 = Column(Integer) n17 = Column(Integer) n18 = Column(Integer) n19 = Column(Integer) n20 = Column(Integer) n21 = Column(Integer) n22 = Column(Integer) n23 = Column(Integer) n24 = Column(Integer) n25 = Column(Integer) n26 = Column(Integer) n27 = Column(Integer) n28 = Column(Integer) n29 = Column(Integer) n30 = Column(Integer) n31 = Column(Integer) n32 = Column(Integer) n33 = Column(Integer) n34 = Column(Integer) n35 = Column(Integer) n36 = Column(Integer) n37 = Column(Integer) n38 = Column(Integer) n39 = Column(Integer) n40 = Column(Integer) n41 = Column(Integer) n42 = Column(Integer) n43 = Column(Integer) n44 = Column(Integer) n45 = Column(Integer) n46 = Column(Integer) n47 = Column(Integer) n48 = Column(Integer) n49 = Column(Integer) n50 = Column(Integer) n51 = Column(Integer) n52 = Column(Integer) n53 = Column(Integer) n54 = Column(Integer) n55 = Column(Integer) n56 = Column(Integer) n57 = Column(Integer) n58 = Column(Integer) n59 = Column(Integer) n60 = Column(Integer) n61 = Column(Integer) n62 = Column(Integer) n63 = Column(Integer) n64 = Column(Integer) n65 = Column(Integer) n66 = Column(Integer) n67 = Column(Integer) n68 = Column(Integer) n69 = Column(Integer) n70 = Column(Integer) n71 = Column(Integer) n72 = Column(Integer) n73 = Column(Integer) n74 = Column(Integer) n75 = Column(Integer) n76 = Column(Integer) n77 = Column(Integer) n78 = Column(Integer) n79 = Column(Integer) n80 = Column(Integer) mega = Column(Integer) depthDict = {0: 0} def __init__(self, DrawID=0): self.draw_id = DrawID self.reset() super().setupDBBase(iDepth, iDepth.draw_id, self.draw_id) def reset(self): for i in range(81): self.depthDict[i] = 0 if i > 0: setattr(self, "n" + str(i), 0) self.mega = 0 def derive(self): if (self.draw_id > 0): cur_id = self.draw_id prev_id = self.draw_id - 1 prev_depth = self.db.session.query(iDepth).filter( iDepth.draw_id == prev_id).first() cur_draw = idraw.iDraw(cur_id) for i in self.depthDict.keys(): if i > 0: if getattr(cur_draw, "n" + str(i)) == 1: setattr(self, "n" + str(i), 0) else: setattr(self, "n" + str(i), getattr(prev_depth, "n" + str(i)) + 1) def setup(self): d = self.db.session.query(iDepth).filter( iDepth.draw_id == self.draw_id).first() if d is not None: self.mega = d.mega for i in self.depthDict.keys(): if i > 0: setattr(self, "n" + str(i), getattr(d, "n" + str(i))) def get_dict(self): dict = {} dict[self.draw_id] = np.array([ self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7, self.n8, self.n9, self.n10, self.n11, self.n12, self.n13, self.n14, self.n15, self.n16, self.n17, self.n18, self.n19, self.n20, self.n21, self.n22, self.n23, self.n24, self.n25, self.n26, self.n27, self.n28, self.n29, self.n30, self.n31, self.n32, self.n33, self.n34, self.n35, self.n36, self.n37, self.n38, self.n39, self.n40, self.n41, self.n42, self.n43, self.n44, self.n45, self.n46, self.n47, self.n48, self.n49, self.n50, self.n51, self.n52, self.n53, self.n54, self.n55, self.n56, self.n57, self.n58, self.n59, self.n60, self.n61, self.n62, self.n63, self.n64, self.n65, self.n66, self.n67, self.n68, self.n69, self.n70, self.n71, self.n72, self.n73, self.n74, self.n75, self.n76, self.n77, self.n78, self.n79, self.n80 ]) return dict def toString(self): return str(self.get_dict()) + "[" + str(self.mega) + "]" def __get_dict__(self): dic = {} dic[self.draw_id] = {} i = 1 while (i <= 80): dic[self.draw_id][i] = getattr(self, "n" + str(i)) i += 1 return dic
class iResult(Base, DBBase): __tablename__ = 'results' db = db_init() draw_id = Column(Integer, primary_key=True) date_time = Column(DateTime) r1 = Column(Integer) r2 = Column(Integer) r3 = Column(Integer) r4 = Column(Integer) r5 = Column(Integer) r6 = Column(Integer) r7 = Column(Integer) r8 = Column(Integer) r9 = Column(Integer) r10 = Column(Integer) r11 = Column(Integer) r12 = Column(Integer) r13 = Column(Integer) r14 = Column(Integer) r15 = Column(Integer) r16 = Column(Integer) r17 = Column(Integer) r18 = Column(Integer) r19 = Column(Integer) r20 = Column(Integer) mega = Column(Integer) pick = '' def __init__(self, DrawID=0, DrawDateTime='', PickArray=[0 for x in range(20)], Mega=0): self.draw_id = DrawID self.date_time = DrawDateTime self.setPick(ipick.iPick(PickArray)) #self.numFromArray(PickArray) self.mega = Mega super().setupDBBase(iResult, iResult.draw_id, self.draw_id) def setup(self): r = self.db.session.query(iResult).filter( iResult.draw_id == self.draw_id).first() if (r is not None): self.date_time = r.date_time self.r1 = r.r1 self.r2 = r.r2 self.r3 = r.r3 self.r4 = r.r4 self.r5 = r.r5 self.r6 = r.r6 self.r7 = r.r7 self.r8 = r.r8 self.r9 = r.r9 self.r10 = r.r10 self.r11 = r.r11 self.r12 = r.r12 self.r13 = r.r13 self.r14 = r.r14 self.r15 = r.r15 self.r16 = r.r16 self.r17 = r.r17 self.r18 = r.r18 self.r19 = r.r19 self.r20 = r.r20 self.mega = r.mega self.pick = ipick.iPick(self.numToArray()) def getPick(self): return self.pick def setPick(self, pick): self.pick = pick self.numFromArray(self.pick.getArray()) def numFromArray(self, array20=[0 for x in range(20)]): self.r1 = array20[0] self.r2 = array20[1] self.r3 = array20[2] self.r4 = array20[3] self.r5 = array20[4] self.r6 = array20[5] self.r7 = array20[6] self.r8 = array20[7] self.r9 = array20[8] self.r10 = array20[9] self.r11 = array20[10] self.r12 = array20[11] self.r13 = array20[12] self.r14 = array20[13] self.r15 = array20[14] self.r16 = array20[15] self.r17 = array20[16] self.r18 = array20[17] self.r19 = array20[18] self.r20 = array20[19] #self.setPick(ipick.iPick(array20)) def numToArray(self): numArray = [ self.r1, self.r2, self.r3, self.r4, self.r5, self.r6, self.r7, self.r8, self.r9, self.r10, self.r11, self.r12, self.r13, self.r14, self.r15, self.r16, self.r17, self.r18, self.r19, self.r20 ] return numArray def get_dict(self): dict = {} dict[self.draw_id] = np.array([ self.r1, self.r2, self.r3, self.r4, self.r5, self.r6, self.r7, self.r8, self.r9, self.r10, self.r11, self.r12, self.r13, self.r14, self.r15, self.r16, self.r17, self.r18, self.r19, self.r20 ]) return dict def toString(self): return str(self.draw_id) + "[" + str( self.date_time) + "]" + self.pick.toString() + "[" + str( self.mega) + "]" def getLastDrawID(self): #return self.db.session.query(func.max(iResult.draw_id)).scalar(); return self.db.session.query(iResult).order_by( iResult.draw_id.desc()).first().draw_id
class iDraw(Base, DBBase): __tablename__ = 'draws' db = db_init() draw_id = Column(Integer, primary_key=True, autoincrement=False) n1 = Column(Integer) n2 = Column(Integer) n3 = Column(Integer) n4 = Column(Integer) n5 = Column(Integer) n6 = Column(Integer) n7 = Column(Integer) n8 = Column(Integer) n9 = Column(Integer) n10 = Column(Integer) n11 = Column(Integer) n12 = Column(Integer) n13 = Column(Integer) n14 = Column(Integer) n15 = Column(Integer) n16 = Column(Integer) n17 = Column(Integer) n18 = Column(Integer) n19 = Column(Integer) n20 = Column(Integer) n21 = Column(Integer) n22 = Column(Integer) n23 = Column(Integer) n24 = Column(Integer) n25 = Column(Integer) n26 = Column(Integer) n27 = Column(Integer) n28 = Column(Integer) n29 = Column(Integer) n30 = Column(Integer) n31 = Column(Integer) n32 = Column(Integer) n33 = Column(Integer) n34 = Column(Integer) n35 = Column(Integer) n36 = Column(Integer) n37 = Column(Integer) n38 = Column(Integer) n39 = Column(Integer) n40 = Column(Integer) n41 = Column(Integer) n42 = Column(Integer) n43 = Column(Integer) n44 = Column(Integer) n45 = Column(Integer) n46 = Column(Integer) n47 = Column(Integer) n48 = Column(Integer) n49 = Column(Integer) n50 = Column(Integer) n51 = Column(Integer) n52 = Column(Integer) n53 = Column(Integer) n54 = Column(Integer) n55 = Column(Integer) n56 = Column(Integer) n57 = Column(Integer) n58 = Column(Integer) n59 = Column(Integer) n60 = Column(Integer) n61 = Column(Integer) n62 = Column(Integer) n63 = Column(Integer) n64 = Column(Integer) n65 = Column(Integer) n66 = Column(Integer) n67 = Column(Integer) n68 = Column(Integer) n69 = Column(Integer) n70 = Column(Integer) n71 = Column(Integer) n72 = Column(Integer) n73 = Column(Integer) n74 = Column(Integer) n75 = Column(Integer) n76 = Column(Integer) n77 = Column(Integer) n78 = Column(Integer) n79 = Column(Integer) n80 = Column(Integer) mega = Column(Integer) d_bin0140 = Column(BigInteger) d_bin4180 = Column(BigInteger) drawDict = {0: 0} def __init__(self, DrawID=0): self.reset() self.draw_id = DrawID if (self.draw_id > 0): self.setup() super().setupDBBase(iDraw, iDraw.draw_id, self.draw_id) def reset(self): for i in range(81): self.drawDict[i] = 0 if i > 0: setattr(self, "n" + str(i), 0) self.mega = 0 self.d_bin0140 = 0 self.d_bin4180 = 0 def derive(self): if (self.draw_id > 0): self.reset() #remove bin setting as it is resource intensive. We will use SQL #b0140 = ibin.iBin(); #b4180 = ibin.iBin(); r = iresult.iResult(self.draw_id) r.setup() #setup from db self.mega = r.mega for x in r.pick.getArray(): setattr(self, "n" + str(x), 1) self.drawDict[x] = 1 #if x > 40: # posn = x - 40; # b4180.set_bit(posn); # else: b0140.set_bit(x) # self.d_bin0140 = b0140.get_bin(); #770947678208 # self.d_bin4180 = b4180.get_bin(); #8606711840 def setBin(self): i = 1 b0140 = ibin.iBin() b4180 = ibin.iBin() while (i <= 80): dr = getattr(self, "n" + str(i)) if dr == 1: if i > 40: b4180.set_bit(i) else: b0140.set_bit(i) i += 1 self.d_bin0140 = b0140.get_bin() #833831981626 for 2277311 self.d_bin4180 = b4180.get_bin() #206435647488 for 2277311 #update db d = self.db.session.query(iDraw).filter( iDraw.draw_id == self.draw_id).update( { iDraw.d_bin0140: self.d_bin0140, iDraw.d_bin4180: self.d_bin4180 }, synchronize_session='fetch') self.db.session.commit() def setBinBySQL(self): #single draw_id # sql_statement = text(""" UPDATE `draws` SET `d_bin0140`=CONV(CONCAT(`n1`, `n2`, `n3`, `n4`, `n5`, `n6`, `n7`, `n8`, `n9`, `n10`, `n11`, `n12`, `n13`, `n14`, `n15`, `n16`, `n17`, `n18`, `n19`, `n20`, `n21`, `n22`, `n23`, `n24`, `n25`, `n26`, `n27`, `n28`, `n29`, `n30`, `n31`, `n32`, `n33`, `n34`, `n35`, `n36`, `n37`, `n38`, `n39`, `n40`),2,10), `d_bin4180`=CONV(CONCAT(`n41`, `n42`, `n43`, `n44`, `n45`, `n46`, `n47`, `n48`, `n49`, `n50`, `n51`, `n52`, `n53`, `n54`, `n55`, `n56`, `n57`, `n58`, `n59`, `n60`, `n61`, `n62`, `n63`, `n64`, `n65`, `n66`, `n67`, `n68`, `n69`, `n70`, `n71`, `n72`, `n73`, `n74`, `n75`, `n76`, `n77`, `n78`, `n79`, `n80`),2,10) WHERE `draw_id` = :draw_id """); # self.db.session.execute(sql_statement, { "draw_id": self.draw_id } ) #multiple draw_ids with dbin value null or 0 sql_statement = text( """ UPDATE `draws` SET `d_bin0140`=CONV(CONCAT(`n1`, `n2`, `n3`, `n4`, `n5`, `n6`, `n7`, `n8`, `n9`, `n10`, `n11`, `n12`, `n13`, `n14`, `n15`, `n16`, `n17`, `n18`, `n19`, `n20`, `n21`, `n22`, `n23`, `n24`, `n25`, `n26`, `n27`, `n28`, `n29`, `n30`, `n31`, `n32`, `n33`, `n34`, `n35`, `n36`, `n37`, `n38`, `n39`, `n40`),2,10), `d_bin4180`=CONV(CONCAT(`n41`, `n42`, `n43`, `n44`, `n45`, `n46`, `n47`, `n48`, `n49`, `n50`, `n51`, `n52`, `n53`, `n54`, `n55`, `n56`, `n57`, `n58`, `n59`, `n60`, `n61`, `n62`, `n63`, `n64`, `n65`, `n66`, `n67`, `n68`, `n69`, `n70`, `n71`, `n72`, `n73`, `n74`, `n75`, `n76`, `n77`, `n78`, `n79`, `n80`),2,10) WHERE `d_bin0140` IS NULL or `d_bin0140` = 0 or `d_bin4180` IS NULL or `d_bin4180` = 0 """ ) self.db.session.execute(sql_statement) def setup(self): d = self.db.session.query(iDraw).filter( iDraw.draw_id == self.draw_id).first() if d is not None: for i in self.drawDict.keys(): if (i > 0): setattr(self, "n" + str(i), getattr(d, "n" + str(i))) self.drawDict[i] = getattr(self, "n" + str(i)) self.mega = d.mega self.d_bin0140 = d.d_bin0140 self.d_bin4180 = d.d_bin4180 def toString(self): return str(self.get_dict()) + "[" + str(self.mega) + "]" def get_dict(self): dict = {} dict[self.draw_id] = np.array([ self.n1, self.n2, self.n3, self.n4, self.n5, self.n6, self.n7, self.n8, self.n9, self.n10, self.n11, self.n12, self.n13, self.n14, self.n15, self.n16, self.n17, self.n18, self.n19, self.n20, self.n21, self.n22, self.n23, self.n24, self.n25, self.n26, self.n27, self.n28, self.n29, self.n30, self.n31, self.n32, self.n33, self.n34, self.n35, self.n36, self.n37, self.n38, self.n39, self.n40, self.n41, self.n42, self.n43, self.n44, self.n45, self.n46, self.n47, self.n48, self.n49, self.n50, self.n51, self.n52, self.n53, self.n54, self.n55, self.n56, self.n57, self.n58, self.n59, self.n60, self.n61, self.n62, self.n63, self.n64, self.n65, self.n66, self.n67, self.n68, self.n69, self.n70, self.n71, self.n72, self.n73, self.n74, self.n75, self.n76, self.n77, self.n78, self.n79, self.n80, self.d_bin0140, self.d_bin4180 ]) return dict def __get_dict__(self): dic = {} dic[self.draw_id] = {} i = 1 while (i <= 80): dic[self.draw_id][i] = getattr(self, "n" + str(i)) i += 1 dic[self.draw_id]['d_bin0140'] = self.d_bin0140 dic[self.draw_id]['d_bin4180'] = self.d_bin4180 return dic
class iExplore(Base, DBBase): __tablename__ = 'explores' db = db_init() xplr_id = Column(Integer, primary_key=True, autoincrement=True) idx_id = Column(Integer, ForeignKey("indexes.idx_id")) from_draw = Column(Integer) to_draw = Column(Integer) qualified_draws = Column(Integer) qualifiers = Column(Integer) win = Column(Integer) efficiency = Column(DECIMAL(5, 4)) best = Column(Integer) worst = Column(Integer) avg = Column(DECIMAL(6, 4)) logs = relationship("iexplorelog.ExploreLog", foreign_keys=xplr_id, backref='my_explore', primaryjoin="ExploreLog.xplr_id==iExplore.xplr_id") #my_index = relationship("index.Index", foreign_keys=idx_id, backref='explores') _IS_CURRENT = False _INDEX = None #Index(); _DRAWS = {} _DEPTHS = {} _LOGS = [] _Q_LIST = {} LABEL = "NOT SET" DESC = "NOT SET" def __init__(self, Label=None, DrawID=0, Cnt=0, isCurrent=False): if (Label != None): self.reset() self._INDEX = index.Index(Label) self.idx_id = self._INDEX.idx_id self._IS_CURRENT = isCurrent if (isCurrent): self.from_draw = DrawID - 1 else: self.from_draw = DrawID self.to_draw = DrawID - Cnt self.load_data() print("iExplore initiated for ", Label, DrawID, Cnt) super().setupDBBase(iExplore, iExplore.xplr_id, self.xplr_id) def register_index(self): print("iExplore.register_index(): Want to register %s %s" % (self.LABEL, self.DESC)) self._INDEX = index.Index(self.LABEL, self.DESC) self._INDEX.register() def load_data(self): FROM_DRAW_ID = self.to_draw - 1 TO_DRAW_ID = self.from_draw + 1 self.load_draws(FROM_DRAW_ID, TO_DRAW_ID) self.load_depths(FROM_DRAW_ID, TO_DRAW_ID) print("Loaded draws/depths from " + str(FROM_DRAW_ID) + "-" + str(TO_DRAW_ID)) def load_draws(self, FROM_DRAW_ID, TO_DRAW_ID): res_draws = self.db.session.query( idraw.iDraw).filter(idraw.iDraw.draw_id >= FROM_DRAW_ID).filter( idraw.iDraw.draw_id <= TO_DRAW_ID).all() for dr in res_draws: self._DRAWS.update(dr.__get_dict__()) #print(self._DRAWS) def load_depths(self, FROM_DRAW_ID, TO_DRAW_ID): res_depths = self.db.session.query(idepth.iDepth).filter( idepth.iDepth.draw_id >= FROM_DRAW_ID).filter( idepth.iDepth.draw_id <= TO_DRAW_ID).all() for dp in res_depths: self._DEPTHS.update(dp.__get_dict__()) def execute_algo(self, X, Y): print("Index algo not defined") def validate(self): for q in self._Q_LIST: if (self._DRAWS[q.draw_id + 1][q.ball] == 1): q.is_win(1) def add_qualifier(self, DrawID, Num, Win=0): q = Qualifier(DrawID, Num, Win) self._Q_LIST.amend(q) self.qualifiers += 1 def validate_qualifier(self, DrawID, Num): Win = 0 if (self._DRAWS[DrawID + 1][Num] == 1): Win = 1 return Win def explore(self, DBSave=True, batch=10): cur_draw = self.to_draw last_draw = self.from_draw #loads data in referse order print("Exploring from " + self.to_draw.__str__() + " to " + self.from_draw.__str__()) while (cur_draw <= last_draw): X = cur_draw self._Q_LIST[X] = {} Y = 1 while (Y <= 80): if (self.execute_algo(X, Y)): self._Q_LIST[X][Y] = self.validate_qualifier(X, Y) Y += 1 cur_draw += 1 self.finalize_logs() self.finalize_metrics() if (DBSave): self.save_logs() self.print_summary() def finalize_logs(self): self.qualified_draws = len(self._Q_LIST) self.qualifiers = 0 self.win = 0 self.best = 0 self.worst = 0 self.avg = 0 for dr, q in self._Q_LIST.items(): #loop per draw q_per_draw = len(q) win_per_draw = 0 eff_per_draw = 0.0 xplrlg = iexplorelog.ExploreLog(self.xplr_id, self.idx_id, dr, q, self._DRAWS[dr]['d_bin0140'], self._DRAWS[dr]['d_bin4180']) self._LOGS.append(xplrlg) for x, y in q.items(): #loop per qualifier if (y == 1): win_per_draw += 1 #now finalize per draw self.qualifiers += q_per_draw self.win += win_per_draw if (q_per_draw > 0): eff_per_draw = win_per_draw / q_per_draw if (eff_per_draw > self.best): self.best = eff_per_draw if (eff_per_draw < self.worst): self.worst = eff_per_draw def finalize_metrics(self): self.efficiency = self.win / self.qualifiers def save_logs(self): self.db_save() self.db.session.bulk_save_objects(self._LOGS) self.db.session.commit() def print_qualifiers(self, result=False): if (result): for dr, q in self._Q_LIST.items(): print(dr, q, len(q)) else: for dr, q in self._Q_LIST.items(): print(dr, list(q.keys()), len(q)) def print_summary(self, qualifiers=False, result=False): print("################################") print("INDEX: %s" % self.LABEL) print("EXPLORE DATA: %s-%s" % (self.from_draw, self.to_draw)) print( "Qualified Draws: %s, Efficiency: %s/%s=%s" % (self.qualified_draws, self.win, self.qualifiers, self.efficiency)) print("Per Draw Best: %s / worst: %s / Avg: %s " % (self.best, self.worst, self.avg)) if (qualifiers): self.print_qualifiers(result) print("#################################") def reset(self): #self.xplr_id=0 self.idx_id = 0 self.from_draw = 0 self.to_draw = 0 self.qualified_draws = 0 self.qualifiers = 0 self.win = 0 self.efficiency = 0 self.best = 0 self.worst = 0 self.avg = 0 def setup(self): d = None if (self.xplr_id > 0): d = self.db.session.query(iExplore).filter( iExplore.xplr_id == self.xplr_id).first() else: d = self.db.session.query(iExplore).filter( iExplore.label == self.label).first() if d is not None: self.xplr_id = d.xplr_id self.idx_id = d.idx_id self.from_draw = d.from_draw self.to_draw = d.to_draw self.qualified_draws = d.qualified_draws self.qualifiers = d.qualifiers self.win = d.win self.efficiency = d.efficiency self.best = d.best self.worst = d.worst self.avg = d.avg
class iTicket(Base, DBBase): __tablename__ = 'tickets' db = db_init() tick_id = Column(Integer, primary_key=True, autoincrement=True) draw_id = Column(Integer) picks_total = Column(Integer) opt_x = Column(Boolean) strat_id = Column(VARCHAR(11)) sess_id = Column(BigInteger) t1 = Column(SmallInteger) t2 = Column(SmallInteger) t3 = Column(SmallInteger) t4 = Column(SmallInteger) t5 = Column(SmallInteger) t6 = Column(SmallInteger) t7 = Column(SmallInteger) t8 = Column(SmallInteger) t9 = Column(SmallInteger) t10 = Column(SmallInteger) t11 = Column(SmallInteger) t12 = Column(SmallInteger) t13 = Column(SmallInteger) t14 = Column(SmallInteger) t15 = Column(SmallInteger) t16 = Column(SmallInteger) t17 = Column(SmallInteger) t18 = Column(SmallInteger) t19 = Column(SmallInteger) t20 = Column(SmallInteger) t21 = Column(SmallInteger) t22 = Column(SmallInteger) t23 = Column(SmallInteger) t24 = Column(SmallInteger) t25 = Column(SmallInteger) t26 = Column(SmallInteger) t27 = Column(SmallInteger) t28 = Column(SmallInteger) t29 = Column(SmallInteger) t30 = Column(SmallInteger) t31 = Column(SmallInteger) t32 = Column(SmallInteger) t33 = Column(SmallInteger) t34 = Column(SmallInteger) t35 = Column(SmallInteger) t36 = Column(SmallInteger) t37 = Column(SmallInteger) t38 = Column(SmallInteger) t39 = Column(SmallInteger) t40 = Column(SmallInteger) t41 = Column(SmallInteger) t42 = Column(SmallInteger) t43 = Column(SmallInteger) t44 = Column(SmallInteger) t45 = Column(SmallInteger) t46 = Column(SmallInteger) t47 = Column(SmallInteger) t48 = Column(SmallInteger) t49 = Column(SmallInteger) t50 = Column(SmallInteger) t51 = Column(SmallInteger) t52 = Column(SmallInteger) t53 = Column(SmallInteger) t54 = Column(SmallInteger) t55 = Column(SmallInteger) t56 = Column(SmallInteger) t57 = Column(SmallInteger) t58 = Column(SmallInteger) t59 = Column(SmallInteger) t60 = Column(SmallInteger) t61 = Column(SmallInteger) t62 = Column(SmallInteger) t63 = Column(SmallInteger) t64 = Column(SmallInteger) t65 = Column(SmallInteger) t66 = Column(SmallInteger) t67 = Column(SmallInteger) t68 = Column(SmallInteger) t69 = Column(SmallInteger) t70 = Column(SmallInteger) t71 = Column(SmallInteger) t72 = Column(SmallInteger) t73 = Column(SmallInteger) t74 = Column(SmallInteger) t75 = Column(SmallInteger) t76 = Column(SmallInteger) t77 = Column(SmallInteger) t78 = Column(SmallInteger) t79 = Column(SmallInteger) t80 = Column(SmallInteger) tx = Column(Integer) t_bin0140 = Column(BigInteger) t_bin4180 = Column(BigInteger) t_matches = Column(Integer) tx_match = Column(Boolean) t_prize = Column(Float(2, True)) tickDict = dict([(i, 0) for i in range(1, 81)]) def __init__(self, tickID=0, drawID=0): self.reset() self.tick_id = tickID self.draw_id = drawID if (self.tick_id > 0): self.setup() super().setupDBBase(iTicket, iTicket.tick_id, self.tick_id) def add_table(self): self.create_table() def reset(self): self.draw_id = 0 self.opt_x = 0 self.picks_total = 0 self.tx = 0 self.t_bin0140 = 0 self.t_bin4180 = 0 self.t_matches = 0 self.tx_match = 0 self.t_prize = 0 for i in range(81): self.tickDict[i] = 0 if i > 0: setattr(self, "t" + str(i), 0) self.tx = 0 def derive_bin(self): return 0 def setStratID(self, stratID): self.strat_id = stratID def getStratID(self): return self.strat_id def setSessionID(self, sessID): self.sess_id = sessID def getSessionID(self): return self.sess_id def setup(self): d = self.db.session.query(iTicket).filter( iTicket.tick_id == self.tick_id).first() if d is not None: self.picks_total = d.picks_total self.opt_x = d.opt_x self.strat_id = d.strat_id self.sess_id = d.sess_id for i in self.tickDict.keys(): if (i > 0): setattr(self, "t" + str(i), getattr(d, "t" + str(i))) #self.drawDict[i]=getattr(self, "t"+str(i)) self.tx = d.tx self.t_bin0140 = d.t_bin0140 self.t_bin4180 = d.t_bin4180 self.t_matches = d.t_matches self.tx_match = d.tx_match self.t_prize = d.t_prize def toString(self): return str(self.get_dict()) + "[" + str(self.tx) + "]" def get_dict(self): dict = {} dict[self.tick_id] = np.array([ self.t1, self.t2, self.t3, self.t4, self.t5, self.t6, self.t7, self.t8, self.t9, self.t10, self.t11, self.t12, self.t13, self.t14, self.t15, self.t16, self.t17, self.t18, self.t19, self.t20, self.t21, self.t22, self.t23, self.t24, self.t25, self.t26, self.t27, self.t28, self.t29, self.t30, self.t31, self.t32, self.t33, self.t34, self.t35, self.t36, self.t37, self.t38, self.t39, self.t40, self.t41, self.t42, self.t43, self.t44, self.t45, self.t46, self.t47, self.t48, self.t49, self.t50, self.t51, self.t52, self.t53, self.t54, self.t55, self.t56, self.t57, self.t58, self.t59, self.t60, self.t61, self.t62, self.t63, self.t64, self.t65, self.t66, self.t67, self.t68, self.t69, self.t70, self.t71, self.t72, self.t73, self.t74, self.t75, self.t76, self.t77, self.t78, self.t79, self.t80 ]) return dict def get_picks_size(self): return self.picks_total def derive_prize(self): pr = iprize.iPrize(self.picks_total, self.t_matches) if (self.tx == 1): self.t_prize = pr.prize_x else: self.t_prize = pr.prize def validate(self, drawID, d_bin0140, d_bin4180): t_matches = 0 if (self.draw_id == drawID): t_match_0140 = d_bin0140 & self.t_bin0140 t_match_4180 = d_bin4180 & self.t_bin4180 print("Draw 1-40: ", '{0:040b}'.format(d_bin0140)) print("Ticket 1-40: ", '{0:040b}'.format(self.t_bin0140)) print("Match 1-40: ", '{0:040b}'.format(t_match_0140)) print(" ") print("Draw 41-80: ", '{0:040b}'.format(d_bin0140)) print("Ticket 41-80: ", '{0:040b}'.format(self.t_bin4180)) print("Match 41-80: ", '{0:040b}'.format(t_match_4180)) t_matches += bin(t_match_0140).count("1") t_matches += bin(t_match_4180).count("1") self.t_matches = t_matches self.derive_prize() #update db #self.db_update({'t_matches':self.t_matches, 'tx_match': self.tx_match, 't_prize':self.t_prize},{'tick_id': self.tick_id}) #self.db_update({iTicket.t_matches:self.t_matches, iTicket.tx_match: self.tx_match, iTicket.t_prize:self.t_prize},{iTicket.tick_id: self.tick_id}) query = self.db.session.query(iTicket).filter( iTicket.tick_id == self.tick_id).update( { iTicket.t_matches: self.t_matches, iTicket.tx_match: self.tx_match, iTicket.t_prize: self.t_prize }, synchronize_session='fetch') self.db.session.commit() def fill_ticket(self, drawID, pick_array, pick_x=0): print("Filling ticket for", pick_array) self.draw_id = drawID b0140 = ibin.iBin() b4180 = ibin.iBin() bx = ibin.iBin() self.picks_total = len(pick_array) if (pick_x > 0): self.opt_x = 1 self.tx = pick_x for p in pick_array: setattr(self, "t" + str(p), 1) if p > 40: posn = p - 40 b4180.set_bit(posn) else: b0140.set_bit(p) #self.t_bin0140 = bytes('{0:040b}'.format(b0140.get_bin()), 'utf-8'); #self.t_bin4180 = bytes('{0:040b}'.format(b4180.get_bin()), 'utf-8'); #self.t_bin0140 = '{0:040b}'.format(b0140.get_bin()); #self.t_bin4180 = '{0:040b}'.format(b4180.get_bin()); # self.t_bin0140 = b0140.get_bin().to_bytes(40, byteorder='big'); # self.t_bin4180 = b4180.get_bin().to_bytes(40, byteorder='big'); #self.t_bin0140 = b0140.to_string(); #self.t_bin4180 = b4180.to_string(); self.t_bin0140 = b0140.get_bin() #770947678208 self.t_bin4180 = b4180.get_bin() #8606711840 # self.t_bin0140 = bin(b0140.get_bin()); #0b1011001110000000000100000000000000000000 # self.t_bin4180 = bin(b4180.get_bin()); #0b1000000001000000000000000000100000 def print_bins(self): print('{0:040b}'.format(self.t_bin0140)) print('{0:040b}'.format(self.t_bin4180)) def print_ticket(self): print(str(self.draw_id), ":[", self.get_dict(), "]") print('{0:040b}'.format(self.t_bin0140), '{0:040b}'.format(self.t_bin4180)) def print_ticket_result(self): print(str(self.draw_id), ":[", self.get_dict(), "]") print('{0:040b}'.format(self.t_bin0140), '{0:040b}'.format(self.t_bin4180)) print("pick size: ", len(pick_array)) print(self.toString())
class Index(Base, DBBase): __tablename__ = 'indexes' db = db_init() idx_id = Column(Integer, primary_key=True, autoincrement=True) label = Column(VARCHAR(20)) desc = Column(Text(1000)) overall_qualified_draws = Column(Integer) overall_qualified_nums = Column(Integer) overall_wins = Column(Integer) overall_efficiency = Column(DECIMAL(5, 4)) overall_best = Column(Integer) overall_worst = Column(Integer) overall_avg = Column(DECIMAL(6, 4)) explores = relationship("iExplore", backref='my_index') #explores = relationship("iexplore.iExplore", foreign_keys=idx_id, backref='my_index', primary_join="iExplore.idx_id==Index.idx_id") def __init__(self, Label=None, Desc=None, idxID=0): self.label = Label self.desc = Desc self.idx_id = idxID if (self.label != None or self.idx_id > 0): self.setup() super().setupDBBase(Index, Index.idx_id, self.idx_id) def register(self): print("Index.register(): Want to register %s %s" % (self.label, self.desc)) d = self.db.session.query(Index).filter( Index.label == self.label).first() if (d == None): self.db_save() print("Registered index %s %s" % (self.label, self.desc)) else: print("Index already registered") def reset(self): print("Resetting Index") self.label = None self.desc = None self.overall_qualified_draws = 0 self.overall_qualified_nums = 0 self.overall_wins = 0 self.overall_efficiency = 0 self.overall_best = 0 self.overall_worst = 0 self.overall_avg = 0 def setup(self): d = None print("Setting up index %s" % self.label) if (self.label != None): d = self.db.session.query(Index).filter( Index.label == self.label).first() print("pulling index by label") else: d = self.db.session.query(Index).filter( Index.idx_id == self.idx_id).first() print("pulling index by id") if d is not None: self.idx_id = d.idx_id self.label = d.label self.desc = d.desc self.overall_qualified_draws = d.overall_qualified_draws self.overall_qualified_nums = d.overall_qualified_nums self.overall_wins = d.overall_wins self.overall_efficiency = d.overall_efficiency self.overall_best = d.overall_best self.overall_worst = d.overall_worst self.overall_avg = d.overall_avg
class iPreds(Base, DBBase): __tablename__ = 'preds' db = db_init() sess_id = Column(BigInteger, primary_key=True, autoincrement=False) p1 = Column(DECIMAL(6, 4)) p2 = Column(DECIMAL(6, 4)) p3 = Column(DECIMAL(6, 4)) p4 = Column(DECIMAL(6, 4)) p5 = Column(DECIMAL(6, 4)) p6 = Column(DECIMAL(6, 4)) p7 = Column(DECIMAL(6, 4)) p8 = Column(DECIMAL(6, 4)) p9 = Column(DECIMAL(6, 4)) p10 = Column(DECIMAL(6, 4)) p11 = Column(DECIMAL(6, 4)) p12 = Column(DECIMAL(6, 4)) p13 = Column(DECIMAL(6, 4)) p14 = Column(DECIMAL(6, 4)) p15 = Column(DECIMAL(6, 4)) p16 = Column(DECIMAL(6, 4)) p17 = Column(DECIMAL(6, 4)) p18 = Column(DECIMAL(6, 4)) p19 = Column(DECIMAL(6, 4)) p20 = Column(DECIMAL(6, 4)) p21 = Column(DECIMAL(6, 4)) p22 = Column(DECIMAL(6, 4)) p23 = Column(DECIMAL(6, 4)) p24 = Column(DECIMAL(6, 4)) p25 = Column(DECIMAL(6, 4)) p26 = Column(DECIMAL(6, 4)) p27 = Column(DECIMAL(6, 4)) p28 = Column(DECIMAL(6, 4)) p29 = Column(DECIMAL(6, 4)) p30 = Column(DECIMAL(6, 4)) p31 = Column(DECIMAL(6, 4)) p32 = Column(DECIMAL(6, 4)) p33 = Column(DECIMAL(6, 4)) p34 = Column(DECIMAL(6, 4)) p35 = Column(DECIMAL(6, 4)) p36 = Column(DECIMAL(6, 4)) p37 = Column(DECIMAL(6, 4)) p38 = Column(DECIMAL(6, 4)) p39 = Column(DECIMAL(6, 4)) p40 = Column(DECIMAL(6, 4)) p41 = Column(DECIMAL(6, 4)) p42 = Column(DECIMAL(6, 4)) p43 = Column(DECIMAL(6, 4)) p44 = Column(DECIMAL(6, 4)) p45 = Column(DECIMAL(6, 4)) p46 = Column(DECIMAL(6, 4)) p47 = Column(DECIMAL(6, 4)) p48 = Column(DECIMAL(6, 4)) p49 = Column(DECIMAL(6, 4)) p50 = Column(DECIMAL(6, 4)) p51 = Column(DECIMAL(6, 4)) p52 = Column(DECIMAL(6, 4)) p53 = Column(DECIMAL(6, 4)) p54 = Column(DECIMAL(6, 4)) p55 = Column(DECIMAL(6, 4)) p56 = Column(DECIMAL(6, 4)) p57 = Column(DECIMAL(6, 4)) p58 = Column(DECIMAL(6, 4)) p59 = Column(DECIMAL(6, 4)) p60 = Column(DECIMAL(6, 4)) p61 = Column(DECIMAL(6, 4)) p62 = Column(DECIMAL(6, 4)) p63 = Column(DECIMAL(6, 4)) p64 = Column(DECIMAL(6, 4)) p65 = Column(DECIMAL(6, 4)) p66 = Column(DECIMAL(6, 4)) p67 = Column(DECIMAL(6, 4)) p68 = Column(DECIMAL(6, 4)) p69 = Column(DECIMAL(6, 4)) p70 = Column(DECIMAL(6, 4)) p71 = Column(DECIMAL(6, 4)) p72 = Column(DECIMAL(6, 4)) p73 = Column(DECIMAL(6, 4)) p74 = Column(DECIMAL(6, 4)) p75 = Column(DECIMAL(6, 4)) p76 = Column(DECIMAL(6, 4)) p77 = Column(DECIMAL(6, 4)) p78 = Column(DECIMAL(6, 4)) p79 = Column(DECIMAL(6, 4)) p80 = Column(DECIMAL(6, 4)) def __init__(self, sessID=0): self.sess_id = sessID self.reset() super().setupDBBase(iPreds, iPreds.sess_id, self.sess_id) def add_table(self): self.create_table() def reset(self): for i in range(81): if i > 0: setattr(self, "p" + str(i), 0.0000) def set_pred(self, pos, pred): setattr(self, "p" + str(pos), pred) def get_pred(self, pos): return getattr(self, "p" + str(pos))
class ExploreLog(Base, DBBase): __tablename__ = 'explore_logs' db = db_init() xplr_log_id = Column(Integer, primary_key=True, autoincrement=True) xplr_id = Column(Integer, ForeignKey("explores.xplr_id")) idx_id = Column(Integer) draw_id = Column(Integer) x_bin0140 = Column(BigInteger) x_bin4180 = Column(BigInteger) qualifiers = Column(Integer) wins = Column(Integer) efficiency = Column(DECIMAL(5, 4)) #my_explore = relationship("iexplore.iExplore", foreign_keys=xplr_id, backref='logs', primaryjoin="iExplore.xplr_id==ExploreLog.xplr_id") #my_index = relationship("Index", backref='my_logs') array_qualifiers = [] def __init__(self, xplrID=0, idxID=0, drawID=0, ar_qualifiers=[], d_bin0140=0, d_bin4180=0): self.xplr_id = xplrID self.idx_id = idxID self.draw_id = drawID self.array_qualifiers = ar_qualifiers self.qualifiers = len(ar_qualifiers) self.set_bin() self.validate_qualifiers(d_bin0140, d_bin4180) self.calc_efficiency() #if (self.label != None or self.idx_id > 0 ) : self.setup() super().setupDBBase(ExploreLog, ExploreLog.xplr_log_id, self.xplr_log_id) def set_bin(self): b0140 = ibin.iBin() b4180 = ibin.iBin() for i in self.array_qualifiers: if i > 40: b4180.set_bit(i) else: b0140.set_bit(i) self.x_bin0140 = b0140.get_bin() #833831981626 for 2277311 self.x_bin4180 = b4180.get_bin() #206435647488 for 2277311 def validate_qualifiers(self, d_bin0140, d_bin4180): self.wins = 0 win_1 = d_bin0140 & self.x_bin0140 win_2 = d_bin4180 & self.x_bin4180 self.wins += bin(win_1).count("1") self.wins += bin(win_2).count("1") def calc_efficiency(self): if self.qualifiers > 0: self.efficiency = self.wins / self.qualifiers else: self.efficiency = 0 def reset(self): self.xplr_log_id = 0 self.xplr_id = 0 self.idx_id = 0 self.draw_id = 0 self.x_bin0140 = 0 self.x_bin4180 = 0 self.qualifiers = 0 self.wins = 0 self.efficiency = 0 #NOT SETUP def setup(self): d = None if (self.label == None): d = self.db.session.query( index.Index).filter(index.Index.idx_id == self.idx_id).first() else: d = self.db.session.query( index.Index).filter(index.Index.label == self.label).first() if d is not None: self.xplr_log_id = d.xplr_log_id self.xplr_id = d.xplr_id self.idx_id = d.idx_id self.draw_id = d.draw_id self.x_bin0140 = d.x_bin0140 self.x_bin4180 = d.x_bin4180 self.qualifiers = d.qualifiers self.wins = d.wins self.efficiency = d.efficiency
class iSession(Base, DBBase): __tablename__ = 'sessions' db = db_init() sess_id = Column(BigInteger, primary_key=True, autoincrement=False) strat_id = Column(VARCHAR(11)) date_time = Column(DateTime) tickets_total = Column(Integer) tickets_claimed = Column(Integer) cost_total = Column(DECIMAL(11, 2)) prize_total = Column(DECIMAL(11, 2)) pnl = Column(DECIMAL(11, 2)) match_max = Column(Integer) prize_max = Column(DECIMAL(11, 2)) match_min = Column(Integer) prize_min = Column(DECIMAL(11, 2)) match_average = Column(Integer) prize_average = Column(DECIMAL(11, 2)) picks = 0 #sessions = relationship('draws',) def __init__(self, sessionID=0): self.reset() self.generateSessionID() if (sessionID > 0): self.sess_id = sessionID super().setupDBBase(iSession, iSession.sess_id, self.sess_id) def reset(self): self.sess_id = 0 self.strat_id = 0 self.date_time = datetime.now() self.tickets_total = 0 self.tickets_claimed = 0 self.cost_total = 0 self.prize_total = 0 self.pnl = 0 self.match_max = 0 self.prize_max = 0 self.match_min = 0 self.prize_min = 0 self.match_average = 0 self.prize_average = 0 self.picks = 0 def add_table(self): self.create_table() def generateSessionID(self): sess_id_format = "%Y%m%d%H%M%S" self.date_time = datetime.now() self.set_session_id(self.date_time.strftime(sess_id_format)) print("New Session ID: ", self.sess_id) return self.sess_id def save(self): self.db_save() def get_session_id(self): return self.sess_id def set_session_id(self, sessID): self.sess_id = sessID def get_tickets_all(self): ticks = self.db.session.query(iticket.iTicket).filter( iticket.iTicket.sess_id == self.sess_id).all() #ticks = self.db.session.commit(); self.picks = ticks[0].picks_total return ticks def updateSessionMetrics(self): self.match_max = self.db.session.query( func.max(iticket.iTicket.t_matches)).filter( iticket.iTicket.sess_id == self.sess_id).scalar() self.prize_total = self.db.session.query( func.sum(iticket.iTicket.t_prize)).filter( iticket.iTicket.sess_id == self.sess_id).scalar() self.cost_total = self.tickets_total * 1.00 self.tickets_claimed = self.db.session.query( func.count(iticket.iTicket.t_prize)).filter( iticket.iTicket.sess_id == self.sess_id).filter( iticket.iTicket.t_prize > 0).scalar() #iSession.update().where(iSession.sess_id == self.sess_id).values(iSession.match_max=self.match_max, iSession.prize_total = self.prize_total, iSession.cost_total=self.cost_total, iSession.prize_total = self.prize_total) self.db.session.query(iSession).filter( iSession.sess_id == self.sess_id).update( { iSession.match_max: self.match_max, iSession.prize_total: self.prize_total, iSession.cost_total: self.cost_total, iSession.prize_total: self.prize_total }, synchronize_session='fetch') def printSummary(self): print("Strategy: ", self.strat_id, "Session ID: ", self.sess_id) print("Total Tickets: ", self.tickets_total, "Picks/Tickets: ") print("Max Match: ", self.match_max, "Tickets Claimed: ", self.tickets_claimed) print("Total Cost: ", self.cost_total, "Total Prize: ", self.prize_total)
class HotSpot: db = db_init() def get_last_draw_id(self): res = iresult.iResult() return res.getLastDrawID() def derive_draw(self, draw_id): new_draw = idraw.iDraw(draw_id) new_draw.derive() new_draw.db_save() def derive_draws(self, start_id, end_id): next_id = start_id while (next_id <= end_id): self.derive_draw(next_id) next_id += 1 def derive_depth(self, draw_id): new_depth = idepth.iDepth(draw_id) new_depth.derive() new_depth.db_save() def derive_depths(self, start_id, end_id): next_id = start_id while (next_id <= end_id): self.derive_depth(next_id) next_id += 1 def find_gaps_draws(self, start_id, end_id): gaps = list() next_id = start_id + 1 master_draws_d = self.get_all_draws() switch_on = 0 while (next_id < end_id): try: d = master_draws_d[next_id] #print("Size of ", next_id, len(master_results_d[next_id])); if switch_on == 0: pass else: switch_on = str(switch_on) + " - " + str(next_id - 1) gaps.append(switch_on) switch_on = 0 except KeyError: if switch_on == 0: switch_on = next_id #gaps.append(switch_on) else: pass next_id += 1 gaps.append(next_id) print("Gaps Draws:", gaps) def setupTickets(self): t = iticket.iTicket() t.create_table() def find_gaps_results(self, start_id, end_id): gaps = list() next_id = start_id + 1 master_results_d = self.get_all_results() switch_on = 0 while (next_id < end_id): try: d = master_results_d[next_id] #print("Size of ", next_id, len(master_results_d[next_id])); if switch_on == 0: pass else: switch_on = str(switch_on) + " - " + str(next_id - 1) gaps.append(switch_on) switch_on = 0 except KeyError: if switch_on == 0: switch_on = next_id #gaps.append(switch_on) else: pass next_id += 1 gaps.append(next_id) print("Gaps Results:", gaps) def find_gaps_depths(self, start_id, end_id): gaps = list() next_id = start_id + 1 master_depths_d = self.get_all_depths() switch_on = 0 while (next_id < end_id): try: d = master_depths_d[next_id] #print("Size of ", next_id, len(master_results_d[next_id])); if switch_on == 0: pass else: switch_on = str(switch_on) + " - " + str(next_id - 1) gaps.append(switch_on) switch_on = 0 except KeyError: if switch_on == 0: switch_on = next_id #gaps.append(switch_on) else: pass next_id += 1 gaps.append(next_id) print("Gaps Depths:", gaps) def find_gaps(self, start_id=0, end_id=0): res = iresult.iResult() min_id = res.get_min('draw_id') max_id = res.get_max('draw_id') if (start_id == 0): start_id = min_id if (end_id == 0): end_id = max_id self.find_gaps_results(start_id, end_id) self.find_gaps_draws(start_id, end_id) self.find_gaps_depths(start_id, end_id) def fetch_single_draw(self, DrawID): f = Fetcher() res = f.fetch_result(DrawID) if (int(res.draw_id) == int(DrawID)): res.db_save() new_draw = idraw.iDraw(DrawID) new_draw.derive() new_draw.db_save() new_depth = idepth.iDepth(DrawID) new_depth.derive() new_depth.db_save() def sync(self, total_draws=0): f = Fetcher() f.sync_results() print("All Done!") def get_all_results(self): master_dict = {} results = self.db.session.query(iresult.iResult).order_by( iresult.iResult.draw_id.asc()).all() for result in results: master_dict.update(result.get_dict()) return master_dict def get_all_draws(self): master_dict = {} draws = self.db.session.query(idraw.iDraw).order_by( idraw.iDraw.draw_id.asc()).all() for draw in draws: master_dict.update(draw.get_dict()) return master_dict def get_all_depths(self): master_dict = {} depths = self.db.session.query(idepth.iDepth).order_by( idepth.iDepth.draw_id.asc()).all() for depth in depths: master_dict.update(depth.get_dict()) return master_dict def validate_db(self): val_obs = { 'missing': [], 'invalid': [] } master_results_d = self.get_all_results() master_draws_d = self.get_all_draws() master_depths_d = self.get_all_depths() master_draws_a = [] for key, value in master_draws_d.items(): master_draws_a.append(value) master_draws = np.matrix(master_draws_a) print(master_draws.shape) np.savez('master_draws', master_draws) # np.savez('master_draws', master_draws.values()) # np.savez('master_depths', master_depths.values()) # # first_draw_id = self.db.session.query(iresult.iResult).order_by(iresult.iResult.draw_id.asc()).first().draw_id; # last_draw_id = self.db.session.query(iresult.iResult).order_by(iresult.iResult.draw_id.asc()).first().draw_id; # next_draw_id = first_draw_id + 1; # # while (next_draw_id < last_draw_id): # print(master_results[next_draw_id]) # print(master_draws[next_draw_id]) # print(master_depths[next_draw_id]) # # next_draw_id +=1; # # next_draw_id = first_draw_id + 1; # while (next_draw_id < last_draw_id): # if next_draw_id not in master_results: val_obs['missing']['results']= next_draw_id; # if next_draw_id not in master_draws: val_obs['missing']['draws']= next_draw_id; # if next_draw_id in master_depths: val_obs['missing']['depths']= next_draw_id; # # next_draw_id +=1; # return val_obs def save_master_matrix(self): data_folder = 'strategies/ai/data/' master_results_d = self.get_all_results() master_draws_d = self.get_all_draws() master_depths_d = self.get_all_depths() master_results_a = [] master_draws_a = [] master_depths_a = [] master_ids_a = [] for key, value in master_draws_d.items(): master_ids_a.append(np.array([key])) master_draws_a.append(value) master_results_a.append(master_results_d.get(key)) for key, value in master_depths_d.items(): master_depths_a.append(value) master_ids = np.matrix(master_ids_a) master_results = np.matrix(master_results_a) master_draws = np.matrix(master_draws_a).astype(float) master_depths = np.matrix(master_depths_a).astype(float) np.savez(data_folder + 'data', ids=master_ids, results=master_results, draws=master_draws, depths=master_depths) np.savez(data_folder + 'master_ids', ids=master_ids) np.savez(data_folder + 'master_results', results=master_results) np.savez(data_folder + 'master_draws', draws=master_draws.astype(float)) np.savez(data_folder + 'master_depths', depths=master_depths.astype(float)) np.savez(data_folder + 'train_75k', ids=master_ids[0:75000], train=master_depths[0:75000], target=master_draws[1:75001]) np.savez(data_folder + 'val_75k', ids=master_ids[75000:77000], val=master_depths[75000:77000], target=master_draws[75001:77001]) np.savez(data_folder + 'test_75k', ids=master_ids[77000:], test=master_depths[77000:-1], target=master_draws[77001:]) np.savez(data_folder + 'train_1', ids=master_ids[0:10000], train=master_depths[0:10000], target=master_draws[1:10001]) np.savez(data_folder + 'val_1', ids=master_ids[10000:11000], val=master_depths[10000:11000], target=master_draws[10001:11001]) np.savez(data_folder + 'test_1', ids=master_ids[11000:12000], test=master_depths[11000:12000], target=master_draws[11001:12001]) np.savez(data_folder + 'train_2', ids=master_ids[12000:22000], train=master_depths[12000:22000], target=master_draws[12001:22001]) np.savez(data_folder + 'val_2', ids=master_ids[22000:23000], val=master_depths[22000:23000], target=master_draws[22001:23001]) np.savez(data_folder + 'test_2', ids=master_ids[23000:24000], test=master_depths[23000:24000], target=master_draws[23001:24001]) np.savez(data_folder + 'train_3', ids=master_ids[24000:34000], train=master_depths[24000:34000], target=master_draws[24001:34001]) np.savez(data_folder + 'val_3', ids=master_ids[34000:35000], val=master_depths[34000:35000], target=master_draws[34001:35001]) np.savez(data_folder + 'test_3', ids=master_ids[35000:36000], test=master_depths[35000:36000], target=master_draws[35001:36001]) #h = HotSpot(); #h.sync() #print(h.save_master_matrix())
class iReport: # https://www.dataquest.io/blog/excel-and-pandas/ db = db_init() _FILE = None _WORKBOOK = None _WORKSHEET = None _FILE_NAME = '' _FILE_FORMAT = 'xlsx' _FILE_LOCATION_R = '/data/reports/' _FILE_LOCATION = r'Y:/rancheros/eclipse/instance/data/workspaces/workspace4i8ynyxq64yvcgk8/medikid-PyLotto/data/reports/' _WRITER = None _DF = None headers = [] rows = {} def __init__(self, FileName=None, FileFormat="xlsx"): self._FILE_FORMAT = FileFormat if (FileName == None): print("File Name not set") else: self._FILE_NAME = FileName self.set_writer() #self.file_open(); def get_file_name(self): return Utils.getTimeStamp( ) + '_Hotspot_' + self._FILE_NAME + "." + self._FILE_FORMAT def get_file(self): return self._FILE_LOCATION + self.get_file_name() return os.path.join(self._FILE_LOCATION, self.get_file_name()) def set_writer(self): if (self._FILE_FORMAT == "csv"): self._WRITER = pd.DataFrame.to_csv(self.get_file()) elif (self._FILE_FORMAT == "xlsx"): self._WRITER = pd.ExcelWriter(self.get_file(), engine='xlsxwriter') def set_workbook(self): self._WORKBOOK = self._WRITER.book def set_worksheet(self, SheetName='Data'): self._WORKSHEET = self._WRITER.sheets[SheetName] def write(self, SheetName=None): sn = '' if (SheetName == None): sn = self._FILE_NAME else: sn = SheetName if (self._FILE_FORMAT == "csv"): self._WRITER = self._DF.to_csv(self.get_file()) elif (self._FILE_FORMAT == "xlsx"): self._DF.to_excel(self._WRITER, sheet_name=sn) def write_dataframe(self, DF=None, SheetName='Data'): if (DF is not None): try: DF.to_excel(self._WRITER, index=None, sheet_name=SheetName) except: print("Could not write dataframe") def data_query(self): results = [] print("Not Implemented") return results def process_dataframe(self): return self._DF def get_dataframe(self): query = self.data_query() self._DF = pd.read_sql(query.statement, self.db.session.bind) self._DF = self.process_dataframe() return self._DF def add_format(self): self.set_workbook() self.set_worksheet() def set_header_bold(self): header_fmt = self._WORKBOOK.add_format({'bold': True}) self._WORKSHEET.set_row(0, None, header_fmt) def file_open(self): try: self.set_writer() print("Opened file", self._FILE_NAME) except FileNotFoundError: print("File does not exist") except: print("Other error") def run(self): self.file_open() self.data_query() self.process_dataframe() self.write() self.set_header_bold() self.file_close() def file_close(self): self._WRITER.save()