-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSummary_Page
241 lines (212 loc) · 7.8 KB
/
Summary_Page
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
print("Status: 200 OK")
print("Content-type: text/html")
print("")
#Starting the website!
print("<!DOCTYPE html>");
print("<html>")
print("<head>")
print("<title>California Lottery Selection</title>")
print("<script src='https://code.jquery.com/jquery-2.2.4.min.js' crossorigin='anonymous'></script>")
print("<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css'>")
print("<script src='https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js'></script>")
print("<nav>")
print("<div class='nav-wrapper green darken-2'>")
print("<a href='#!' class='brand-logo'>Logo</a>")
print("<ul class='right hide-on-med-and-down'>")
print("<li><a href='evs.py'>Single Ticket</a></li>")
print("<li class='active'><a href='allev.py'>All Tickets</a></li>")
print("</ul>")
print("</div>")
print("</nav>")
print("</head>")
#Progress bar
print("<div class='progress'>")
print("<div class='determinate' style='width: 100%'></div>")
print("</div>")
#Collecting the selected option into the input dictionary
inp = {}
import os
qs = os.environ["QUERY_STRING"]
if len(qs) > 0:
args = qs.split("&")
for arg in args:
kv = arg.split("=")
inp[kv[0]] = kv[1]
#Basic packages necessary for our project
from bs4 import BeautifulSoup
import requests
class Game:
price = 0
name = ""
more = ""
prizes =[]
class Prize(Game):
amount =0
odds = 0
winners =0
claimed =0
available = 0
prizes_url='http://www.calottery.com/play/scratchers-games/top-prizes-remaining'
all_lottodata =[]
#Will be put into a module to extract the relevant data from the website whenever the program is called
#or maybe not.
r = requests.get(prizes_url)
soup = BeautifulSoup(r.text,"lxml")
data_table = soup.find('table',id='topprizetable')
rows = data_table.findAll('tr')
for row in rows:
if len(row) != 8:
continue
else:
cells = row.findAll('td')
g = Game()
g.price = int(str(cells[0].text.strip())[1:]) #convert to text, strip dollar sign and convert to int
g.name = cells[2].text.strip()
g.more = 'http://www.calottery.com'+str(cells[7].find('a')['href']) #explore this link for more information on all the prizes.
all_lottodata.append(g)
p = requests.get(g.more)
newsoup = BeautifulSoup(p.text,'lxml')
prizes_table = newsoup.find('table',class_='draw_games tag_even')
allprizes = prizes_table.findAll('tr')
g.prizes=[]
for prize in allprizes:
if len(prize) ==5:
pdata = prize.findAll('td')
if all(char.isalpha() for char in str(pdata[0].text.strip()) ):
p=Prize()
p.amount = str(pdata[0].text.strip()).replace(',','')
p.odds = int(str(pdata[1].text.strip()).replace(',',''))
p.winners = int(str(pdata[2].text.strip()).replace(',',''))
p.claimed = int(str(pdata[3].text.strip()).replace(',',''))
p.available = int(str(pdata[4].text.strip()).replace(',',''))
g.prizes.append(p)
else:
p = Prize()
p.amount = int(str(pdata[0].text.strip()).replace(',','')[1:])
p.odds = int(str(pdata[1].text.strip()).replace(',',''))
p.winners = int(str(pdata[2].text.strip()).replace(',',''))
p.claimed = int(str(pdata[3].text.strip()).replace(',',''))
p.available = int(str(pdata[4].text.strip()).replace(',',''))
g.prizes.append(p)
all_lottodata = [x for x in all_lottodata if x.name]
rows = []
for x in all_lottodata:
for y in x.prizes:
rows.append([x.name, y.amount, y.odds, y.winners, y.claimed,y.available, x.price])
class Ticket:
name = ''
prizes =[]
expected_value=0
new_expected_value = 0
value_change = 0
price = 0
prob = 0
N=0
n=0
def predcount(self):
N = sum(prize['total_winners'] for prize in self.prizes)/sum(prize['probability'] for prize in self.prizes)
n = sum(prize['available'] for prize in self.prizes)/sum(prize['probability'] for prize in self.prizes)
self.n = n
self.N = N
return(self)
def new_prizes(self):
for prize in self.prizes:
prize['new_probability'] = prize['available']/self.n
return(self)
tickets = []
def info(data):
names =[]
for row in rows:
names.append((row[0],row[6]))
names = set(names)
for name in names:
t=Ticket()
t.name = name[0]
t.price = int(name[1])
tickets.append(t)
return(tickets)
def prizes(lst, data):
tickets=[]
for ticket in lst:
ticketdata = list(filter(lambda x: x[0]==ticket.name and x[6] == ticket.price,data))
prizes=[]
for row in ticketdata:
try:
prize = {'prize':int(row[1]),'probability':1/int(row[2]),'total_winners':int(row[3]),'claimed':int(row[4]), 'available':int(row[5])}
except:
prize = {'prize':row[1],'probability':1/int(row[2]),'total_winners':int(row[3]),'claimed':int(row[4]), 'available':int(row[5])}
prizes.append(prize)
ticket.prizes = prizes
tickets.append(ticket)
return(tickets)
def ev(item):
prize_probs = []
for prize in item.prizes:
prize_prob = (prize['prize'],prize['probability'])
prize_probs.append(prize_prob)
initev = sum(i[0]*i[1] for i in prize_probs if i[0] != 'Ticket')
if prize_probs[-1][0]=='Ticket':
ticketprize = prize_probs[-1]
ticketprizevalue_wght = 1/(1/ticketprize[1]-1)
item.expected_value= (initev+ticketprizevalue_wght*initev)/item.price
else:
item.expected_value = initev/item.price
return(item)
def newev(item):
prize_probs = []
for prize in item.prizes:
prize_prob = (prize['prize'],prize['new_probability'])
prize_probs.append(prize_prob)
initev = sum(i[0]*i[1] for i in prize_probs if i[0] != 'Ticket')
if prize_probs[-1][0]=='Ticket':
ticketprize = prize_probs[-1]
ticketprizevalue_wght = 1/(1/ticketprize[1]-1)
item.new_expected_value= (initev+ticketprizevalue_wght*initev)/item.price
else:
item.new_expected_value = initev/item.price
return(item)
def deltavalue(item):
old=item.expected_value
new=item.new_expected_value
item.valuechange = (new-old)/old
return(item)
ticketlists = info(rows)
ticketlists = prizes(ticketlists,rows)
ticketlists = [ev(ticket) for ticket in ticketlists]
ticketlists = [t.predcount() for t in ticketlists]
ticketlists = [t.new_prizes() for t in ticketlists]
ticketlists = [newev(ticket) for ticket in ticketlists]
ticketlists = [deltavalue(ticket) for ticket in ticketlists]
ticketlists.sort(key = lambda x: x.price)
ticketlists.sort(key = lambda x: x.new_expected_value)
ticketlists.sort(key = lambda x: x.valuechange)
#Showcase all tickets
print("<body>")
print("<div class='container'>")
print("<h1 align='center'>SHOW ALL TICKETS!</h1>")
print("<form action='allev.py'>")
#Table header
print("<table id = 'alldata' class = 'striped'>")
print("<thead>")
print("<tr>")
print("<th>Ticket Name</th>")
print("<th>Ticket Price</th>")
print("<th>Current Expected Value</th>")
print("<th>Change in EV(%)</th>")
print("</tr>")
print("</thead>")
#Table body
print("<tbody>")
for ticket in ticketlists:
print("<tr>")
print("<td>"+ticket.name+"</td>")
print("<td>"+"{0:.2f}".format(ticket.price)+"</td>")
print("<td>"+"{0:.2f}".format(ticket.new_expected_value)+"</td>")
print("<td>"+"{0:.2f}".format(ticket.valuechange *100)+'%'"</td>")
print("</tr>")
print("</tbody>")
print("</table>")
print("</form>")
print("</div>")
print("</body>")
print("</html>")