Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. import os
  2. import csv
  3.  
  4.  
  5. class BaseCar:
  6.     def __init__(self, photo_file_name, brand, carrying):
  7.         self.photo_file_name = photo_file_name
  8.         self.brand = brand
  9.         self.carrying = carrying
  10.  
  11.     def get_photo_file_ext(self):
  12.         return os.path.splitext(self.photo_file_name)[1]
  13.  
  14.  
  15. class Car(BaseCar):
  16.     def __init__(self, photo_file_name, brand, carrying, passenger_seats_count):
  17.         super().__init__(photo_file_name, brand, carrying)
  18.         self.car_type = 'car'
  19.         self.passenger_seats_count = passenger_seats_count
  20.  
  21.  
  22. class Truck(BaseCar):
  23.     def __init__(self, photo_file_name, brand, carrying, body_lwh):
  24.         super().__init__(photo_file_name, brand, carrying)
  25.         self.car_type = 'truck'
  26.         lwh = body_lwh or '0x0x0'
  27.         l, w, h = (float(i) for i in lwh.split('x'))
  28.         self.body_length = l
  29.         self.body_width = w
  30.         self.body_height = h
  31.  
  32.     def get_body_volume(self):
  33.         return self.body_length * self.body_width * self.body_height
  34.  
  35.  
  36. class SpecMachine(BaseCar):
  37.     def __init__(self, photo_file_name, brand, carrying, extra):
  38.         super().__init__(photo_file_name, brand, carrying)
  39.         self.car_type = 'spec_machine'
  40.         self.extra = extra
  41.  
  42.  
  43. def get_car_list(csv_filename):
  44.     car_list = []
  45.     with open(csv_filename) as csv_fd:
  46.         reader = csv.reader(csv_fd, delimiter=';')
  47.         next(reader)  # пропускаем заголовок
  48.         for row in reader:
  49.             try:
  50.                 if row[0] == 'car':
  51.                     car_list.append(Car(row[3], row[1], float(row[5]), int(row[2])))
  52.                 elif row[0] == 'truck':
  53.                     car_list.append(Truck(row[3], row[1], float(row[5]), row[4]))
  54.                 elif row[0] == 'spec_machine':
  55.                     car_list.append(SpecMachine(row[3], row[1], float(row[5]), row[6]))
  56.                 else:
  57.                     pass
  58.             except IndexError:
  59.                 pass
  60.     return car_list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement