예제 #1
0
def get_car_list(filename):
    car_list = []
    with open(csv_filename) as csv_fd:
        reader = csv.reader(csv_fd, delimiter=';')
        next(reader)  # пропускаем заголовок
        for row in reader:
            car_type = ''
            try:
                car_type = row[0]
            except IndexError:
                print('row is passed')
                pass

            if car_type == 'car':
                vehicle = cars.Car(row[1], row[3], row[5], row[2])
            elif car_type == 'truck':
                vehicle = cars.Truck(row[1], row[3], row[5], row[4])
            elif car_type == 'spec_machine':
                vehicle = cars.SpecMachine(row[1], row[3], row[5], row[6])
                print(row[1])
            else:
                print(
                    f'Unknown car type {car_type}; row is not added to database!'
                )
                pass

            car_list.append(vehicle)

    return car_list
예제 #2
0
#导入类
from cars import Car

my_new_car = Car('audi','a4','2016')
print(my_new_car.get_descriptive_name())

my_new_car.odometer_reading = 23
my_new_car.read_odometer()
#导入多个类
from cars import ElectricCar,Car

my_tesla = ElectricCar('tesla','model s',2016)

print(my_tesla.get_descriptive_name())
my_tesla.battery.describe_battery()
my_tesla.battery.get_range()

#导入一个模块
import cars
my_beetle =cars.Car('volkswagen','beetle',2016)
print(my_beetle.get_descriptive_name())

my_tesla = cars.Car('tesla','roadster',2016)


예제 #3
0
"""
导入整个模块
"""
import cars

my_beetle = cars.Car('volkswagen', 'beetle', 2016)
print(my_beetle.get_descriptive_name())

my_tesla = cars.ElectricCar('tesla', 'roadster', 2016)
print(my_tesla.get_descriptive_name())
예제 #4
0
# Once imported, we are free to use each class as needed
from cars import Car, ElectricCar

myBeetle = Car("volkswagen", "beetle", 2019)
print(myBeetle.getDescriptiveName())

myTesla = ElectricCar("tesla", "roadster", 2019)
print(myTesla.getDescriptiveName())

# Importing an entire module
# We can also import an entire module and then access the classes we need by using dot notation. This approach is simple and results in code that is easy to read.
print("\n")
# Here we import the entire cars module and we access it by using moduleName.ClassName syntax
import cars

myBeetle = cars.Car("volkswagen", "beetle", 2019)
print(myBeetle.getDescriptiveName())

myTesla = cars.ElectricCar("tesla", "roadster", 2019)
print(myTesla.getDescriptiveName())

# Importing all classes from a module
# from moduleName import *

# This method is not recommended for a few reasons.
# One with this approach it's unclear which classes we're using from the module. This results in confusion with names in the file. It can result in importing another module that has the same naming for classes which will conflict and or overwrite your classes. This can lead to difficult dianoses.

# Importing a module into a module
# When our module in electricCars.py is import Car from cars, we now need to import both the original car module, and the new electricCars module into our file to use them

print("\n", "module in module")
예제 #5
0
 def deploy_car(self):
     rand_choice = random.randint(1, 6)
     if rand_choice == 1:
         line = random.choice(self.car_lines)
         self.cars.append(cars.Car(line))
예제 #6
0
import cars

my_dream_car = cars.Car('Audi', 'Q5', 2019)
print(my_dream_car.get_descriptive_name())

my_tesla = cars.ElectricCar('tesla', 'model s', 2019)
print(my_tesla.get_descriptive_name())