Skip to content

Commit c6cbd7d

Browse files
committed
More Python3/2 compatability changes
Part of google#412 Started with `futurize -nw --stage1 .`, but had to do a few manual changes.
1 parent 6c69936 commit c6cbd7d

File tree

11 files changed

+18
-16
lines changed

11 files changed

+18
-16
lines changed

extensions/googletransit/pybcp47/bcp47languageparser.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pkg_resources import resource_string
2020
import re
2121
import string
22+
from functools import reduce
2223

2324
class FileParseError(Exception):
2425
"""Exception raised for errors in the subtag registry file. """
@@ -74,11 +75,11 @@ def _ReadLanguageSubtagRegistryFile(self):
7475
# Load the entries from the registry file in this package.
7576
line_iterator = self._GetLinesFromLanguageSubtagRegistryFile()
7677
# Read the header lines with the File-Date record.
77-
first_line, line_number = line_iterator.next()
78+
first_line, line_number = next(line_iterator)
7879
if not first_line[:11] == 'File-Date: ':
7980
raise FileParseError(line_number,
8081
"Invalid first line '%s'! Must be a File-Date record." % (first_line))
81-
second_line, line_number = line_iterator.next()
82+
second_line, line_number = next(line_iterator)
8283
if not second_line == '%%':
8384
raise FileParseError(line_number,
8485
"Invalid first record '%s'! Must start with '%%%%'." % (second_line))

extensions/googletransit/pybcp47/testpybcp47.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _CheckTagsInFile(self, filename, should_be_wellformed, should_be_valid):
7878
full_filename = os.path.join(os.path.dirname(__file__), "testdata",
7979
filename)
8080
fileObj = codecs.open(full_filename, "r", "utf-8" )
81-
for line in fileObj.xreadlines():
81+
for line in fileObj:
8282
line_parts = line.split("#")
8383
tag = line_parts[0].strip()
8484
if tag:

misc/import_ch_zurich.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
def ReadCSV(s, cols):
7979
csv_dialect = csv.Sniffer().sniff(s[0])
8080
reader = csv.reader(s, csv_dialect)
81-
header = reader.next()
81+
header = next(reader)
8282
col_index = [-1] * len(cols)
8383
for i in range(len(cols)):
8484
if cols[i] in header:
@@ -241,7 +241,7 @@ def ImportRoutes(self, s):
241241
route.name = name
242242
route.color = "FFFFFF"
243243
route.color_text = "000000"
244-
if TRAM_LINES.has_key(name):
244+
if name in TRAM_LINES:
245245
route.type = TYPE_TRAM
246246
route.color = TRAM_LINES[name][0]
247247
route.color_text = TRAM_LINES[name][1]

misc/sql_loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def LoadNamedFile(file_name, conn):
8181
def LoadFile(f, table_name, conn):
8282
"""Import lines from f as new table in db with cursor c."""
8383
reader = csv.reader(f)
84-
header = reader.next()
84+
header = next(reader)
8585

8686
columns = []
8787
for n in header:

tests/testexamples.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def runTest(self):
2020
if not m:
2121
raise Exception("Failed to find source code on wiki page")
2222
wiki_code = m.group(1)
23-
exec wiki_code
23+
exec(wiki_code)
2424

2525

2626
class shuttle_from_xmlfeed(util.TempDirTestCaseBase):

