-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·169 lines (127 loc) · 4.25 KB
/
main.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import csv, os, sys
from decimal import *
from tabulate import tabulate
from collections import defaultdict
TRIP_PATH = sys.argv[1] if len(sys.argv) > 1 else './trips/'
TABULATION = "\n "
def parseCSV(file):
with open(file, "r") as csvFile:
csvData = list(csv.reader(csvFile))
return csvData
def calcTotals(tripItems):
base = 0
worn = 0
food = 0
for item in tripItems:
isWorn = item[8].lower()
isFood = item[9].lower()
worn += bool(isWorn)
food += bool(isFood)
base += not isWorn and not isFood
table = [
["Base", base],
["Worn", worn],
["Food", food],
["Total", len(tripItems)]
]
headers = ["Category", "Count"]
return tabulate(table, headers=headers, tablefmt="simple", numalign="center")
def calcWeight(tripItems):
base = 0
worn = 0
food = 0
nullValues = 0
for item in tripItems:
quantity = item[3]
value = item[4]
isWorn = item[8].lower()
isFood = item[9].lower()
if quantity and value:
if isWorn != "":
worn += int(quantity) * float(value)
elif isFood != "":
food += int(quantity) * float(value)
else:
base += int(quantity) * float(value)
else:
nullValues += 1
total = base + worn + food
table = [
["Base", gramsToLBS(base)],
["Worn", gramsToLBS(worn)],
["Food", gramsToLBS(food)],
["Total", gramsToLBS(total)]
]
headers = ["Category", "Weight (lbs)"]
return tabulate(table, headers=headers, tablefmt="simple", numalign="center") + nullValueFormat(nullValues)
def calcCost(tripItems):
base = 0
worn = 0
food = 0
nullValues = 0
for item in tripItems:
quantity = item[3]
value = item[7]
isWorn = item[8].lower()
isFood = item[9].lower()
if quantity and value:
if isWorn:
worn += int(quantity) * Decimal(value)
elif isFood:
food += int(quantity) * Decimal(value)
else:
base += int(quantity) * Decimal(value)
else:
nullValues += 1
total = base + worn + food
table = [
["Base", "${:,.2f}".format(base)],
["Worn", "${:,.2f}".format(worn)],
["Food", "${:,.2f}".format(food)],
["Total", "${:,.2f}".format(total)]
]
headers = ["Category", "Value"]
return tabulate(table, headers=headers, tablefmt="simple", numalign="center") + nullValueFormat(nullValues)
def calcCategories(tripItems):
tmpList = defaultdict(lambda: {"count": 0, "weight": 0, "value": 0})
table = []
for item in tripItems:
category = item[1]
weight = item[4]
cost = item[7]
tmpList[category]["count"] += 1
tmpList[category]["weight"] += float(weight or 0)
tmpList[category]["value"] += Decimal(cost or 0)
for category, info in tmpList.items():
count = info['count']
weight = gramsToLBS(info['weight'])
value = "${:,.2f}".format(info['value'])
table.append([category, count, weight, value])
headers = ["Category", "Count", "Weight (lbs)", "Value"]
return tabulate(table, headers=headers, tablefmt="simple", numalign="center")
def gramsToLBS(grams):
return format(float(grams) / 453.59237, ".2f")
def nullValueFormat(value):
if value and value > 0:
return f"\n\033[91mNo Data: {value}\033[0m"
return ""
def buildTripList(TRIP_PATH):
if os.path.isdir(TRIP_PATH):
return [(file, parseCSV(TRIP_PATH + file)) for file in os.listdir(TRIP_PATH)]
elif os.path.isfile(TRIP_PATH):
return [(TRIP_PATH, parseCSV(TRIP_PATH))]
def calculateTrip(trip):
tripName, tripItems = trip
print("Trip: " + tripName + "\n")
print(calcTotals(tripItems) + "\n")
print(calcWeight(tripItems) + "\n")
print(calcCost(tripItems) + "\n")
print(calcCategories(tripItems) + "\n")
def main():
listOfTrips = buildTripList(TRIP_PATH)
for trip in listOfTrips:
calculateTrip(trip)
if len(listOfTrips) > 1:
print("********************************************************\n")
if __name__ == "__main__":
main()