-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselaesql.py
138 lines (122 loc) · 4.95 KB
/
selaesql.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from argparse import ArgumentParser
from core import SQL, Display
from selae.selae20 import Euromillions, EuromillionsDraw, EuroPrize
from configparser import ConfigParser
from datetime import datetime
from itertools import combinations
class EuroSQL(SQL):
def __init__(self):
SQL.__init__(self)
self.euromillions = Euromillions()
def config_dict(self):
cfg = ConfigParser()
cfg.read('sql.ini')
conn = cfg['Connection']
return dict([(k,v) for k,v in conn.items()])
def get_keys(self):
keys = self.get_table_keys(table='boles', key_name='id_sorteig')
return keys
def update(self):
"""
Actualitza la base de dates agafant les dades del fitxer euromillones.bin
"""
table = 'boles'
cols = ('data_sorteig', 'bola1', 'bola2', 'bola3', 'bola4', 'bola5', 'estrella1', 'estrella2', 'data_insercio')
keys = self.get_keys()
for draw in self.euromillions.data:
if draw.id in keys:
continue
display.verbose("INSERT INTO:", draw.id, draw.balls, draw.stars, level=0)
values = list(draw.balls)
values.append(draw.stars[0])
values.append(draw.stars[1])
values.append(f'{datetime.now():%Y-%m-%d %H:%M:%S}')
values.insert(0, f"{draw.date.to_date():%Y-%m-%d}")
self.insert_into(table, cols, values)
self.connection.commit()
def lastdraw(self):
return self.get_last_recordset('boles', 'data_sorteig DESC')
def init(self):
self.set_connection(self.config_dict())
self.euromillions.load()
def get_match(self, numbers, r=5):
"""
min: 2, max: len(numbers)
Obtenim els sortejos que continguen x boles iguals, ha de ser un nombre
entre 2 i 5.
"""
if r < 2:
r = 2
numbers = tuple(sorted(numbers))
where = ''
for combination in combinations(numbers, r):
for number in combination:
where += f"{number} IN (bola1, bola2, bola3, bola4, bola5) AND "
where = where.strip("AND ")
where += ' OR '
where = where.strip("OR ")
return self.match('boles', where)
def soupify(self, soup):
i=1
string = ""
for key in soup:
string += f"{i}.- [:{key}:] ->\n\t[#{len(soup[key])}: {', '.join(str(x) for x in soup[key])}#]\n"
i += 1
return string
def got_matches(self, r=4):
draws = self.euromillions.data
i=0
replier_draw = []
replied_draw = []
repeated_draws = []
draw_dict = {}
for draw in draws:
numbers = draw.balls
match = [md for md in self.get_match(numbers, r) if f"{draw.date.to_date():%Y/%m/%d}" > f"{md['data_sorteig']:%Y/%m/%d}"]
if len(match) > 0:
i += 1
display.print(
f"{i: >3}.- [{draw.id: >4}] {draw.date.to_date():%Y/%m/%d} {draw.balls} [{len(match)}] -> ",
[
(
f"[{mdraw['id_sorteig']}] {mdraw['data_sorteig']:%Y/%m/%d}",
mdraw['bola1'],
mdraw['bola2'],
mdraw['bola3'],
mdraw['bola4'],
mdraw['bola5'],
)
for mdraw in match
]
)
replier_draw.append(draw.id)
for mdraw in match:
if mdraw['id_sorteig'] not in replied_draw:
replied_draw.append(mdraw['id_sorteig'])
else:
repeated_draws.append(mdraw['id_sorteig'])
if draw_dict.get(draw):
draw_dict[draw.id].append(mdraw['id_sorteig'])
else:
draw_dict[draw.id] = [mdraw['id_sorteig']]
display.print ("REPLICATED:", len(replied_draw), replied_draw)
display.print ("REPLICANT:", len(replier_draw), replier_draw)
display.print ("REPEATED:", len(repeated_draws), repeated_draws)
display.print ("REPEATSET:", len(set(repeated_draws)), list(sorted(set(repeated_draws))))
display.print (self.soupify(draw_dict))
def run(self):
try:
self.update()
self.got_matches(args.same)
if args.same > 4:
self.got_matches(4)
finally:
self.connection.close()
if __name__ == "__main__":
arg_parser = ArgumentParser()
arg_parser.add_argument('-s', '--same', dest='same', action='store', type=int, default=4)
args = arg_parser.parse_args()
display = Display()
eurosql = EuroSQL()
eurosql.init()
eurosql.run()