forked from d101tm/tmstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtakealeap.py
More file actions
executable file
·101 lines (80 loc) · 3.66 KB
/
takealeap.py
File metadata and controls
executable file
·101 lines (80 loc) · 3.66 KB
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
#!/usr/bin/env python3
""" Create reports for the Take a Leap promotion:
Clubs earn $40 for every DCP level they increase from the previous year.
For example, a club going from not Distinguished to Select Distinguished earns $80.
New clubs are considered "not Distinguished" for the previous year. """
import tmutil, sys, csv
import tmglobals
myglobals = tmglobals.tmglobals()
levels = {' ':0, 'D': 1, 'S': 2, 'P': 3}
class myclub:
def __init__(self, dict):
for item in dict:
self.__dict__[item[0]] = item[1]
self.thisyear = levels[(self.clubdistinguishedstatus+' ')[0]]
self.lastyear = 0
if self.activemembers < 20:
self.needed = min(20-self.activemembers, self.membase+5-self.activemembers)
else:
self.needed = 0
clubs = {}
### Insert classes and functions here. The main program begins in the "if" statement below.
if __name__ == "__main__":
import tmparms
# Establish parameters
parms = tmparms.tmparms()
parms.add_argument('--tmyear', default=0, type=int, help="Toastmasters year to report on.")
parms.add_argument('--outfile', default='takealeap.html')
parms.add_argument('--csvfile', default='takealeap.csv')
# Add other parameters here
# Do global setup
myglobals.setup(parms)
curs = myglobals.curs
conn = myglobals.conn
if not parms.tmyear:
parms.tmyear = myglobals.today.year - 1 # Use the previous TM Year during July-December
# Find the latest date in the database for this and the previous TM years:
finder = "SELECT MAX(asof) FROM clubperf WHERE monthstart >= '%d-07-01' AND monthstart <= '%d-06-01'"
curs.execute(finder % (parms.tmyear, parms.tmyear+1))
thisyearsdate = curs.fetchall()[0]
curs.execute(finder % (parms.tmyear-1, parms.tmyear))
lastyearsdate = curs.fetchall()[0]
# Get current clubs
curs.execute("SELECT * FROM clubperf WHERE asof = %s", (thisyearsdate,))
# Get the column names
columns = [item[0] for item in curs.description]
columnsforcsv = columns[columns.index('district'):columns.index('color')]
columnsforcsv.append('laststatus')
columnsforcsv.append('delta')
columnsforcsv.append('needed')
for row in curs.fetchall():
club = myclub(list(zip(columns, row)))
clubs[club.clubnumber] = club
# Now, update to include last year's status
curs.execute("SELECT clubnumber, clubdistinguishedstatus FROM clubperf WHERE asof = %s", (lastyearsdate,))
for (clubnumber, status) in curs.fetchall():
try:
clubs[clubnumber].laststatus = status
clubs[clubnumber].lastyear = levels[(status+' ')[0]]
except KeyError:
pass
# Now, compute results
leapers = [[],[],[],[]] # For 0-3 levels
for club in list(clubs.values()):
club.delta = max(0,club.thisyear - club.lastyear)
if club.delta > 0:
leapers[club.delta].append(club)
# And create the output file
outfile = open(parms.outfile, 'w')
for i in range(1,4):
if leapers[i]:
outfile.write('<p>Congratulations to ')
outfile.write(tmutil.getClubBlock(leapers[i]))
outfile.write(' for earning $%d for leaping %d level%s.<p>\n' % (40*i, i, 's' if i > 1 else ''))
outfile.close()
csvfile = open(parms.csvfile, 'w')
csvwriter= csv.DictWriter(csvfile, fieldnames=columnsforcsv, extrasaction="ignore")
csvwriter.writeheader()
for club in sorted(list(clubs.values()), key=lambda c:'%s%s%.8d' % (c.division, c.area, c.clubnumber)):
csvwriter.writerow(club.__dict__)
csvfile.close()