From 01d1b18a4980699a9f117697d563897636023bf1 Mon Sep 17 00:00:00 2001 From: Yaron de Leeuw Date: Sun, 27 Jan 2019 20:10:54 -0500 Subject: [PATCH] More Python3/2 compatability changes Part of #412 Started with `futurize -nw --stage1 .`, but had to do a few manual changes. --- extensions/googletransit/pybcp47/bcp47languageparser.py | 5 +++-- extensions/googletransit/pybcp47/testpybcp47.py | 2 +- misc/import_ch_zurich.py | 4 ++-- misc/sql_loop.py | 2 +- tests/testexamples.py | 2 +- tests/transitfeed/testschedule.py | 4 ++-- transitfeed/loader.py | 4 ++-- transitfeed/problems.py | 1 + transitfeed/schedule.py | 2 +- transitfeed/trip.py | 2 +- transitfeed/util.py | 6 +++--- 11 files changed, 18 insertions(+), 16 deletions(-) diff --git a/extensions/googletransit/pybcp47/bcp47languageparser.py b/extensions/googletransit/pybcp47/bcp47languageparser.py index 1f175539..289dff21 100644 --- a/extensions/googletransit/pybcp47/bcp47languageparser.py +++ b/extensions/googletransit/pybcp47/bcp47languageparser.py @@ -19,6 +19,7 @@ from pkg_resources import resource_string import re import string +from functools import reduce class FileParseError(Exception): """Exception raised for errors in the subtag registry file. """ @@ -74,11 +75,11 @@ def _ReadLanguageSubtagRegistryFile(self): # Load the entries from the registry file in this package. line_iterator = self._GetLinesFromLanguageSubtagRegistryFile() # Read the header lines with the File-Date record. - first_line, line_number = line_iterator.next() + first_line, line_number = next(line_iterator) if not first_line[:11] == 'File-Date: ': raise FileParseError(line_number, "Invalid first line '%s'! Must be a File-Date record." % (first_line)) - second_line, line_number = line_iterator.next() + second_line, line_number = next(line_iterator) if not second_line == '%%': raise FileParseError(line_number, "Invalid first record '%s'! Must start with '%%%%'." % (second_line)) diff --git a/extensions/googletransit/pybcp47/testpybcp47.py b/extensions/googletransit/pybcp47/testpybcp47.py index 9ad748bb..4980e2a9 100644 --- a/extensions/googletransit/pybcp47/testpybcp47.py +++ b/extensions/googletransit/pybcp47/testpybcp47.py @@ -78,7 +78,7 @@ def _CheckTagsInFile(self, filename, should_be_wellformed, should_be_valid): full_filename = os.path.join(os.path.dirname(__file__), "testdata", filename) fileObj = codecs.open(full_filename, "r", "utf-8" ) - for line in fileObj.xreadlines(): + for line in fileObj: line_parts = line.split("#") tag = line_parts[0].strip() if tag: diff --git a/misc/import_ch_zurich.py b/misc/import_ch_zurich.py index 5bb5352d..81b2e6e6 100755 --- a/misc/import_ch_zurich.py +++ b/misc/import_ch_zurich.py @@ -78,7 +78,7 @@ def ReadCSV(s, cols): csv_dialect = csv.Sniffer().sniff(s[0]) reader = csv.reader(s, csv_dialect) - header = reader.next() + header = next(reader) col_index = [-1] * len(cols) for i in range(len(cols)): if cols[i] in header: @@ -241,7 +241,7 @@ def ImportRoutes(self, s): route.name = name route.color = "FFFFFF" route.color_text = "000000" - if TRAM_LINES.has_key(name): + if name in TRAM_LINES: route.type = TYPE_TRAM route.color = TRAM_LINES[name][0] route.color_text = TRAM_LINES[name][1] diff --git a/misc/sql_loop.py b/misc/sql_loop.py index 0fb76c64..297105ac 100755 --- a/misc/sql_loop.py +++ b/misc/sql_loop.py @@ -81,7 +81,7 @@ def LoadNamedFile(file_name, conn): def LoadFile(f, table_name, conn): """Import lines from f as new table in db with cursor c.""" reader = csv.reader(f) - header = reader.next() + header = next(reader) columns = [] for n in header: diff --git a/tests/testexamples.py b/tests/testexamples.py index 4e83a134..73c80e06 100644 --- a/tests/testexamples.py +++ b/tests/testexamples.py @@ -20,7 +20,7 @@ def runTest(self): if not m: raise Exception("Failed to find source code on wiki page") wiki_code = m.group(1) - exec wiki_code + exec(wiki_code) class shuttle_from_xmlfeed(util.TempDirTestCaseBase): diff --git a/tests/transitfeed/testschedule.py b/tests/transitfeed/testschedule.py index 0b9fb90a..53478a43 100644 --- a/tests/transitfeed/testschedule.py +++ b/tests/transitfeed/testschedule.py @@ -921,7 +921,7 @@ def testNoServiceGapBeforeTodayIfTodayHasService(self): # If the feed starts today NO previous service gap should be found # even if today does not have service def testNoServiceGapBeforeTodayIfTheFeedStartsToday(self): - self.schedule.Validate(today=date(2009, 06, 01), + self.schedule.Validate(today=date(2009, 6, 1), service_gap_interval=13) # This service gap is the one between FULLW and WE @@ -936,7 +936,7 @@ def testNoServiceGapBeforeTodayIfTheFeedStartsToday(self): # If there is a gap at the end of the one-year period we should find it def testGapAtTheEndOfTheOneYearPeriodIsDiscovered(self): - self.schedule.Validate(today=date(2009, 06, 22), + self.schedule.Validate(today=date(2009, 6, 22), service_gap_interval=13) # This service gap is the one between FULLW and WE diff --git a/transitfeed/loader.py b/transitfeed/loader.py index 1719f3bc..15722bd9 100755 --- a/transitfeed/loader.py +++ b/transitfeed/loader.py @@ -161,7 +161,7 @@ def _ReadCsvDict(self, file_name, cols, required, deprecated): # integer and id fields; they will be validated at higher levels. reader = csv.reader(eol_checker, skipinitialspace=True) - raw_header = reader.next() + raw_header = next(reader) header_occurrences = util.defaultdict(lambda: 0) header = [] valid_columns = [] # Index into raw_header and raw_row @@ -291,7 +291,7 @@ def _ReadCSV(self, file_name, cols, required, deprecated): file_name, self._problems) reader = csv.reader(eol_checker) # Use excel dialect - header = reader.next() + header = next(reader) header = map(lambda x: x.strip(), header) # trim any whitespace header_occurrences = util.defaultdict(lambda: 0) for column_header in header: diff --git a/transitfeed/problems.py b/transitfeed/problems.py index 985ec5a3..05aa8118 100755 --- a/transitfeed/problems.py +++ b/transitfeed/problems.py @@ -20,6 +20,7 @@ import time from . import util +from functools import reduce # Problem types: # Error: A data issue not allowed by the GTFS spec. diff --git a/transitfeed/schedule.py b/transitfeed/schedule.py index 9fa6057f..df48546b 100755 --- a/transitfeed/schedule.py +++ b/transitfeed/schedule.py @@ -613,7 +613,7 @@ def _WriteArchiveString(self, archive, filename, stringio): zi = zipfile.ZipInfo(filename) # See # http://stackoverflow.com/questions/434641/how-do-i-set-permissions-attributes-on-a-file-in-a-zip-file-using-pythons-zipf - zi.external_attr = 0666 << 16L # Set unix permissions to -rw-rw-rw + zi.external_attr = 0o666 << 16 # Set unix permissions to -rw-rw-rw # ZIP_DEFLATED requires zlib. zlib comes with Python 2.4 and 2.5 zi.compress_type = zipfile.ZIP_DEFLATED archive.writestr(zi, stringio.getvalue()) diff --git a/transitfeed/trip.py b/transitfeed/trip.py index 18fd6822..77957748 100755 --- a/transitfeed/trip.py +++ b/transitfeed/trip.py @@ -104,7 +104,7 @@ def ReplaceStopTimeObject(self, stoptime, schedule=None): "stop_sequence=? and stop_id=?", (self.trip_id, stoptime.stop_sequence, stoptime.stop_id)) if cursor.rowcount == 0: - raise problems_module.Error, 'Attempted replacement of StopTime object which does not exist' + raise problems_module.Error('Attempted replacement of StopTime object which does not exist') self._AddStopTimeObjectUnordered(stoptime, schedule) def AddStopTimeObject(self, stoptime, schedule=None, problems=None): diff --git a/transitfeed/util.py b/transitfeed/util.py index 7ca6564b..542a00ab 100644 --- a/transitfeed/util.py +++ b/transitfeed/util.py @@ -463,7 +463,7 @@ def TimeToSecondsSinceMidnight(time_string): m = re.match(r'(\d{1,3}):([0-5]\d):([0-5]\d)$', time_string) # ignored: matching for leap seconds if not m: - raise problems_module.Error, 'Bad HH:MM:SS "%s"' % time_string + raise problems_module.Error('Bad HH:MM:SS "%s"' % time_string) return int(m.group(1)) * 3600 + int(m.group(2)) * 60 + int(m.group(3)) def FormatSecondsSinceMidnight(s): @@ -611,7 +611,7 @@ def __iter__(self): def next(self): """Return next line without end of line marker or raise StopIteration.""" try: - next_line = self._f.next() + next_line = next(self._f) except StopIteration: self._FinalCheck() raise @@ -629,7 +629,7 @@ def next(self): elif m_eol.group() == "": # Should only happen at the end of the file try: - self._f.next() + next(self._f) raise RuntimeError("Unexpected row without new line sequence") except StopIteration: # Will be raised again when EndOfLineChecker.next() is next called