bluethundr0

Untitled

Jun 10th, 2021
695
9 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """Model for aircraft flights"""
  2.  
  3. class Flight:
  4.     """A flight with a particular passenger aircraft."""
  5.     def __init__(self,number,aircraft):
  6.         if not number[:2].isalpha():
  7.             raise ValueError("No airline code in '{}'".format(number))
  8.        
  9.         if not number[:2].isupper():
  10.             raise ValueError("Invalid airline code '{}'".format(number))        
  11.              
  12.         if not (number[2:].isdigit() and int(number[2:]) <= 9999):
  13.             raise ValueError("Invalid route number '{}'".format( number))
  14.        
  15.         self._number = number
  16.         self._aircraft = aircraft
  17.  
  18.         rows,seats = self._aircraft.seating_plan()
  19.         self._seating = [None] + [{letter: None for letter in seats} for _ in rows]
  20.  
  21.     def number(self):
  22.         return self._number
  23.  
  24.     def airline(self):
  25.         return self._number[:2]
  26.  
  27.     def aircraft_model(self):
  28.         return self._aircraft.model()
  29.  
  30.     def seating_plan(self):
  31.        return (range(1, self._num_rows + 1),"ABCDEFGHJK"[:self._num_seats_per_row])
  32.  
  33.     def allocate_seat(self, seat, passenger):
  34.         """Allocate a seat to a passenger.
  35.  
  36.        Args:
  37.            seat: A seat designator such as '12C' or '21F'.
  38.            passenger: The passenger name.
  39.  
  40.        Raises:
  41.            ValueError: If the seat is unavailable.
  42.        """
  43.         row, letter = self._parse_seat(seat)
  44.  
  45.         if self._seating[row][letter] is not None:
  46.             raise ValueError("Seat {} already occupied".format(seat))
  47.  
  48.         self._seating[row][letter] = passenger
  49.  
  50.     def _parse_seat(self,seat):
  51.         """Parse a seat designator into a valid row and letter.
  52.            Args:
  53.                seat: A seat designator such as 12F
  54.                
  55.            Returns:
  56.                A tuple containing an integer and a string for a row and a seat.
  57.        """
  58.         row_numbers, seat_letters = self._aircraft.seating_plan()
  59.  
  60.         letter = seat[-1]
  61.         if letter not in seat_letters:
  62.             raise ValueError("Invalid seat letter {}".format(letter))
  63.  
  64.         row_text = seat[:-1]
  65.         try:
  66.             row = int(row_text)
  67.         except ValueError:
  68.             raise ValueError("Invalid seat letter {}".format(row))
  69.  
  70.         return row, letter
  71.  
  72.     def relocate_passenger(self, from_seat, to_seat):
  73.         """Relocate a passenger to a different seat.
  74.  
  75.        Args:
  76.            from_seat: The existing seat designator for the passenger to be moved.
  77.  
  78.            to_seat: The new seat designator.
  79.        """
  80.         from_row, from_letter = self._parse_seat(from_seat)
  81.  
  82.         if self._seating[from_row][from_letter] is None:
  83.             raise ValueError("No passenger to relocate in seat {}".format(from_seat))
  84.  
  85.             to_row, to_letter = self._parse_seat(to_seat)
  86.  
  87.             if self._seating[from_row][from_letter] is None:
  88.                 raise ValueError("No passenger to relocate in seat {}".format(from_seat))
  89.  
  90.             to_row, to_letter = self._parse_seat(to_seat)
  91.  
  92.             if self._seating[to_row] is not None:
  93.                 raise ValueError("Seat {} already occupied".format(to_seat))
  94.  
  95.             self._seating[to_row][to_letter] = self._seating[from_row][from_letter]
  96.             self._seating[from_row][from_letter] = None
  97.  
  98.     def num_available_seats(self):
  99.         return sum(sum(1 for s in row.values() if s is None)
  100.         for row in self._seating
  101.         if row is not None)
  102.  
  103.     def make_boarding_cards(self, card_printer):
  104.         for passenger, seat in sorted(self._passenger_seats()):
  105.             card_printer(passenger, seat, self.number(),
  106.             self.aircraft_model())
  107.  
  108.     def _passenger_seats(self):
  109.         """An iterable series of passenger seating allocations."""
  110.         row_numbers, seat_letters = self._aircraft.seating_plan()
  111.         for row in row_numbers:
  112.             for letter in seat_letters:
  113.                 passenger = self._seating[row][letter]
  114.                 if passenger is not None:
  115.                     yield(passenger, "{}{}".format(row,letter))
  116.  
  117. class Aircraft:
  118.     def num_seats(self):
  119.         rows, row_seats = self._seating_plan()
  120.         return len(rows) * len(row_seats)
  121.  
  122. class AirbusA319(Aircraft):
  123.     def __init__(self, registration):
  124.         self._registration = registration
  125.  
  126.     def registration(self):
  127.         return self._registration
  128.  
  129.     def model(self):
  130.         return "Airbus A319"
  131.  
  132.     def seating_plan(self):
  133.         return range(1, 23), "ABCDEF"
  134.  
  135. class Boeing777(Aircraft):
  136.     def __init__(self, registration):
  137.         self._registration = registration
  138.  
  139.     def registration(self):
  140.         return self._registration
  141.  
  142.     def model(self):
  143.         return "Boeing 777"
  144.  
  145.     def seating_plan(self):
  146.         # For simplicity's sake we ignore complex
  147.         # seating arrangement for first-class
  148.         return range(1, 56),"ABCDEGHK"
  149.    
  150. def make_flights():
  151.     f = Flight("BA758", AirbusA319("G-EUPT"))
  152.     f.allocate_seat('12A', 'Guido van Rossum')
  153.     f.allocate_seat('15F', 'Bjarne Stroustrup')
  154.     f.allocate_seat('15E', 'Anders Heljsberg')
  155.     f.allocate_seat('1C', 'John McCarthy')
  156.     f.allocate_seat('1D', 'Richard Hickey')
  157.  
  158.     g = Flight("AF72", Boeing777("F-GSPS"))
  159.     g.allocate_seat('55K', 'Larry Wall')
  160.     g.allocate_seat('33G', 'Yukihiro Matsumoto')
  161.     g.allocate_seat('4B', 'Brian Kernighan')
  162.     g.allocate_seat('4A', 'Dennis Ritchie')
  163.  
  164.     return f, g
  165.  
  166. def console_card_printer(passenger, seat, flight_number, aircraft):
  167.     output = "| Name: {0}"                                        \
  168.                 "   Flight: {1}"                                  \
  169.                 "   Seat: {2}"                                    \
  170.                 "   Airctaft: {3}"                                \
  171.                 " |".format(passenger, flight_number, seat, aircraft)
  172.     banner = '+' + '-' * (len(output) -2) + '+'
  173.     border = '|' + ' ' * (len(output) - 2) + '|'
  174.     lines = [banner, border, output, border, banner]
  175.     card = '\n'.join(lines)
  176.     print(card)
  177.     print()
  178.  
  179. def make_boarding_cards(self, card_printer):
  180.     for passenger, seat in sorted(self._passenger_seats()):
  181.         card_printer(passenger, seat, self.number(),
  182.         self.aircraft_model())
RAW Paste Data