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
|
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
from csv import reader
from optparse import OptionParser
class LotteryNumber:
rank = None
lottery = None
def __init__(self, rank, lottery):
self.rank = rank
self.lottery = lottery
def __cmp__(self, other):
if self.rank < other.rank:
return -1
elif self.rank > other.rank:
return 1
elif self.lottery > other.lottery:
return -1
elif self.lottery < other.lottery:
return 1
else:
return 0
def __str__(self):
return "%s/%s" % (("%.4f" % (self.rank,)).rstrip('0').rstrip('.'), self.lottery)
def __hash__(self):
return (int)((40 - self.rank) * 100000000) + self.lottery
class Contestant:
lotteryNumber = None
lotteryClass = None
name = None
def __init__(self, lotteryNumber, lotteryClass, name):
self.lotteryNumber = lotteryNumber
self.lotteryClass = lotteryClass
self.name = name
def __cmp__(self, other):
return cmp(self.lotteryNumber, other.lotteryNumber)
def __str__(self):
return self.name
class Suite:
lotteryNumber = None
contestants = None
def __init__(self, lotteryNumber):
self.lotteryNumber = lotteryNumber
self.contestants = []
def addContestant(self, contestant):
if contestant.lotteryNumber == self.lotteryNumber:
self.contestants.append(contestant)
def __cmp__(self, other):
if type(self) != type(other):
return 1
return cmp(self.lotteryNumber, other.lotteryNumber)
def __len__(self):
return len(self.contestants)
def __str__(self):
return ", ".join([str(contestant) for contestant in self.contestants])
class Lottery:
contestants = []
suites = {}
def __init__(self, csvfile):
data = reader(open(csvfile))
for row in data:
nextContestant = Contestant(LotteryNumber(float(row[2]), int(row[3])), row[1], row[0])
self.contestants.append(nextContestant)
if nextContestant.lotteryClass == "Suite":
if nextContestant.lotteryNumber in self.suites:
self.suites[nextContestant.lotteryNumber].addContestant(nextContestant)
else:
suite = Suite(nextContestant.lotteryNumber)
suite.addContestant(nextContestant)
self.suites[nextContestant.lotteryNumber] = suite
nextContestant = None
def printContestants(self):
index = 1
for contestant in sorted(self.contestants, reverse=True):
print "%s: %s - %s in %s" % (index, contestant.lotteryNumber, contestant.name, contestant.lotteryClass)
index += 1
def printGeneral(self):
index = 1
for contestant in sorted(self.contestants, reverse=True):
if contestant.lotteryClass != "General":
continue
print "%s: %s - %s" % (index, contestant.lotteryNumber, contestant.name)
index += 1
def printSuites(self):
index = 1
for suite in sorted(self.suites.values(), reverse=True):
print "%s: %s - %s" % (index, suite.lotteryNumber, suite)
index += 1
def printSizedSuites(self, size):
index = 1
for suite in sorted(self.suites.values(), reverse=True):
if len(suite) != size:
continue
print "%s: %s - %s" % (index, suite.lotteryNumber, suite)
index += 1
parser = OptionParser(usage = "usage: %prog [options] filename")
parser.add_option("-g", "--general", action="store_true", help="Print general selection contestants.")
parser.add_option("-s", "--suite", type="int", help="Print suite selection contestants in suites of SIZE. If SIZE is 0, print all suites.", metavar="SIZE")
parser.add_option("-a", "--all", action="store_true", help="Print all contestants.")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("You must give 1 csv file to parse.")
try:
lotto = Lottery(args[0])
except:
parser.error("Could not parse csv.")
if options.all == True:
lotto.printContestants()
if options.general == True:
lotto.printGeneral()
if options.suite != None:
if options.suite == 0:
lotto.printSuites()
else:
lotto.printSizedSuites(options.suite)
|