Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_excessive_subsecond_precision(self):
self.assertEqual(
ciso8601.parse_datetime('20140203T103527.234567891234'),
datetime.datetime(2014, 2, 3, 10, 35, 27, 234567)
)
def get_row(self, item):
time = item['scheduledCall']['scheduledDepartureTime']
if not time:
return
live = item.get('expectedDepartureTime')
if live:
live = ciso8601.parse_datetime(live)
return {
'time': ciso8601.parse_datetime(time),
'live': live,
'service': self.get_service(item['routeInfo']['lineName']),
'destination': item['tripInfo']['headsign'],
'vehicle': item['vehicleRTI']['vehicleID'],
'operator': item['agencyCode'],
}
def decode_datetime_objects(nested_value):
if isinstance(nested_value, list):
return [decode_datetime_objects(item) for item in nested_value]
elif isinstance(nested_value, dict):
for key, value in nested_value.iteritems():
if isinstance(value, dict) and 'type' in value.keys():
if value['type'] == 'encoded_datetime':
nested_value[key] = ciso8601.parse_datetime(value['value'])
if value['type'] == 'encoded_date':
nested_value[key] = ciso8601.parse_datetime(value['value']).date()
if value['type'] == 'encoded_time':
nested_value[key] = ciso8601.parse_datetime(value['value']).time()
if value['type'] == 'encoded_decimal':
nested_value[key] = Decimal(value['value'])
elif isinstance(value, dict):
nested_value[key] = decode_datetime_objects(value)
elif isinstance(value, list):
nested_value[key] = decode_datetime_objects(value)
return nested_value
return nested_value
print(item)
elif operator.pk == 'SCCM':
operator_options = ('SCCM', 'SCPB', 'SCHU', 'SCBD')
elif operator.pk == 'CBBH':
operator_options = ('CBBH', 'CBNL')
vehicle, created = self.get_vehicle(operator_options or operator, item)
journey = None
if 'PublishedLineName' in item:
line_name = item['PublishedLineName']
journey_code = item['DatedVehicleJourneyRef']
if journey_code == 'UNKNOWN':
journey_code = ''
departure_time = ciso8601.parse_datetime(item['OriginAimedDepartureTime'])
else:
line_name = ''
journey_code = ''
departure_time = None
recorded_at_time = ciso8601.parse_datetime(item['RecordedAtTime'])
if not created and vehicle.latest_location:
location = vehicle.latest_location
latest_journey = location.journey
if line_name == latest_journey.route_name and journey_code == latest_journey.code:
if departure_time is None or departure_time == latest_journey.datetime:
if recorded_at_time - location.datetime < timedelta(hours=1):
journey = journey = latest_journey
else:
location = VehicleLocation()
if isinstance(end, int):
# TODO: add check for future (negative values) and ensure that start < end
if not isinstance(start_time, timedelta):
raise ValueError("Can't mix relative and absolute times")
end_time = timedelta(seconds=end)
elif isinstance(end, timedelta):
if not isinstance(start_time, timedelta):
raise ValueError("Can't mix relative and absolute times")
end_time = end
elif end is None:
end_time = utcnow() # TODO: or MAX_DATE?
elif isinstance(end, datetime):
end_time = end.replace(tzinfo=UTC)
else:
end_time = ciso8601.parse_datetime(end).replace(tzinfo=UTC)
if isinstance(start_time, timedelta):
return RelativeTimeInterval(start=start_time, end=end_time)
else:
return TimeInterval(start=start_time, end=end_time)
def get_datetime(item):
try:
return timezone.make_aware(parse_datetime(item['live']['timestamp']['dateTime']))
except AmbiguousTimeError:
return timezone.make_aware(parse_datetime(item['live']['timestamp']['dateTime']), is_dst=True)
def get_datetime(string):
if string:
try:
when = parse_datetime(string)
except ValueError:
when = datetime.strptime(string, '%d/%m/%Y %H:%M:%S')
try:
return timezone.make_aware(when)
except ValueError:
return when
def departures_from_response(self, res):
return sorted([{
'time': ciso8601.parse_datetime(item['ScheduledArrival']),
'live': ciso8601.parse_datetime(item['ExpectedArrival']),
'service': self.get_service(item['LineName']),
'destination': item['DestinationName'],
} for item in res.json()['Predictions']['Prediction'] if item['ExpectedArrival']], key=lambda d: d['live'])
def decode_datetime_objects(nested_value):
if isinstance(nested_value, list):
return [decode_datetime_objects(item) for item in nested_value]
elif isinstance(nested_value, dict):
for key, value in nested_value.iteritems():
if isinstance(value, dict) and 'type' in value.keys():
if value['type'] == 'encoded_datetime':
nested_value[key] = ciso8601.parse_datetime(value['value'])
if value['type'] == 'encoded_date':
nested_value[key] = ciso8601.parse_datetime(value['value']).date()
if value['type'] == 'encoded_time':
nested_value[key] = ciso8601.parse_datetime(value['value']).time()
if value['type'] == 'encoded_decimal':
nested_value[key] = Decimal(value['value'])
elif isinstance(value, dict):
nested_value[key] = decode_datetime_objects(value)
elif isinstance(value, list):
nested_value[key] = decode_datetime_objects(value)
return nested_value
return nested_value