def trip_transport_matrix(trips_year): """ Return the matrices of TripTemplates and dates, with each entry containing the trip that is (dropped off, picked up, return to Hanover) on the given date. """ # TODO: only show visible triptypes templates = TripTemplate.objects.filter(trips_year=trips_year) dates = Section.dates.trip_dates(trips_year) dropoff_matrix = OrderedMatrix(templates, dates) pickup_matrix = OrderedMatrix(templates, dates) return_matrix = OrderedMatrix(templates, dates) trips = Trip.objects.with_counts(trips_year=trips_year).select_related( 'dropoff_route', 'pickup_route', 'return_route', 'template__pickup_stop__route', 'template__dropoff_stop__route', 'template__return_route', ) for trip in trips: dropoff_matrix[trip.template][trip.dropoff_date] = trip pickup_matrix[trip.template][trip.pickup_date] = trip return_matrix[trip.template][trip.return_date] = trip return dropoff_matrix, pickup_matrix, return_matrix
def matrix(self, trips_year): """ Return a matrix of scheduled trips. matrix[template][section] gives the scheduled trip with template and section, or None if it is not scheduled. Returns OrderedDicts so that you can iterate over the keys: >>> for template in matrix: >>> for section in matrix[template]: >>> ... """ from fyt.trips.models import Section, TripTemplate sections = Section.objects.filter(trips_year=trips_year) templates = TripTemplate.objects.filter( trips_year=trips_year).select_related('triptype') matrix = OrderedMatrix(templates, sections) # see https://docs.djangoproject.com/en/dev/ref/models/querysets/#id7 # http://stackoverflow.com/questions/6795202/django-count-in-multiple-annotations trips = self.with_counts(trips_year) for trip in trips: matrix[trip.template][trip.section] = trip return matrix
def get_internal_rider_matrix(trips_year): """ Compute which trips are riding on each route every day. """ routes = Route.objects.internal(trips_year).select_related('vehicle') dates = Section.dates.trip_dates(trips_year) trips = Trip.objects.with_counts(trips_year).select_related( 'template', 'section', 'pickup_route', 'dropoff_route', 'return_route', 'template__dropoff_stop__route', 'template__pickup_stop__route', 'template__return_route', ) matrix = OrderedMatrix(routes, dates, lambda: Riders()) for trip in trips: # dropoff if trip.get_dropoff_route(): matrix[trip.get_dropoff_route()][trip.dropoff_date] += Riders( dropping_off=[trip]) # pickup if trip.get_pickup_route(): matrix[trip.get_pickup_route()][trip.pickup_date] += Riders( picking_up=[trip]) # return matrix[trip.get_return_route()][trip.return_date] += Riders( returning=[trip]) return matrix
def external_route_matrix(trips_year, default=None): """ Return an OrderedMatrix of [routes][sections] for local sections and external routes. """ from fyt.trips.models import Section from fyt.transport.models import Route rts = Route.objects.external(trips_year) sxns = Section.objects.local(trips_year) return OrderedMatrix(rts, sxns, default=default)
def matrix(self, trips_year): from .models import Gear requests = self.filter(trips_year=trips_year).prefetch_related( 'gear', 'provided' ) equipment = Gear.objects.filter(trips_year=trips_year) matrix = OrderedMatrix(requests, equipment, default=False) for request in requests: for gear in request.gear.all(): matrix[request][gear] = True return matrix
def get_internal_route_matrix(trips_year): """ A matrix of all the scheduled internal buses, categorized by date and by route. """ routes = Route.objects.internal(trips_year).select_related('vehicle') dates = Section.dates.trip_dates(trips_year) matrix = OrderedMatrix(routes, dates) scheduled = (InternalBus.objects.internal(trips_year).select_related( 'route__vehicle').prefetch_related('stoporder_set')) preload_transported_trips(scheduled, trips_year) for bus in scheduled: matrix[bus.route][bus.date] = bus return matrix
def matrix(self, trips_year): """ Return of a matrix of trips residency by date. matrix[campsite][date] is a list of all trips staying at campsite on date. """ from .models import Section, Trip campsites = self.filter(trips_year=trips_year) dates = Section.dates.camping_dates(trips_year) matrix = OrderedMatrix(campsites, dates, default=list) trips = Trip.objects.filter(trips_year=trips_year).select_related( 'template__campsite1', 'template__campsite2' ) for trip in trips: matrix[trip.template.campsite1][trip.section.at_campsite1].append(trip) matrix[trip.template.campsite2][trip.section.at_campsite2].append(trip) return matrix
def test_map_creates_new_instance(self): rows = [0, 1] cols = [0, 1] m = OrderedMatrix(rows, cols, default=0) n = m.map(lambda x: x + 1) self.assertEqual(m[0][0], 0)
def test_map(self): rows = [0, 1] cols = [0, 1] m = OrderedMatrix(rows, cols, default=0) n = m.map(lambda x: x + 1) self.assertTrue(n[0][0] == n[0][1] == n[1][0] == n[1][1] == 1)
def test_truncate_matrix(self): rows = [0, 1] cols = [0, 1] m = OrderedMatrix(rows, cols) m[1][1] = True self.assertEqual(m.truncate(), {1: {0: None, 1: True}})