def get_ride(ride_id): if isinstance(ride_id, int): try: rides = app_settings.RIDES except AttributeError: return jsonify({ 'status_code': 404, 'success': False, 'message': 'Error. Failed to retrieve rides.' }), 404 for ride in rides: for key in ride: if key == 'id' and ride['id'] == ride_id: new_ride = Ride(ride['distance'], ride['startTime'], ride['duration']) current_ride = dict() current_ride[ 'readableDuration'] = new_ride.get_human_readable_duration( ) current_ride['endTime'] = new_ride.get_end_time() return jsonify({ 'status_code': 200, 'success': True, 'data': current_ride }), 200 return jsonify({ 'status_code': 404, 'success': False, 'message': 'This ride does not exist.' }), 404
def start_ride(self, user_id, start_position, start_time): ride = Ride(self.id, user_id) ride.start_ride(start_position, start_time) self.ride = ride self.current_position = start_position if (self.last_ride_end_time > 0): this_wait = start_time - self.last_ride_end_time if (this_wait > self.longest_wait): self.longest_wait = this_wait return self.ride
def on_post(self, request, response, rider_id=None): if not rider_id: raise falcon.HTTPPreconditionFailed("Rider id not provided") rider = helpers.get_rider(rider_id) if not rider: raise falcon.HTTPPreconditionFailed("Rider with id: {} not found".format(rider_id)) if rider.riding: raise falcon.HTTPUnprocessableEntity("Rider is already riding") body = json.load(request.stream) pickup_location = body.get("pickup_location") drop_location = body.get("drop_location") if not pickup_location or not drop_location: raise falcon.HTTPPreconditionFailed("Pickup or Drop location not specified") try: ride = Ride(Location(**pickup_location), Location(**drop_location), rider) except Exception as e: raise falcon.HTTPUnprocessableEntity(e) taxi_category = body.get("category") taxi = helpers.get_nearest_available_taxi(Location(**pickup_location), taxi_category) if not taxi: ride.set_taxi_unavailable() raise falcon.HTTPUnprocessableEntity("Taxi is unavailable") ride.start(taxi) RIDES.append(ride) response.body = json.dumps({"message": "Ride registered.", "data": ride.to_dict()}) response.status = falcon.HTTP_201
def get_all_rides(): try: rides = app_settings.RIDES except AttributeError: return jsonify({ 'status_code': 404, 'success': False, 'message': 'Error. Failed to retrieve rides.' }), 404 all_rides = [] for ride in rides: current_ride = Ride(ride['distance'], ride['startTime'], ride['duration']) ride['cost'] = current_ride.calculate_ride_cost() all_rides.append(ride) return jsonify({ 'status_code': 200, 'success': True, 'data': all_rides }), 200
def read_file(self): with open(self.file_path, 'r') as input_file: data = input_file.readlines() # first line is the parameters so pop it off params = data.pop(0).replace('\n', '').split(' ') self.rows, self.columns, self.vehicle_count, self.ride_count, self.on_time_bonus, self.sim_steps = int( params[0]), int(params[1]), int(params[2]), int( params[3]), int(params[4]), int(params[5]) counter = 0 for line in data: parsed_line = line.replace('\n', '').split(' ') ride = Ride(ride_id=counter, row_start=int(parsed_line[0]), col_start=int(parsed_line[1]), row_end=int(parsed_line[2]), col_end=int(parsed_line[3]), earliest_start=int(parsed_line[4]), latest_finish=int(parsed_line[5])) self.rides.append(ride) counter += 1
def get_distance_from_drop_position(self): return Ride.calc_distance(self.current_position, self.drop_position)
def setUp(self): self.user_id = 1234 self.bird_id = "ABCD" self.user = User(self.user_id) self.ride = Ride(self.bird_id, self.user_id)
class RideTest(unittest.TestCase): def setUp(self): self.user_id = 1234 self.bird_id = "ABCD" self.ride = Ride(self.bird_id, self.user_id) def test_initialize(self): """A ride should correctly set user_id and bird_id""" self.assertEqual(self.ride.bird_id, self.bird_id) self.assertEqual(self.ride.user_id, self.user_id) def test_distance(self): """A ride's distance should equal the Cartesian distance between its start and stop points""" self.ride.start_ride(tuple([15, 18]), 20) self.ride.end_ride(tuple([19, 21]), 120) self.assertEqual(self.ride.get_distance(), 5) def test_cost_under_a_minute(self): """A ride under a minute should be free""" start_time = 50 self.ride.start_ride(tuple([15, 18]), start_time) self.ride.end_ride(tuple([15, 31]), start_time + 52) self.assertEqual(self.ride.get_cost(), 0) def test_cost_time_evenly_divided_by_60(self): """A ride should be $1 to start and .15 per minute""" start_time = 100 self.ride.start_ride(tuple([15, 18]), start_time) self.ride.end_ride(tuple([15, 31]), start_time + 120) self.assertEqual(self.ride.get_cost(), 1.30) def test_cost_time_not_evenly_divided_by_60(self): """A ride should be $1 to start and .15 per minute If there are extra seconds that should be counted as an additional minute """ start_time = 200 self.ride.start_ride(tuple([15, 18]), start_time) self.ride.end_ride(tuple([15, 31]), start_time + 153) self.assertEqual(self.ride.get_cost(), 1.45) def test_get_time(self): """A ride's time should equal the difference between end time and start time""" self.ride.start_ride(tuple([15, 18]), 20) self.ride.end_ride(tuple([19, 21]), 120) self.assertEqual(self.ride.get_time(), 100)
def bookRide(self, rideid, riderid, fromLocation, toLocation, user): rider = user.customer_details[riderid] driver = self.fetch_nearest_driver(user, fromLocation) if not driver: raise DriversNotAvailableException ride = Ride() ride.setRideId(rideid) ride.setDriver(driver) ride.setCustomer(rider) ride.setFromLocation(fromLocation) ride.setToLocation(toLocation) RideService.ride_details[rideid] = ride rider.getRides().append(ride) return ride