Skip to content

Commit f2c349e

Browse files
committed
Code health improvements
1 parent d3965ac commit f2c349e

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

Diff for: generate_directory.py3

+60-36
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ DataRow = namedtuple(
1616
[
1717
'ttimestamp',
1818
'email',
19-
'name',
19+
'fullname',
2020
'facebook',
2121
'linkedin',
2222
'twitter',
@@ -59,21 +59,40 @@ DataRow = namedtuple(
5959

6060
"""
6161
Loads a manual blacklist from 'blacklist.csv' for people who have been having problems with their profile pictures. Reason is that people without images must appear at the end of the website, otherwise it looks bad.
62+
63+
Blacklist file format:
64+
```
65+
66+
67+
Foo Bar # fullname, be careful with collisions
68+
...
69+
```
70+
6271
"""
6372
def load_blacklist():
6473
blacklist = []
6574
try:
6675
with open(BLACKLIST_FILE) as blacklist_fp:
67-
for email in blacklist_fp:
68-
email = email.rstrip("\n\r")
69-
if email:
70-
print("INFO: blacklisting: %s.", email, file=sys.stderr)
71-
blacklist.append(email)
76+
for blacklist_entry in blacklist_fp:
77+
blacklist_entry = blacklist_entry.rstrip("\n\r")
78+
if blacklist_entry:
79+
print("INFO: blacklisting: ", blacklist_entry, file=sys.stderr)
80+
blacklist.append(blacklist_entry)
7281
except IOError as e:
7382
print("INFO: blacklist.csv file not found.", file=sys.stderr)
7483
return blacklist
7584

7685

86+
# Todo: try to minimeze collisions, somehow...
87+
def is_blacklisted(blacklist, person_tuple):
88+
if (person_tuple.email in blacklist):
89+
return True
90+
91+
if (person_tuple.fullname in blacklist):
92+
return True
93+
94+
return False
95+
7796
"""
7897
Loads a CSV file that contains <URL_FROM>,<URL_TO> for people who are having troubles getting the URL right, and the logic in this script deson't solve it
7998
"""
@@ -91,20 +110,18 @@ def load_swaps():
91110
return swaps
92111

93112

94-
def convertDataRow(rawRow):
113+
def convert_data_row(rawRow):
95114
return DataRow(*rawRow)
96115

97116

98117
def put_no_pictures_at_end(data, blacklist):
99118
result = []
100-
picture_ok = 0
101119
for row in data:
102120
if (not row.picture):
103121
result.append(row)
104-
elif (row.email in blacklist):
122+
elif is_blacklisted(blacklist, row):
105123
result.append(row)
106124
else:
107-
picture_ok += 1
108125
result.insert(0, row)
109126

110127
return result
@@ -122,29 +139,31 @@ def mix_women_men(data, blacklist):
122139
j = 0
123140
result = []
124141
while (i < len(women) and j < len(menAndDefault)):
125-
if women[i].email in blacklist:
142+
if is_blacklisted(blacklist, women[i]):
126143
break
127-
if menAndDefault[i].email in blacklist:
144+
if is_blacklisted(blacklist, menAndDefault[i]):
128145
break
129146
result.append(women[i])
130147
result.append(menAndDefault[j])
131148
i += 1
132149
j += 1
133150

134-
# put women with pictures
151+
# put any leftover people with pictures
152+
135153
while (i < len(women)):
136-
if women[i].email in blacklist:
154+
if is_blacklisted(blacklist, women[i]):
137155
break
138156
result.append(women[i])
139157
i += 1
140158

141-
# put mens with pictures
142159
while (j < len(menAndDefault)):
143-
if menAndDefault[i].email in blacklist:
160+
if is_blacklisted(blacklist, menAndDefault[i]):
144161
break
145162
result.append(menAndDefault[j])
146163
j += 1
147164

165+
# put any leftover people without pictures
166+
148167
while (i < len(women)):
149168
result.append(women[i])
150169
i += 1
@@ -156,6 +175,28 @@ def mix_women_men(data, blacklist):
156175
return result
157176

158177

178+
def process_profile_picture(person_tuple, swaps):
179+
profile_picture_url = person_tuple.picture
180+
181+
if (profile_picture_url in swaps):
182+
profile_picture_url = swaps[profile_picture_url]
183+
184+
profile_picture_url = profile_picture_url.replace(' ', '')
185+
186+
if not profile_picture_url.startswith('https://drive.google.com'):
187+
return profile_picture_url
188+
189+
slices = profile_picture_url.split('/')
190+
if len(slices) == 7:
191+
return 'https://drive.google.com/uc?export=view&id=' + slices[5]
192+
elif len(slices) == 4:
193+
return 'https://drive.google.com/uc?export=view&id=' + profile_picture_url.split('=')[1]
194+
else:
195+
print("ERROR %s (%s) with image %s" % (
196+
person_tuple.fullname, person_tuple.email, person_tuple.picture), file=sys.stderr)
197+
return ""
198+
199+
159200
def main():
160201
print("INFO: loading blacklist.", file=sys.stderr)
161202
blacklist = load_blacklist()
@@ -169,37 +210,20 @@ def main():
169210
data = []
170211
with open(INPUT_DATA) as csvfile:
171212
reader = csv.reader(csvfile)
172-
#reader.next()
173213
next(reader)
174214
for rawRow in reader:
175-
row = convertDataRow(rawRow)
215+
row = convert_data_row(rawRow)
176216
data.append(row)
177217

178218
data = shuffle_participants(data)
179219
data = put_no_pictures_at_end(data, blacklist)
180220
data = mix_women_men(data, blacklist)
181-
# data_copy = data[:picture_ok]
182-
# data_copy = shuffle_participants(data_copy)
183-
# data[:picture_ok] = data_copy
184221

185222
for row in data:
186-
# We process Google Dirve images.
187-
profile_picture_url = row.picture
188-
if (profile_picture_url in swaps):
189-
profile_picture_url = swaps[profile_picture_url]
190-
191-
profile_picture_url = profile_picture_url.replace(' ', '')
192-
if profile_picture_url.startswith('https://drive.google.com'):
193-
slices = profile_picture_url.split('/')
194-
if len(slices) == 7:
195-
profile_picture_url = 'https://drive.google.com/uc?export=view&id=' + slices[5]
196-
elif len(slices) == 4:
197-
profile_picture_url = 'https://drive.google.com/uc?export=view&id=' + profile_picture_url.split('=')[1]
198-
else:
199-
print("ERROR %s (%s) with image %s",row.name, row.email, row.picture, file=sys.stderr)
223+
profile_picture_url = process_profile_picture(row, swaps)
200224

201225
user = {
202-
'name': row.name,
226+
'fullname': row.fullname,
203227
'role': row.role,
204228
'affiliation': row.affiliation,
205229
'picture': profile_picture_url,

Diff for: template.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@
115115
/* ---- fields ---- */
116116

117117
.field { margin: 10px 5px 0px 5px; }
118-
.name {
118+
.fullname {
119119
font-size: 110%;
120120
font-weight: bold;
121121
}
@@ -144,6 +144,11 @@
144144
<br/>
145145
<br/>
146146
<h1 align="center">Meet the <a href="http://sites.nyuad.nyu.edu/hackathon/">2022 NYUAD Hackathon</a> Participants for Social Good in the Arab World</h1>
147+
148+
<div align="center">
149+
<img alt="event logo" src="https://drive.google.com/uc?export=view&id=1HWmvHf8Yiq8rHeYk0mN4VoB4mIYdeFYV" border=0>
150+
</div>
151+
147152
<div align="center">
148153
<a target="_blank" title="find us on Facebook" href="https://www.facebook.com/NYUADHackathon/"><img alt="find us on facebook" src="https://drive.google.com/uc?export=view&id=1q_o4Xt0lvIY37HpPypU9oa1z-pef7G6D" border=0></a>
149154
<a target="_blank" title="follow me on twitter" href="https://twitter.com/NYUADHackathon"><img alt="follow me on twitter" src="https://drive.google.com/uc?export=view&id=1EW7TMi5c0dVdiGWruKSP2QEq9AKL5isR" border=0></a>
@@ -160,8 +165,8 @@ <h1 align="center">Meet the <a href="http://sites.nyuad.nyu.edu/hackathon/">2022
160165
</div>
161166
<div class="cell-right">
162167
<div class="floating-text" style="overflow-y: auto;">
163-
<div class="name field">
164-
<span class="name">{{u.name}}</span>
168+
<div class="fullname field">
169+
<span class="fullname">{{u.fullname}}</span>
165170
<span class="role">({{u.role}})</span>
166171
</div>
167172
<div class="affiliation field">

0 commit comments

Comments
 (0)