Untitled
unknown
python
3 years ago
4.5 kB
9
Indexable
import sys
from typing import List, Optional, Tuple, Union
class Command:
def create_parking_lot(self, total_slots: Union[int, str]) -> str:
self.parking_lot = ParkingLot(int(total_slots))
return f"Created a parking lot with {total_slots} slots"
def park(self, registration_number: str, colour: str) -> str:
car = Car(registration_number, colour)
success, slot_number = self.parking_lot.park(car)
if success:
return f"Allocated slot number: {slot_number}"
else:
return "Sorry, parking lot is full"
def leave(self, slot: Union[int, str]) -> str:
_, slot = self.parking_lot.leave(int(slot))
return f"Slot number {slot} is free"
def status(self) -> str:
slots = self.parking_lot.slots
report = []
header = "{header_1: <10}{header_2: <20}{header_3}".format(
header_1="Slot No. ",
header_2="Registration No",
header_3="Colour",
)
report.append(header)
for index, slot in enumerate(slots):
if slot:
content = "{slot_no: <10}{reg_no: <20}{colour}".format(
slot_no=index + 1,
reg_no=slot.registration_number,
colour=slot.colour
)
report.append(content)
return "\n".join(report)
def registration_numbers_for_cars_with_colour(self, colour: str) -> str:
result_queries = self.parking_lot.query_by('colour', colour)
registration_numbers = []
for _, car in result_queries:
if car:
registration_numbers.append(car.registration_number)
if registration_numbers:
return ", ".join(registration_numbers)
else:
return "Not found"
def slot_numbers_for_cars_with_colour(self, colour: str) -> str:
result_queries = self.parking_lot.query_by('colour', colour)
slots = []
for slot, car in result_queries:
slots.append(str(slot))
if slots:
return ", ".join(slots)
else:
return "Not found"
def slot_number_for_registration_number(self, registration_number: str) -> str:
result_queries = self.parking_lot.query_by('registration_number', registration_number)
slots = []
for slot, car in result_queries:
slots.append(str(slot))
if slots:
return ", ".join(slots)
else:
return "Not found"
def exit(self) -> int:
sys.exit(0)
class Car:
def __init__(self, registration_number: str, colour: str):
self.registration_number = registration_number
self.colour = colour
def __repr__(self):
return f"Car: <colour {self.colour}>, <registration_number: {self.registration_number}>"
class ParkingLot:
def __init__(self, total_slots: int):
self.total_slots = total_slots
self.slots: List[Optional[Car]] = [None] * total_slots
def park(self, car: Car) -> Tuple[bool, int | None]:
try:
available_slot_index = self.slots.index(None)
self.slots[available_slot_index] = car
return True, (available_slot_index + 1)
except ValueError:
return False, None
def leave(self, slot_number: int) -> Tuple[Optional[Car], int]:
car = self.slots[slot_number - 1]
self.slots[slot_number - 1] = None
return car, slot_number
def query_by(self, attribute: str, value: str) -> List[Tuple[int, Optional[Car]]]:
matching_cars = []
for index, car in enumerate(self.slots):
car_attribute = getattr(car, attribute)
if car_attribute == value:
matching_cars.append((index + 1, car))
return matching_cars
if __name__ == "__main__":
command = Command()
if len(sys.argv) == 1:
while True:
user_command_with_arguments = input()
user_command, *args = user_command_with_arguments.split()
result = eval(f"command.{user_command}(*args)")
print(result)
elif len(sys.argv) == 2:
list_command = None
with open(sys.argv[1], 'r') as f:
list_command = f.read().strip().split('\n')
for user_command_with_arguments in list_command:
user_command, *args = user_command_with_arguments.split()
result = eval(f"command.{user_command}(*args)")
print(result)
Editor is loading...