def AddFrequency(self, start_time, end_time, headway_secs, exact_times=0, problem_reporter=problems_module.default_problem_reporter): """Adds a period to this trip during which the vehicle travels at regular intervals (rather than specifying exact times for each stop). Args: start_time: The time at which this headway period starts, either in numerical seconds since midnight or as "HH:MM:SS" since midnight. end_time: The time at which this headway period ends, either in numerical seconds since midnight or as "HH:MM:SS" since midnight. This value should be larger than start_time. headway_secs: The amount of time, in seconds, between occurences of this trip. exact_times: If 1, indicates that frequency trips should be scheduled exactly as determined by their start time and headway. Default is 0. problem_reporter: Optional parameter that can be used to select how any errors in the other input parameters will be reported. Returns: None """ if start_time == None or start_time == '': # 0 is OK problem_reporter.MissingValue('start_time') return if isinstance(start_time, basestring): try: start_time = util.TimeToSecondsSinceMidnight(start_time) except problems_module.Error: problem_reporter.InvalidValue('start_time', start_time) return elif start_time < 0: problem_reporter.InvalidValue('start_time', start_time) if end_time == None or end_time == '': problem_reporter.MissingValue('end_time') return if isinstance(end_time, basestring): try: end_time = util.TimeToSecondsSinceMidnight(end_time) except problems_module.Error: problem_reporter.InvalidValue('end_time', end_time) return elif end_time < 0: problem_reporter.InvalidValue('end_time', end_time) return if not headway_secs: problem_reporter.MissingValue('headway_secs') return try: headway_secs = int(headway_secs) except ValueError: problem_reporter.InvalidValue('headway_secs', headway_secs) return if headway_secs <= 0: problem_reporter.InvalidValue('headway_secs', headway_secs) return if end_time <= start_time: problem_reporter.InvalidValue('end_time', end_time, 'should be greater than start_time') if not exact_times: exact_times = 0 if exact_times not in (0, 1): problem_reporter.InvalidValue('exact_times', exact_times, 'Should be 0 (no fixed schedule) or 1 (fixed and regular schedule)') self._headways.append((start_time, end_time, headway_secs, exact_times))
def __init__(self, problems, stop, arrival_time=None, departure_time=None, stop_headsign=None, pickup_type=None, drop_off_type=None, shape_dist_traveled=None, arrival_secs=None, departure_secs=None, stop_time=None, stop_sequence=None): self._schedule = None Persistable.__init__(self, self._schedule) # Implementation note from Andre, July 22, 2010: # The checks performed here should be in their own Validate* methods to # keep consistency. Unfortunately the performance degradation is too great, # so the validation was left in __init__. # Performance is also the reason why we don't use the GtfsFactory, but # have StopTime._STOP_CLASS instead. If a Stop class that does not inherit # from transitfeed.Stop is used, the extension should also provide a # StopTime class that updates _STOP_CLASS accordingly. # # For more details see the discussion at # http://codereview.appspot.com/1713041 if stop_time != None: arrival_time = departure_time = stop_time if arrival_secs != None: self.arrival_secs = arrival_secs elif arrival_time in (None, ""): self.arrival_secs = None # Untimed arrival_time = None else: try: self.arrival_secs = util.TimeToSecondsSinceMidnight( arrival_time) except problems_module.Error: problems.InvalidValue('arrival_time', arrival_time) self.arrival_secs = None if departure_secs != None: self.departure_secs = departure_secs elif departure_time in (None, ""): self.departure_secs = None departure_time = None else: try: self.departure_secs = util.TimeToSecondsSinceMidnight( departure_time) except problems_module.Error: problems.InvalidValue('departure_time', departure_time) self.departure_secs = None if not isinstance(stop, self._STOP_CLASS): # Not quite correct, but better than letting the problem propagate problems.InvalidValue('stop', stop) self.stop = stop self.stop_headsign = stop_headsign if pickup_type in (None, ""): self.pickup_type = None else: try: pickup_type = int(pickup_type) except ValueError: problems.InvalidValue('pickup_type', pickup_type) else: if pickup_type < 0 or pickup_type > 3: problems.InvalidValue('pickup_type', pickup_type) self.pickup_type = pickup_type if drop_off_type in (None, ""): self.drop_off_type = None else: try: drop_off_type = int(drop_off_type) except ValueError: problems.InvalidValue('drop_off_type', drop_off_type) else: if drop_off_type < 0 or drop_off_type > 3: problems.InvalidValue('drop_off_type', drop_off_type) self.drop_off_type = drop_off_type if (self.pickup_type == 1 and self.drop_off_type == 1 and self.arrival_secs == None and self.departure_secs == 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 (self.departure_secs < self.arrival_secs)): problems.InvalidValue( 'departure_time', departure_time, 'The departure time at this stop (%s) is before ' 'the arrival time (%s). This is often caused by ' 'problems in the feed exporter\'s time conversion') # 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): # self.departure_secs might be None because departure_time was invalid, # so we need to check both problems.MissingValue( 'departure_time', 'arrival_time and departure_time should either ' 'both be provided or both be left blank. ' '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): problems.MissingValue( 'arrival_time', 'arrival_time and departure_time should either ' 'both be provided or both be left blank. ' 'It\'s OK to set them both to the same value.') if shape_dist_traveled in (None, ""): self.shape_dist_traveled = None else: try: self.shape_dist_traveled = float(shape_dist_traveled) except ValueError: problems.InvalidValue('shape_dist_traveled', shape_dist_traveled) if stop_sequence is not None: self.stop_sequence = stop_sequence