Skip to content

Commit 863be98

Browse files
committed
Run 2to3
This should complete the migration to Python 3.
1 parent 47dba9d commit 863be98

27 files changed

+176
-175
lines changed

analytics/apps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __future__ import unicode_literals
1+
22

33
from django.apps import AppConfig
44

analytics/views.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def total_requests(request, time_frame=None):
8383
timezone.activate(DISPLAY_TIME_ZONE)
8484
early_time, delta, format = get_time_bounds(time_frame)
8585
data = RequestCount.tabulate_requests(early_time, delta, lambda _: 1)
86-
labels, counts = itertools.izip(*((format_date(t, format), item.get(1, 0)) for t, item in data))
86+
labels, counts = list(zip(*((format_date(t, format), item.get(1, 0)) for t, item in data)))
8787
return HttpResponse(json.dumps({"labels": labels, "data": counts, "total": "{:,}".format(sum(counts))}), content_type="application/json")
8888

8989
USER_AGENT_TYPES = [
@@ -127,7 +127,7 @@ def logged_in_users(request, time_frame=None):
127127
early_time, delta, format = get_time_bounds(time_frame)
128128
data = RequestCount.tabulate_requests(early_time, delta, lambda _: 1, distinct_users=True)
129129
total_data = RequestCount.tabulate_requests(early_time, None, lambda _: 1, distinct_users=True)
130-
labels, counts = itertools.izip(*((format_date(t, format), item.get(1, 0)) for t, item in data))
130+
labels, counts = list(zip(*((format_date(t, format), item.get(1, 0)) for t, item in data)))
131131
return HttpResponse(json.dumps({"labels": labels, "data": counts, "total": "{:,}".format(total_data.get(1, 0))}), content_type="application/json")
132132

133133
SEMESTERS = [
@@ -170,7 +170,7 @@ def user_semesters(request, time_frame=None):
170170
labels = SEMESTERS
171171

172172
semester_buckets = [0 for _ in SEMESTERS]
173-
for semester, count in data.items():
173+
for semester, count in list(data.items()):
174174
if not semester or semester < 0 or semester >= len(semester_buckets):
175175
continue
176176
semester_buckets[semester] += count
@@ -184,7 +184,7 @@ def request_paths(request, time_frame=None):
184184
data = RequestCount.tabulate_requests(early_time, None, lambda request: request.path)
185185
labels = set(data.keys()) - set([None])
186186
counts = {label: data.get(label, 0) for label in labels}
187-
labels, counts = itertools.izip(*sorted(counts.items(), key=lambda x: x[1], reverse=True))
187+
labels, counts = list(zip(*sorted(list(counts.items()), key=lambda x: x[1], reverse=True)))
188188
if len(labels) > 15:
189189
labels = labels[:15]
190190
counts = counts[:15]

catalog/apps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __future__ import unicode_literals
1+
22

33
from django.apps import AppConfig
44

catalog/models.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from __future__ import unicode_literals
1+
22
from django.core.exceptions import ObjectDoesNotExist
33

44
from django.db import models
@@ -33,7 +33,7 @@ def combine(cls, attrs, unique_id):
3333
are combined together to form a course that satisfies each attribute's
3434
requirement, and a unique id is assigned to the course only if it makes
3535
sense for each attribute to exist multiple times in a list of courses"""
36-
new_attr = cls(" ".join(map(lambda a: a.requirement,attrs)),True)
36+
new_attr = cls(" ".join([a.requirement for a in attrs]),True)
3737
for attr in attrs:
3838
new_attr.course = attr.modify_course(new_attr.course)
3939
new_attr.needs_unique_id = new_attr.needs_unique_id and attr.needs_unique_id
@@ -189,7 +189,7 @@ def list_converter(value):
189189
"Custom Color": (CourseFields.custom_color, string_converter)
190190
}
191191

192-
FIELD_TO_CSV = {field_name: csv_header for csv_header, (field_name, _) in CSV_HEADERS.items()}
192+
FIELD_TO_CSV = {field_name: csv_header for csv_header, (field_name, _) in list(CSV_HEADERS.items())}
193193

194194
# Create your models here.
195195
class Course(models.Model):

catalog/tests.py

+29-29
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,23 @@ def test_json_object(self):
8888
course = Course.objects.get(subject_id="21M.030")
8989
result = course.to_json_object(full=False)
9090
expected = {
91-
CourseFields.subject_id: u"21M.030",
92-
CourseFields.title: u"World Music",
91+
CourseFields.subject_id: "21M.030",
92+
CourseFields.title: "World Music",
9393
CourseFields.total_units: 12,
9494
CourseFields.offered_fall: True,
9595
CourseFields.offered_IAP: False,
9696
CourseFields.offered_spring: True,
9797
CourseFields.offered_summer: False,
9898
CourseFields.public: True,
99-
CourseFields.level: u"U",
100-
CourseFields.joint_subjects: [u"21M.830", u"21M.290"],
101-
CourseFields.equivalent_subjects: [u"21M.031"],
102-
CourseFields.quarter_information: u"1,march 1",
103-
CourseFields.not_offered_year: u"2019-2020",
104-
CourseFields.instructors: [u"E. Zimmer", u"C. Smith"],
105-
CourseFields.communication_requirement: u"CI-H",
106-
CourseFields.hass_attribute: u"HASS-A",
107-
CourseFields.gir_attribute: u"REST",
99+
CourseFields.level: "U",
100+
CourseFields.joint_subjects: ["21M.830", "21M.290"],
101+
CourseFields.equivalent_subjects: ["21M.031"],
102+
CourseFields.quarter_information: "1,march 1",
103+
CourseFields.not_offered_year: "2019-2020",
104+
CourseFields.instructors: ["E. Zimmer", "C. Smith"],
105+
CourseFields.communication_requirement: "CI-H",
106+
CourseFields.hass_attribute: "HASS-A",
107+
CourseFields.gir_attribute: "REST",
108108
}
109109
self.maxDiff = None
110110
self.assertDictEqual(expected, result)
@@ -113,23 +113,23 @@ def test_json_object_full(self):
113113
course = Course.objects.get(subject_id="21M.030")
114114
result = course.to_json_object()
115115
expected = {
116-
CourseFields.subject_id: u"21M.030",
117-
CourseFields.title: u"World Music",
116+
CourseFields.subject_id: "21M.030",
117+
CourseFields.title: "World Music",
118118
CourseFields.total_units: 12,
119119
CourseFields.offered_fall: True,
120120
CourseFields.offered_IAP: False,
121121
CourseFields.offered_spring: True,
122122
CourseFields.offered_summer: False,
123123
CourseFields.public: True,
124-
CourseFields.level: u"U",
125-
CourseFields.joint_subjects: [u"21M.830", u"21M.290"],
126-
CourseFields.equivalent_subjects: [u"21M.031"],
127-
CourseFields.quarter_information: u"1,march 1",
128-
CourseFields.not_offered_year: u"2019-2020",
129-
CourseFields.instructors: [u"E. Zimmer", u"C. Smith"],
130-
CourseFields.communication_requirement: u"CI-H",
131-
CourseFields.hass_attribute: u"HASS-A",
132-
CourseFields.gir_attribute: u"REST",
124+
CourseFields.level: "U",
125+
CourseFields.joint_subjects: ["21M.830", "21M.290"],
126+
CourseFields.equivalent_subjects: ["21M.031"],
127+
CourseFields.quarter_information: "1,march 1",
128+
CourseFields.not_offered_year: "2019-2020",
129+
CourseFields.instructors: ["E. Zimmer", "C. Smith"],
130+
CourseFields.communication_requirement: "CI-H",
131+
CourseFields.hass_attribute: "HASS-A",
132+
CourseFields.gir_attribute: "REST",
133133
CourseFields.lecture_units: 5,
134134
CourseFields.lab_units: 0,
135135
CourseFields.design_units: 0,
@@ -138,11 +138,11 @@ def test_json_object_full(self):
138138
CourseFields.is_half_class: False,
139139
CourseFields.pdf_option: False,
140140
CourseFields.has_final: False,
141-
CourseFields.description: u"Test description of 21M.030",
142-
CourseFields.prerequisites: u"21M.051/''permission of instructor''",
143-
CourseFields.schedule: u"Lecture,4-364/MW/0/9.30-11,4-364/MW/0/11-12.30",
144-
CourseFields.url: u"http://student.mit.edu/catalog/m21Ma.html#21M.030",
145-
CourseFields.related_subjects: [u"21M.011", u"21M.031"],
141+
CourseFields.description: "Test description of 21M.030",
142+
CourseFields.prerequisites: "21M.051/''permission of instructor''",
143+
CourseFields.schedule: "Lecture,4-364/MW/0/9.30-11,4-364/MW/0/11-12.30",
144+
CourseFields.url: "http://student.mit.edu/catalog/m21Ma.html#21M.030",
145+
CourseFields.related_subjects: ["21M.011", "21M.031"],
146146
CourseFields.rating: 5.0,
147147
CourseFields.enrollment_number: 45.0,
148148
CourseFields.in_class_hours: 3.0,
@@ -194,8 +194,8 @@ def test_lookup_subject(self):
194194
response = views.lookup(request, subject_id="2.001")
195195
self.assertEqual(200, response.status_code)
196196
self.assertDictContainsSubset({
197-
CourseFields.subject_id: u"2.001",
198-
CourseFields.title: u"Foo"
197+
CourseFields.subject_id: "2.001",
198+
CourseFields.title: "Foo"
199199
}, json.loads(response.content))
200200

201201
def test_lookup_subject_not_existing(self):

catalog_parse/catalog_parser.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131

3232
# For type checking str or unicode in Python 2 and 3
3333
try:
34-
basestring
34+
str
3535
except NameError:
36-
basestring = str
36+
str = str
3737

3838
COURSE_NUMBERS = [
3939
"1", "2", "3", "4",
@@ -83,7 +83,7 @@ def load_course_elements(url):
8383
course_ids = []
8484
courses = []
8585
for element in course_elements[0].getchildren():
86-
if element.tag == "a" and "name" in element.keys():
86+
if element.tag == "a" and "name" in list(element.keys()):
8787
subject_id = element.get("name")
8888
course_ids.append(subject_id)
8989
courses.append([])
@@ -119,10 +119,10 @@ def recursively_extract_info(node):
119119
contents = get_inner_html(node)
120120

121121
if node.tag == "img":
122-
if "title" in node.keys() and len(node.get("title")):
122+
if "title" in list(node.keys()) and len(node.get("title")):
123123
info_items.append(node.get("title"))
124124
elif node.tag == "a":
125-
if "name" in node.keys():
125+
if "name" in list(node.keys()):
126126
return (info_items, True)
127127
text = node.text_content().strip()
128128
if len(text):
@@ -381,7 +381,7 @@ def merge_duplicates(courses):
381381

382382
if len(course_dict[subject_id]) > 1:
383383
total_course = {}
384-
keys = set().union(*(other.keys() for other in course_dict[subject_id]))
384+
keys = set().union(*(list(other.keys()) for other in course_dict[subject_id]))
385385
for key in keys:
386386
vals = [other.get(key, '') for other in course_dict[subject_id]]
387387

@@ -442,7 +442,7 @@ def courses_from_dept_code(dept_code, **options):
442442
schedules = {other_id: schedules[""] for other_id in subject_ids}
443443

444444
for other_id in subject_ids:
445-
copied_course = {key: val for key, val in attribs.items()}
445+
copied_course = {key: val for key, val in list(attribs.items())}
446446
copied_course[CourseAttribute.subjectID] = other_id
447447
if other_id in schedules:
448448
copied_course[CourseAttribute.schedule] = schedules[other_id]
@@ -471,7 +471,7 @@ def writing_description_for_attribute(course, attribute):
471471
return ""
472472

473473
item = course[attribute]
474-
if isinstance(item, basestring):
474+
if isinstance(item, str):
475475
return '"' + item.replace('"', "'").replace('\n', '\\n') + '"'
476476
elif isinstance(item, bool):
477477
return "Y" if item == True else "N"
@@ -484,7 +484,7 @@ def writing_description_for_attribute(course, attribute):
484484
elif isinstance(item, list):
485485
return '"' + ",".join(item) + '"'
486486
else:
487-
print("Don't have a way to represent attribute {}: {} ({})".format(attribute, item, type(item)))
487+
print(("Don't have a way to represent attribute {}: {} ({})".format(attribute, item, type(item))))
488488
return str(item)
489489

490490
def write_courses(courses, filepath, attributes):
@@ -546,7 +546,7 @@ def parse(output_dir, evaluations_path=None, equivalences_path=None, write_relat
546546
if len(addl_courses) == 0:
547547
continue
548548

549-
print("======", total_code)
549+
print(("======", total_code))
550550
dept_courses += addl_courses
551551
if original_html is None:
552552
original_html = LAST_PAGE_HTML

catalog_parse/consensus_catalog.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def make_corrections(corrections, consensus):
3333
if correction[col]:
3434
if col not in consensus.columns:
3535
consensus[col] = ""
36-
print("Correction for {}: {} ==> {}".format(idx, col, correction[col]))
36+
print(("Correction for {}: {} ==> {}".format(idx, col, correction[col])))
3737
consensus.ix[idx][col] = correction[col]
3838

3939
elif subject_id in consensus.index:
@@ -42,12 +42,12 @@ def make_corrections(corrections, consensus):
4242
for col in correction:
4343
if col == "Subject Id": continue
4444
if correction[col]:
45-
print("Correction for {}: {} ==> {}".format(subject_id, col, correction[col]))
45+
print(("Correction for {}: {} ==> {}".format(subject_id, col, correction[col])))
4646
consensus_row[col] = correction[col]
4747

4848
else:
4949
# Add the subject
50-
print("Correction: adding subject {}".format(subject_id))
50+
print(("Correction: adding subject {}".format(subject_id)))
5151
consensus.loc[subject_id] = {col: correction.get(col, None) for col in consensus.columns}
5252

5353

@@ -63,7 +63,7 @@ def build_consensus(base_path, out_path, corrections=None):
6363
semester_data[semester] = all_courses
6464

6565
# Sort in reverse chronological order
66-
semester_data = sorted(semester_data.items(), key=lambda x: semester_sort_key(x[0]), reverse=True)
66+
semester_data = sorted(list(semester_data.items()), key=lambda x: semester_sort_key(x[0]), reverse=True)
6767
if len(semester_data) == 0:
6868
print("No raw semester data found.")
6969
return
@@ -90,7 +90,7 @@ def build_consensus(base_path, out_path, corrections=None):
9090
consensus = pd.concat([consensus, data], sort=False)
9191

9292
consensus = consensus.drop_duplicates(subset=[CourseAttribute.subjectID], keep='first')
93-
print("Added {} courses with {}.".format(len(consensus) - last_size, semester))
93+
print(("Added {} courses with {}.".format(len(consensus) - last_size, semester)))
9494
last_size = len(consensus)
9595

9696
consensus.set_index(CourseAttribute.subjectID, inplace=True)
@@ -141,7 +141,7 @@ def write_df(df, path):
141141
out_path = sys.argv[2]
142142

143143
if os.path.exists(out_path):
144-
print("Fatal: the directory {} already exists. Please delete it or choose a different location.".format(out_path))
144+
print(("Fatal: the directory {} already exists. Please delete it or choose a different location.".format(out_path)))
145145
exit(1)
146146

147147
build_consensus(in_path, out_path)

catalog_parse/delta_gen.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def write_delta_file(semester_name, delta, outpath):
4040
file.write(str(version_num) + "\n")
4141
file.write("\n".join(delta))
4242

43-
print("Delta file written to {}.".format(delta_file_path))
43+
print(("Delta file written to {}.".format(delta_file_path)))
4444

4545
def delta_file_name(path):
4646
if ".txt" in path:
@@ -96,8 +96,8 @@ def commit_delta(new_directory, old_directory, server_path, delta):
9696
delta = make_delta(new_directory, old_directory)
9797
for file in delta:
9898
print(file)
99-
if raw_input("Ready to write files?") in ['y', 'yes', '\n']:
99+
if eval(input("Ready to write files?")) in ['y', 'yes', '\n']:
100100
commit_delta(new_directory, old_directory, server_path, delta)
101-
print("Old files moved to {}. New files moved to {}.".format(os.path.join(os.path.dirname(old_directory), old_name + "-old"), old_directory))
101+
print(("Old files moved to {}. New files moved to {}.".format(os.path.join(os.path.dirname(old_directory), old_name + "-old"), old_directory)))
102102
else:
103103
print("Aborting.")

0 commit comments

Comments
 (0)