Booking seats

Now we can proceed with implementing a simple booking system. For each flight we simply need to keep track of who is sitting in each seat. We'll represent the seat allocations using a list of dictionaries. The list will contain one entry for each seat row, and each entry will be a dictionary mapping from seat-letter to occupant name. If a seat is unoccupied, the corresponding dictionary value will contain None.

We initialize the seating plan in Flight.__init__() using this fragment:

rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [{letter: None for letter in seats} for _ in rows]

In the first line we retrieve the seating plan for the aircraft and use tuple unpacking to put the row and seat identifiers into local variables rows and seats. In the second line we create a list for the seat allocations. Rather than continually deal with the fact that row indexes are one-based whereas Python lists use zero-based indexes, we choose to waste one entry at the beginning of the list. This first wasted entry is the single element list containing None. To this single element list we concatenate another list containing one entry for each real row in the aircraft. This list is constructed by a list comprehension which iterates over the rows object, which is the range of row numbers retrieved from the  _aircraft on the previous line:


Figure 8.4: The object graph for the seating-plan data structure

We're not actually interested in the row number, since we know it will match up with the list index in the final list, so we discard it by using the dummy underscore variable.

The item expression part of the list comprehension is itself a comprehension; specifically a dictionary comprehension! This iterates over each row letter, and creates a mapping from the single character string to None to indicate an empty seat.

We use a list comprehension, rather than list replication with the multiplication operator, because we want a distinct dictionary object to be created for each row; remember, repetition is shallow.

Here's the code after we put it into the initializer:

def __init__(self, number, aircraft):
if not number[:2].isalpha():
raise ValueError("No airline code in '{}'".format(number))

if not number[:2].isupper():
raise ValueError("Invalid airline code '{}'".format(number))

if not (number[2:].isdigit() and int(number[2:]) <= 9999):
raise ValueError("Invalid route number '{}'".format(number))

self._number = number
self._aircraft = aircraft

rows, seats = self._aircraft.seating_plan()
self._seating = [None] + [{letter: None for letter in seats} for _
in rows]

Before we go further, let's test our code in the REPL:

>>> from airtravel import *
>>> f = Flight("BA758", Aircraft("G-EUPT", "Airbus A319", num_rows=22,
... num_seats_per_row=6))
>>>

Thanks to the fact that everything is "public" we can access implementation details during development. It's clear enough that we're deliberately defying convention here during development, since the leading underscore reminds us what’s public and what's private:

>>> f._seating
[None, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None,
'A': None},

{'F': None, 'D': None, 'E': None, 'B': None, 'C': None, 'A': None}, {'F': None,'D': None, 'E': None, 'B': None, 'C': None, 'A': None},
{'F': None, 'D': None,
'E': None, 'B': None, 'C': None, 'A': None}, {'F': None, 'D': None, 'E': None,'B': None, 'C': None, 'A': None},
{'F': None, 'D': None, 'E': None, 'B': None,
'C': None, 'A': None}, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None,'A': None}, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None, 'A': None},
{'F': None, 'D': None, 'E': None, 'B': None, 'C': None, 'A': None}, {'F': None,'D': None, 'E': None, 'B': None, 'C': None, 'A': None}, {'F': None, 'D': None,'E': None, 'B': None, 'C': None, 'A': None}, {'F': None, 'D': None, 'E': None,'B': None, 'C': None, 'A': None}, {'F': None, 'D': None, 'E': None, 'B': None,'C': None, 'A': None}, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None,'A': None}, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None, 'A': None},
{'F': None, 'D': None, 'E': None, 'B': None, 'C': None, 'A': None}, {'F': None,'D': None, 'E': None, 'B': None, 'C': None, 'A': None}, {'F': None, 'D': None,'E': None, 'B': None, 'C': None, 'A': None}, {'F': None, 'D': None, 'E': None,'B': None, 'C': None, 'A': None}, {'F': None, 'D': None, 'E': None, 'B': None,'C': None, 'A': None}, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None,'A': None}, {'F': None, 'D': None, 'E': None, 'B': None, 'C': None, 'A': None}]

Tha's accurate, but not particularly beautiful. Let's try again with pretty-print:

>>> from pprint import pprint as pp
>>> pp(f._seating)
[None,
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None},
{'A': None, 'B': None, 'C': None, 'D': None, 'E': None, 'F': None}]

Perfect!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset