-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
72 lines (55 loc) · 2.25 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pandas as pd
import pickle
import time
from math import atan2, cos, pow, sin, sqrt
import const
get_ratio_func = lambda x: round(x['Earnings'] / ((x['Distance'] + x['CraftDistance']) / x['CraftCruise']), 2)
def get_distance(lat1, lon1, lat2, lon2):
r = 6371000
dlon = lon2 - lon1
dlat = lat2 - lat1
a = pow(sin(dlat/2), 2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2), 2)
c = 2 * atan2(sqrt(a), sqrt(1-a))
return round((r * c) / 1850, 1)
def get_earnings(row, rent_type):
res = row['Pay']
pt_amount = row['PtAssignment']
if pt_amount > 6:
res -= res * pt_amount / 100
return round(res - row[rent_type], 2) if row[rent_type] else 0
def get_ratio(x, earnings_column):
return round(x[earnings_column] / ((x['Distance'] + x['CraftDistance']) / x['Cruise']), 2)
def load_pickled_assignments():
with open('assignments', 'rb') as f:
assignments = pickle.load(f)
assignments.Pay = assignments.Pay.astype(int)
assignments.Amount = assignments.Amount.astype(int)
assignments['All-In'] = assignments['All-In'].map(lambda x: True if x == 'true' else False)
assignments.PtAssignment = assignments.PtAssignment.map(lambda x: True if x == 'true' else False)
return assignments
def load_airports():
airports = pd.read_csv(const.AIRPORTS_FILENAME)
airports.lat = airports.lat.astype(float)
airports.lon = airports.lon.astype(float)
return airports
def load_aircrafts():
aircrafts = pd.read_csv(const.AIRCRAFTS_FILENAME)
aircrafts.columns = ['Model', 'Crew', 'Seats', 'Cruise', 'Ext1', 'LTip', 'LAux', 'LMain', 'Center', 'Center2',
'Center3', 'RMain', 'RAux', 'RTip', 'Ext2', 'GPH', 'FuelType', 'MTOW', 'EmptyWeight', 'Price']
aircrafts.Seats = aircrafts.Seats.astype(int)
aircrafts.Crew = aircrafts.Crew.astype(int)
aircrafts.Cruise = aircrafts.Cruise.astype(float)
return aircrafts
def retry(func, *args, **kwargs):
c = 1
count = kwargs.pop("count", 10)
error_type = kwargs.pop("error_type", Exception)
interval = kwargs.pop("interval", 60)
while True:
try:
return func(*args, **kwargs)
except error_type:
if c >= count:
raise
c += 1
time.sleep(interval)