tests/transitfeed/testschedule.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ def testNoServiceGapBeforeTodayIfTodayHasService(self):
921921
# If the feed starts today NO previous service gap should be found
922922
# even if today does not have service
923923
def testNoServiceGapBeforeTodayIfTheFeedStartsToday(self):
924-
self.schedule.Validate(today=date(2009, 06, 01),
924+
self.schedule.Validate(today=date(2009, 6, 1),
925925
service_gap_interval=13)
926926

927927
# This service gap is the one between FULLW and WE
@@ -936,7 +936,7 @@ def testNoServiceGapBeforeTodayIfTheFeedStartsToday(self):
936936

937937
# If there is a gap at the end of the one-year period we should find it
938938
def testGapAtTheEndOfTheOneYearPeriodIsDiscovered(self):
939-
self.schedule.Validate(today=date(2009, 06, 22),
939+
self.schedule.Validate(today=date(2009, 6, 22),
940940
service_gap_interval=13)
941941

942942
# This service gap is the one between FULLW and WE

transitfeed/loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def _ReadCsvDict(self, file_name, cols, required, deprecated):
161161
# integer and id fields; they will be validated at higher levels.
162162
reader = csv.reader(eol_checker, skipinitialspace=True)
163163

164-
raw_header = reader.next()
164+
raw_header = next(reader)
165165
header_occurrences = util.defaultdict(lambda: 0)
166166
header = []
167167
valid_columns = [] # Index into raw_header and raw_row
@@ -291,7 +291,7 @@ def _ReadCSV(self, file_name, cols, required, deprecated):
291291
file_name, self._problems)
292292
reader = csv.reader(eol_checker) # Use excel dialect
293293

294-
header = reader.next()
294+
header = next(reader)
295295
header = map(lambda x: x.strip(), header) # trim any whitespace
296296
header_occurrences = util.defaultdict(lambda: 0)
297297
for column_header in header:

transitfeed/problems.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from __future__ import print_function
1818
from __future__ import absolute_import
19+
from functools import reduce
1920
import logging
2021
import time
2122

transitfeed/schedule.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def _WriteArchiveString(self, archive, filename, stringio):
615615
zi = zipfile.ZipInfo(filename)
616616
# See
617617
# http://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zipf
618-
zi.external_attr = 0666 << 16L # Set unix permissions to -rw-rw-rw
618+
zi.external_attr = 0o666 << 16 # Set unix permissions to -rw-rw-rw
619619
# ZIP_DEFLATED requires zlib. zlib comes with Python 2.4 and 2.5
620620
zi.compress_type = zipfile.ZIP_DEFLATED
621621
archive.writestr(zi, stringio.getvalue())

transitfeed/trip.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def ReplaceStopTimeObject(self, stoptime, schedule=None):
104104
"stop_sequence=? and stop_id=?",
105105
(self.trip_id, stoptime.stop_sequence, stoptime.stop_id))
106106
if cursor.rowcount == 0:
107-
raise problems_module.Error, 'Attempted replacement of StopTime object which does not exist'
107+
raise problems_module.Error('Attempted replacement of StopTime object which does not exist')
108108
self._AddStopTimeObjectUnordered(stoptime, schedule)
109109

110110
def AddStopTimeObject(self, stoptime, schedule=None, problems=None):

transitfeed/util.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def TimeToSecondsSinceMidnight(time_string):
462462
m = re.match(r'(\d{1,3}):([0-5]\d):([0-5]\d)$', time_string)
463463
# ignored: matching for leap seconds
464464
if not m:
465-
raise errors.Error, 'Bad HH:MM:SS "%s"' % time_string
465+
raise errors.Error('Bad HH:MM:SS "%s"' % time_string)
466466
return int(m.group(1)) * 3600 + int(m.group(2)) * 60 + int(m.group(3))
467467

468468
def FormatSecondsSinceMidnight(s):
@@ -610,7 +610,7 @@ def __iter__(self):
610610
def next(self):
611611
"""Return next line without end of line marker or raise StopIteration."""
612612
try:
613-
next_line = self._f.next()
613+
next_line = next(self._f)
614614
except StopIteration:
615615
self._FinalCheck()
616616
raise
@@ -628,7 +628,7 @@ def next(self):
628628
elif m_eol.group() == "":
629629
# Should only happen at the end of the file
630630
try:
631-
self._f.next()
631+
next(self._f)
632632
raise RuntimeError("Unexpected row without new line sequence")
633633
except StopIteration:
634634
# Will be raised again when EndOfLineChecker.next() is next called

0 commit comments

Comments
 (0)