Skip to content

Commit

Permalink
Merge pull request google#461 from jarondl/py3_misc
Browse files Browse the repository at this point in the history
More Python3/2 compatability changes
  • Loading branch information
stotala committed Feb 10, 2019
2 parents e5e4751 + c6cbd7d commit 1c7f8c0
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 16 deletions.
5 changes: 3 additions & 2 deletions extensions/googletransit/pybcp47/bcp47languageparser.py
Expand Up @@ -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. """
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion extensions/googletransit/pybcp47/testpybcp47.py
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions misc/import_ch_zurich.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion misc/sql_loop.py
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/testexamples.py
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions tests/transitfeed/testschedule.py
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions transitfeed/loader.py
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions transitfeed/problems.py
Expand Up @@ -16,6 +16,7 @@

from __future__ import print_function
from __future__ import absolute_import
from functools import reduce
import logging
import time

Expand Down
2 changes: 1 addition & 1 deletion transitfeed/schedule.py
Expand Up @@ -615,7 +615,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())
Expand Down
2 changes: 1 addition & 1 deletion transitfeed/trip.py
Expand Up @@ -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):
Expand Down
6 changes: 3 additions & 3 deletions transitfeed/util.py
Expand Up @@ -462,7 +462,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 errors.Error, 'Bad HH:MM:SS "%s"' % time_string
raise errors.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):
Expand Down Expand Up @@ -610,7 +610,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
Expand All @@ -628,7 +628,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
Expand Down

0 comments on commit 1c7f8c0

Please sign in to comment.