forked from ryanGT/teaching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_list_maker.py
187 lines (141 loc) · 4.84 KB
/
class_list_maker.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
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
import txt_mixin, os, misc_utils, txt_database
from numpy import column_stack, row_stack
def get_names_from_Banner_txt(filename):
#this approach assumes labels are in the top row (1 row of labels
#only) and that the column label for the student names is "Student Name"
mydb = txt_database.db_from_file(filename)
names = mydb.Student_Name
return names
def get_banner_ids(filename):
if type(filename) == list:
filename = filename[0]
#mysheet = spreadsheet.TabDelimSpreadSheet(filename)
#mysheet.ReadData()
#ids = mysheet.get_col(2)
mydb = txt_database.db_from_file(filename)
ids = mydb.ID
if ids[0].lower() == 'id':
ids.pop(0)
return ids
def combined_names_from_all_sections(fnlist):
all_names = []
poplist = ['Student Name']
for fn in fnlist:
names = get_names_from_Banner_txt(fn)
if names[0] in poplist:
names.pop(0)
all_names.extend(names)
all_names.sort()
return all_names
def split_names(name_list, drop_middle=True):
first_names = []
last_names = []
for name in name_list:
last, first = name.split(',', 1)
last = last.strip()
first = first.strip()
if drop_middle:
if first.find(' ') > -1:
first, middle = first.split(' ',1)
first = first.strip()
last_names.append(last)
first_names.append(first)
return last_names, first_names
def list_to_table_row(listin):
row_str = ' & '.join(listin)
row_str += ' \\\\'
return row_str
def _get_names(csvlist):
if type(csvlist) == str:
csvlist = [csvlist]
all_names = combined_names_from_all_sections(csvlist)
last_names, first_names = split_names(all_names)
return last_names, first_names
def make_class_list(csvlist, extra_col_labels=None, \
fmt_str=None, vrule='\\rule{0pt}{14pt}', \
hrule=None, headerpath=None, outpath=None, \
course='', semester='', section='', \
students_per_page=20):
last_names, first_names = _get_names(csvlist)
labels = ['Last Name', 'First Name']
if extra_col_labels is not None:
labels.extend(extra_col_labels)
if fmt_str is None:
N = len(labels)
fmt_str = '|l' * N + '|'
if headerpath is None:
if os.path.exists('header.tex'):
headerpath = 'header.tex'
else:
headerpath = '/Users/rkrauss/git/report_generation/class_list_header.tex'
headerline = '\\input{%s}' % headerpath
startline = '\\begin{tabular}{%s}' % fmt_str
latex_out = [headerline]
out = latex_out.append
out('\\pagestyle{fancy}')
ws = ' '*4
out(ws + '\\lhead{%s}' % course)
out(ws + '\\rhead{%s}' % semester)
out(ws + '\\chead{%s}' % section)
out(ws + '\\rfoot{\\thepage}')
out(ws + '\\lfoot{Ryan Krauss}')
out(ws + '\\cfoot{}')
out('\\renewcommand{\headrulewidth}{0pt}')
out('\\begin{document}')
out(startline)
out('\\hline')
label_row = list_to_table_row(labels)
out(label_row)
out('\\hline')
if extra_col_labels is None:
Nec = 0
else:
Nec = len(extra_col_labels)
Nstudents = len(last_names)
ilist = range(Nstudents)
for i, last, first in zip(ilist, last_names, first_names):
if (i % 2) == 0:
out('\\rowcolor[gray]{0.9}')
curlist = [last, first]
if hrule is None:
eclist = ['']*Nec
else:
eclist = [hrule]*Nec
curlist += eclist
last = curlist[-1]
last += ' ' + vrule
curlist[-1] = last
currow = list_to_table_row(curlist)
out(currow)
out('\\hline')
if i == students_per_page:
out('\\end{tabular}')
out('')
out(startline)
out('\\hline')
out(label_row)
out('\\hline')
out('\\end{tabular}')
out('\\end{document}')
if outpath is not None:
txt_mixin.dump(outpath, latex_out)
return latex_out
def dumpcsv(outpath, csvlist, ids=False, extra_col_labels=None):
last_names, first_names = _get_names(csvlist)
N = len(last_names)
vector_list = [last_names, first_names]
labels = ['Last Name', 'First Name']
if ids:
banner_ids = get_banner_ids(csvlist)
vector_list.append(banner_ids)
labels.append('ID')
if extra_col_labels is not None:
labels.extend(extra_col_labels)
Nec = len(extra_col_labels)
for i in range(Nec):
cur_empty = ['']*N
vector_list.append(cur_empty)
data = column_stack(vector_list)
data2 = row_stack([labels, data])
txt_mixin.dump_delimited(outpath, data2, delim=',', fmt='%s')
#spreadsheet.WriteMatrixtoCSV(data2, outpath)