Skip to content
This repository has been archived by the owner on Nov 30, 2023. It is now read-only.

Use recommended comparison style with 'None' #505

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/transitfeed/testtransitfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ def testDeprecatedFieldDefaultsToNoneIfNotProvided(self):
schedule = self.MakeLoaderAndLoad(self.problems,
gtfs_factory=self.gtfs_factory)
agency = schedule._agencies.values()[0]
self.assertTrue(agency.agency_url == None)
self.assertTrue(agency.agency_url is None)
# stop.txt from util.MemoryZipTestCase does not have 'stop_desc', accessing
# the variable stop_desc should default to None instead of raising an
# AttributeError
stop = schedule.stops.values()[0]
self.assertTrue(stop.stop_desc == None)
self.assertTrue(stop.stop_desc is None)
self.accumulator.AssertNoMoreExceptions()


Expand Down
12 changes: 6 additions & 6 deletions transitfeed/fareattribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ def __init__(self,
self.payment_method = int(self.payment_method)
except (TypeError, ValueError):
pass
if self.transfers == None or self.transfers == "":
if self.transfers is None or self.transfers == "":
self.transfers = None
else:
try:
self.transfers = int(self.transfers)
except (TypeError, ValueError):
pass
if self.transfer_duration == None or self.transfer_duration == "":
if self.transfer_duration is None or self.transfer_duration == "":
self.transfer_duration = None
else:
try:
Expand Down Expand Up @@ -105,7 +105,7 @@ def ValidateFareId(self, problems):
problems.MissingValue("fare_id")

def ValidatePrice(self, problems):
if self.price == None:
if self.price is None:
problems.MissingValue("price")
elif not isinstance(self.price, float) and not isinstance(self.price, int):
problems.InvalidValue("price", self.price)
Expand All @@ -119,20 +119,20 @@ def ValidateCurrencyType(self, problems):
problems.InvalidValue("currency_type", self.currency_type)

def ValidatePaymentMethod(self, problems):
if self.payment_method == "" or self.payment_method == None:
if self.payment_method == "" or self.payment_method is None:
problems.MissingValue("payment_method")
elif (not isinstance(self.payment_method, int) or
self.payment_method not in range(0, 2)):
problems.InvalidValue("payment_method", self.payment_method)

def ValidateTransfers(self, problems):
if not ((self.transfers == None) or
if not ((self.transfers is None) or
(isinstance(self.transfers, int) and
self.transfers in range(0, 3))):
problems.InvalidValue("transfers", self.transfers)

def ValidateTransferDuration(self, problems):
if ((self.transfer_duration != None) and
if ((self.transfer_duration is not None) and
not isinstance(self.transfer_duration, int)):
problems.InvalidValue("transfer_duration", self.transfer_duration)
if self.transfer_duration and (self.transfer_duration < 0):
Expand Down
2 changes: 1 addition & 1 deletion transitfeed/problems.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def ContextTupleToDict(context):
if not context:
return d
for k, v in zip(ExceptionWithContext.CONTEXT_PARTS, context):
if v != '' and v != None: # Don't ignore int(0), a valid row_num
if v != '' and v is not None: # Don't ignore int(0), a valid row_num
d[k] = v
return d

Expand Down
2 changes: 1 addition & 1 deletion transitfeed/serviceperiod.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def SetDateHasService(self, date, has_service=True, problems=None):
(self.service_id, date),
type=problems_module.TYPE_WARNING)
exception_context_tuple = (has_service and self._EXCEPTION_TYPE_ADD or
self._EXCEPTION_TYPE_REMOVE, problems != None and
self._EXCEPTION_TYPE_REMOVE, problems is not None and
problems.GetFileContext() or None)
self.date_exceptions[date] = exception_context_tuple

Expand Down
26 changes: 13 additions & 13 deletions transitfeed/stoptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def __init__(self, problems, stop,
#
# For more details see the discussion at
# http://codereview.appspot.com/1713041
if stop_time != None:
if stop_time is not None:
arrival_time = departure_time = stop_time

if arrival_secs != None:
if arrival_secs is not None:
self.arrival_secs = arrival_secs
elif arrival_time in (None, ""):
self.arrival_secs = None # Untimed
Expand All @@ -90,7 +90,7 @@ def __init__(self, problems, stop,
problems.InvalidValue('arrival_time', arrival_time)
self.arrival_secs = None

if departure_secs != None:
if departure_secs is not None:
self.departure_secs = departure_secs
elif departure_time in (None, ""):
self.departure_secs = None
Expand All @@ -116,15 +116,15 @@ def __init__(self, problems, stop,
drop_off_type, [0, 1, 2, 3], None, True, 'drop_off_type', problems)

if (self.pickup_type == 1 and self.drop_off_type == 1 and
self.arrival_secs == None and self.departure_secs == None):
self.arrival_secs is None and self.departure_secs is None):
problems.OtherProblem('This stop time has a pickup_type and '
'drop_off_type of 1, indicating that riders '
'can\'t get on or off here. Since it doesn\'t '
'define a timepoint either, this entry serves no '
'purpose and should be excluded from the trip.',
type=problems_module.TYPE_WARNING)

if ((self.arrival_secs != None) and (self.departure_secs != None) and
if ((self.arrival_secs is not None) and (self.departure_secs is not None) and
(self.departure_secs < self.arrival_secs)):
problems.InvalidValue('departure_time', departure_time,
'The departure time at this stop (%s) is before '
Expand All @@ -134,8 +134,8 @@ def __init__(self, problems, stop,

# If the caller passed a valid arrival time but didn't attempt to pass a
# departure time complain
if (self.arrival_secs != None and
self.departure_secs == None and departure_time == None):
if (self.arrival_secs is not None and
self.departure_secs is None and departure_time is None):
# self.departure_secs might be None because departure_time was invalid,
# so we need to check both
problems.MissingValue('departure_time',
Expand All @@ -144,8 +144,8 @@ def __init__(self, problems, stop,
'It\'s OK to set them both to the same value.')
# If the caller passed a valid departure time but didn't attempt to pass a
# arrival time complain
if (self.departure_secs != None and
self.arrival_secs == None and arrival_time == None):
if (self.departure_secs is not None and
self.arrival_secs is None and arrival_time is None):
problems.MissingValue('arrival_time',
'arrival_time and departure_time should either '
'both be provided or both be left blank. '
Expand Down Expand Up @@ -203,9 +203,9 @@ def GetSqlValuesTuple(self, trip_id):
def GetTimeSecs(self):
"""Return the first of arrival_secs and departure_secs that is not None.
If both are None return None."""
if self.arrival_secs != None:
if self.arrival_secs is not None:
return self.arrival_secs
elif self.departure_secs != None:
elif self.departure_secs is not None:
return self.departure_secs
else:
return None
Expand All @@ -214,10 +214,10 @@ def __getattr__(self, name):
if name == 'stop_id':
return self.stop.stop_id
elif name == 'arrival_time':
return (self.arrival_secs != None and
return (self.arrival_secs is not None and
util.FormatSecondsSinceMidnight(self.arrival_secs) or '')
elif name == 'departure_time':
return (self.departure_secs != None and
return (self.departure_secs is not None and
util.FormatSecondsSinceMidnight(self.departure_secs) or '')
elif name == 'shape_dist_traveled':
return ''
Expand Down
26 changes: 13 additions & 13 deletions transitfeed/trip.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ def AddStopTimeObject(self, stoptime, schedule=None, problems=None):
if row[0] is None:
# This is the first stop_time of the trip
stoptime.stop_sequence = 1
if new_secs == None:
if new_secs is None:
problems.OtherProblem(
'No time for first StopTime of trip_id "%s"' % (self.trip_id,))
else:
stoptime.stop_sequence = row[0] + 1
prev_secs = max(row[1], row[2])
if new_secs != None and new_secs < prev_secs:
if new_secs is not None and new_secs < prev_secs:
problems.OtherProblem(
'out of order stop time for stop_id=%s trip_id=%s %s < %s' %
(util.EncodeUnicode(stoptime.stop_id),
Expand Down Expand Up @@ -194,14 +194,14 @@ def GetTimeInterpolatedStops(self):
distance_traveled_between_timepoints = 0

for i, st in enumerate(stoptimes):
if st.GetTimeSecs() != None:
if st.GetTimeSecs() is not None:
cur_timepoint = st
distance_between_timepoints = 0
distance_traveled_between_timepoints = 0
if i + 1 < len(stoptimes):
k = i + 1
distance_between_timepoints += util.ApproximateDistanceBetweenStops(stoptimes[k-1].stop, stoptimes[k].stop)
while stoptimes[k].GetTimeSecs() == None:
while stoptimes[k].GetTimeSecs() is None:
k += 1
distance_between_timepoints += util.ApproximateDistanceBetweenStops(stoptimes[k-1].stop, stoptimes[k].stop)
next_timepoint = stoptimes[k]
Expand Down Expand Up @@ -281,9 +281,9 @@ def GetFrequencyStopTimes(self, problems=None):
# go through the pattern and generate stoptimes
for st in stoptime_pattern:
arrival_secs, departure_secs = None, None # default value if the stoptime is not timepoint
if st.arrival_secs != None:
if st.arrival_secs is not None:
arrival_secs = st.arrival_secs - first_secs + run_secs
if st.departure_secs != None:
if st.departure_secs is not None:
departure_secs = st.departure_secs - first_secs + run_secs
# append stoptime
stoptimes.append(stoptime_class(problems=problems, stop=st.stop,
Expand All @@ -308,9 +308,9 @@ def GetStartTime(self, problems=problems_module.default_problem_reporter):
'SELECT arrival_secs,departure_secs FROM stop_times WHERE '
'trip_id=? ORDER BY stop_sequence LIMIT 1', (self.trip_id,))
(arrival_secs, departure_secs) = cursor.fetchone()
if arrival_secs != None:
if arrival_secs is not None:
return arrival_secs
elif departure_secs != None:
elif departure_secs is not None:
return departure_secs
else:
problems.InvalidValue('departure_time', '',
Expand Down Expand Up @@ -350,9 +350,9 @@ def GetEndTime(self, problems=problems_module.default_problem_reporter):
'SELECT arrival_secs,departure_secs FROM stop_times WHERE '
'trip_id=? ORDER BY stop_sequence DESC LIMIT 1', (self.trip_id,))
(arrival_secs, departure_secs) = cursor.fetchone()
if departure_secs != None:
if departure_secs is not None:
return departure_secs
elif arrival_secs != None:
elif arrival_secs is not None:
return arrival_secs
else:
problems.InvalidValue('arrival_time', '',
Expand Down Expand Up @@ -420,7 +420,7 @@ def AddFrequency(self, start_time, end_time, headway_secs, exact_times=0,
Returns:
None
"""
if start_time == None or start_time == '': # 0 is OK
if start_time is None or start_time == '': # 0 is OK
problem_reporter.MissingValue('start_time')
return
if isinstance(start_time, basestring):
Expand All @@ -432,7 +432,7 @@ def AddFrequency(self, start_time, end_time, headway_secs, exact_times=0,
elif start_time < 0:
problem_reporter.InvalidValue('start_time', start_time)

if end_time == None or end_time == '':
if end_time is None or end_time == '':
problem_reporter.MissingValue('end_time')
return
if isinstance(end_time, basestring):
Expand Down Expand Up @@ -719,7 +719,7 @@ def ValidateAfterAdd(self, problems):
def _CheckSpeed(self, prev_stop, next_stop, depart_time,
arrive_time, max_speed, problems):
# Checks that the speed between two stops is not faster than max_speed
if prev_stop != None:
if prev_stop is not None:
try:
time_between_stops = arrive_time - depart_time
except TypeError:
Expand Down
4 changes: 2 additions & 2 deletions transitfeed/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def IsValidHexColor(color):
Checks the validity of a hex color value:
- the color string must consist of 6 hexadecimal digits
"""
return not re.match('^[0-9a-fA-F]{6}$', color) == None
return not re.match('^[0-9a-fA-F]{6}$', color) is None

def IsValidLanguageCode(lang):
"""
Expand Down Expand Up @@ -473,7 +473,7 @@ def FormatSecondsSinceMidnight(s):
def DateStringToDateObject(date_string):
"""Return a date object for a string "YYYYMMDD"."""
# If this becomes a bottleneck date objects could be cached
if re.match('^\d{8}$', date_string) == None:
if re.match('^\d{8}$', date_string) is None:
return None
try:
return datetime.date(int(date_string[0:4]), int(date_string[4:6]),
Expand Down