SHARE
TWEET

Untitled

a guest Jan 28th, 2020 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. from enum import Enum
  3. import datetime
  4. import random
  5. import pandas as pd
  6.  
  7. class KindOfTrip(Enum):
  8.     IN_CITY = 1
  9.     OUTSIDE_CITY = 2
  10.  
  11. class Trip(object):
  12.     def __init__(self, date, number, brand, speedometer, kind_of_trip):
  13.         self.date = date
  14.         self.number = number
  15.         self.brand = brand
  16.         self.speedometer = speedometer
  17.         self.kind_of_trip = kind_of_trip
  18.    
  19.     def __lt__(self, other):
  20.         return (self.number, self.date) <  (other.number, other.date)
  21.  
  22.     def __sub__(self, other):
  23.         if self.number == other.number:
  24.             return self.speedometer - other.speedometer
  25.         else:
  26.             raise ValueError("Different numbers")    
  27.  
  28.     def __str__(self):
  29.         return "Trip : date-" + str(self.date) + ", number-" + \
  30.                 self.number + ", brand -" +self.brand + \
  31.                 ", speedometer: " + str(self.speedometer) + \
  32.                 ", kind of trip - " + str(self.kind_of_trip)
  33.  
  34.     def to_dict(self):
  35.         return {
  36.             "date": self.date,
  37.             "number": self.number,
  38.             "brand": self.brand,
  39.             "speedometer": self.speedometer,
  40.             "kind": self.kind_of_trip
  41.         }            
  42.        
  43. class T_char(object):
  44.     def __init__(self, incity_mileage, outside_mileage, tech_check_distance):
  45.         self.incity_mileage = incity_mileage
  46.         self.outside_mileage = outside_mileage
  47.         self.tech_check_distance = tech_check_distance
  48.  
  49.     def to_dict(self):
  50.         return {
  51.             "incity": self.incity_mileage,
  52.             "outside": self.outside_mileage,
  53.             "tech_check": self.tech_check_distance
  54.         }
  55.  
  56.  
  57. char = {
  58.     "Nissan": T_char(12, 10, 2000),
  59.     "Suzuki": T_char(10, 8, 1000),
  60.     "Mazda": T_char(15, 10, 5000 )
  61. }
  62.  
  63.  
  64. def parse(line):
  65.     datestr, number, model, speedometer_str, kind_str = line.split()
  66.     dt = datetime.datetime.strptime(datestr, "%d.%m.%Y")
  67.     speedometer = int(speedometer_str)
  68.     kind = None
  69.     if kind_str == "incity":
  70.         kind = KindOfTrip.IN_CITY
  71.     else:
  72.         kind = KindOfTrip.OUTSIDE_CITY
  73.  
  74.     return Trip(dt, number, model, speedometer, kind)
  75.  
  76. def calculate_for_car(number, trips):
  77.     car_trips = []
  78.     for trip in trips:
  79.         if trip.number == number:
  80.             car_trips.append(trip)
  81.  
  82.     car_trips = sorted(car_trips)
  83.    
  84.     if len(car_trips) == 0:
  85.         raise ValueError("Car not present")
  86.  
  87.     car_char = char[car_trips[0].brand]
  88.     milein = car_char.incity_mileage
  89.     mileout = car_char.outside_mileage
  90.     tech_check = car_char.tech_check_distance
  91.  
  92.     mileage = 0
  93.     fuel = 0
  94.     for i in range(1, len(car_trips)):
  95.         lentrip = car_trips[i] - car_trips[i-1]
  96.         mileage += lentrip
  97.         if car_trips[i].kind_of_trip == KindOfTrip.IN_CITY:
  98.             fuel += milein*lentrip
  99.         else:
  100.             fuel += mileout*lentrip
  101.  
  102.     should_check = (car_trips[-1].speedometer%tech_check) >= 0.95 * tech_check
  103.     return fuel, mileage, should_check
  104.  
  105.  
  106.  
  107. def get_cars_for_check(cars, trips):
  108.     result = []
  109.     for car in cars:
  110.         _, _, should_check = calculate_for_car(car, trips)
  111.         if should_check:
  112.             result.append(car)
  113.     return result
  114.  
  115.        
  116.  
  117. if __name__ == "__main__":
  118.     f = open("journal.txt", "r")
  119.     trips = []
  120.     for line in f.readlines():
  121.         trips.append(parse(line))
  122.  
  123.     while True:
  124.         ans = input("Do you want to enter new trip (y/n)")
  125.         if ans == "n":
  126.             break
  127.         line = input("Enter data: ")
  128.         trips.append(parse(line))
  129.  
  130.     random.shuffle(trips)
  131.     for trip in trips:
  132.         print(trip)
  133.  
  134.     trips = sorted(trips)
  135.  
  136.     print("----")
  137.  
  138.     for trip in trips:
  139.         print(trip)
  140.  
  141.     df = pd.DataFrame.from_records([s.to_dict() for s in trips])
  142.     df.to_csv("trips.csv")
  143.     print(df)
  144.  
  145.     char_records = []
  146.  
  147.     for model in df['brand'].unique():
  148.         record = char[model].to_dict()
  149.         record['model'] = model
  150.         char_records.append(record)
  151.  
  152.     df = pd.DataFrame.from_records( char_records)
  153.  
  154.     print("----")
  155.     print(df)
  156.  
  157.     df.to_csv("chars.csv")
  158.  
  159.  
  160.     cars = set()
  161.     for trip in trips:
  162.         cars.add(trip.number)
  163.  
  164.     for car in cars:
  165.         fuel, mileage, should_check = calculate_for_car(car, trips)
  166.         print(car, ":", mileage, "km,", fuel, "lt, ", should_check)
  167.  
  168.     print(get_cars_for_check(cars, trips))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top