-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort-output.py
241 lines (192 loc) · 7.96 KB
/
sort-output.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
###############################################################################
# #
# A custom Python script to sort the output of the Hadoop job by revenue in #
# decreasing order (i.e highest revenue at the top) as well as a few other #
# interesting orders. #
# #
# author: christopher moussa #
# #
# usage: python3 sort-output.py path/to/output_file #
# #
###############################################################################
import sys
import os
def enrich_by_month_year(file_object):
revenue_dict = {}
for line in file_object:
# convert line to key-value pair
line = "=".join(line.split())
tmp_dict = dict(item.split("=") for item in line.split(","))
# - insert key-value pair into dictionary
# - make sure to convert revenue to a float and round to 2 decmial
# places
for i in tmp_dict:
revenue_dict[i] = round(float(tmp_dict[i]), 2)
# sort the dictionary that contains all key-value pairs of month/year-revenue
# by value (i.e revenue)
sorted_revenue = sorted(revenue_dict.items(), key=lambda x: x[1])
# print out the dictionary in reverse so that the key-value pair with the
# highest revenue gets printed out first
#
# format: month-year: 000.00
for month_year in reversed(sorted_revenue):
print(f"{month_year[0]}: {month_year[1]}")
return revenue_dict
def enrich_by_season(revenue_dict):
# dictionary that specifies which season a month belongs to
seasons = {
"fall": [9, 10, 11],
"winter": [12, 1, 2],
"spring": [3, 4, 5],
"summer": [6, 7, 8],
}
# first, we need to move our key-value pairs into their respective
# "seasons" bins, as defined by the seasons dictionary above
fall = []
winter = []
spring = []
summer = []
# "for every key in the dictionary"
for i in revenue_dict:
month_year = i.split("-")
if int(month_year[0]) in seasons["fall"]:
fall.append(i)
elif int(month_year[0]) in seasons["winter"]:
winter.append(i)
elif int(month_year[0]) in seasons["spring"]:
spring.append(i)
elif int(month_year[0]) in seasons["summer"]:
summer.append(i)
# now, add up the revenue for each season
fall_revenue = 0.0
for key in fall:
fall_revenue += revenue_dict[key]
winter_revenue = 0.0
for key in winter:
winter_revenue += revenue_dict[key]
spring_revenue = 0.0
for key in spring:
spring_revenue += revenue_dict[key]
summer_revenue = 0.0
for key in summer:
summer_revenue += revenue_dict[key]
seasonal_revenue = {}
seasonal_revenue["fall"] = round(float(fall_revenue), 2)
seasonal_revenue["winter"] = round(float(winter_revenue), 2)
seasonal_revenue["spring"] = round(float(spring_revenue), 2)
seasonal_revenue["summer"] = round(float(summer_revenue), 2)
print(
f"max seasonal revenue was in {max(seasonal_revenue, key=lambda key: seasonal_revenue[key])}: {max(seasonal_revenue.values())}\n"
)
print("seasonal revenue rankings (descending order):")
sorted_seasonal_revenue = sorted(seasonal_revenue.items(), key=lambda x: x[1])
for season in reversed(sorted_seasonal_revenue):
print(f" {season[0]} - {season[1]}")
def enrich_by_year(revenue_dict):
# dictionary of lists to hold key-value pairs by year
revenue_by_years = {
2015: [],
2016: [],
2017: [],
2018: [],
}
# "for every key in the dictionary"
for i in revenue_dict:
month_year = i.split("-")
if int(month_year[1]) == 2015:
revenue_by_years[2015].append(i)
elif int(month_year[1]) == 2016:
revenue_by_years[2016].append(i)
elif int(month_year[1]) == 2017:
revenue_by_years[2017].append(i)
elif int(month_year[1]) == 2018:
revenue_by_years[2018].append(i)
# now, add up the revenue for each year
revenue_2015 = 0.0
for key in revenue_by_years[2015]:
revenue_2015 += revenue_dict[key]
revenue_2016 = 0.0
for key in revenue_by_years[2016]:
revenue_2016 += revenue_dict[key]
revenue_2017 = 0.0
for key in revenue_by_years[2017]:
revenue_2017 += revenue_dict[key]
revenue_2018 = 0.0
for key in revenue_by_years[2018]:
revenue_2018 += revenue_dict[key]
yearly_revenue = {}
yearly_revenue[2015] = round(float(revenue_2015), 2)
yearly_revenue[2016] = round(float(revenue_2016), 2)
yearly_revenue[2017] = round(float(revenue_2017), 2)
yearly_revenue[2018] = round(float(revenue_2018), 2)
print(
f"max yearly revenue was in {max(yearly_revenue, key=lambda key: yearly_revenue[key])}: {max(yearly_revenue.values())}\n"
)
print("yearly revenue rankings (descending order):")
sorted_yearly_revenue = sorted(yearly_revenue.items(), key=lambda x: x[1])
for year in reversed(sorted_yearly_revenue):
print(f" {year[0]} - {year[1]}")
def enrich_by_month(revenue_dict):
# dictionary of lists to hold key-value pairs by month
revenue_by_month = {month: [] for month in range(1, 13)}
# dictionary to convert month numbers to their respective names
month_int_to_str = {
1: "january",
2: "february",
3: "march",
4: "april",
5: "may",
6: "june",
7: "july",
8: "august",
9: "september",
10: "october",
11: "november",
12: "december",
}
# "for every key in the dictionary"
for i in revenue_dict:
month_year = i.split("-")
revenue_by_month[int(month_year[0])].append(i)
# now, add up the revenue for each month
monthly_revenue = {month: round(sum(revenue_dict[key] for key in revenue_by_month[month]), 2) for month in range(1, 13)}
most_profitable_month = max(monthly_revenue, key=lambda key: monthly_revenue[key])
print(
f"max monthly revenue was in {month_int_to_str[most_profitable_month]}: {max(monthly_revenue.values())}\n"
)
print("monthly revenue rankings (descending order):")
sorted_monthly_revenue = sorted(monthly_revenue.items(), key=lambda x: x[1])
for month in reversed(sorted_monthly_revenue):
print(f" {month_int_to_str[month[0]]} - {month[1]}")
def main():
# read in output file from the command line
try:
file_object = open(sys.argv[1], "r+")
except IndexError:
print("Usage: python3 " + os.path.basename(__file__) + " path/to/output_file")
sys.exit(1)
print("sorted hadoop output file: revenue by month/year")
print("------------------------------------------------")
revenue_dict = enrich_by_month_year(file_object)
print()
file_object.close()
#######################################
# start of extra credit opportunities #
#######################################
# extra credit 1: enriching data set (sorting by season)
print("data enrichment #1: enrich data by season")
print("-----------------------------------------")
enrich_by_season(revenue_dict)
print()
# extra credit 2: enriching data set (sorting by year)
print("data enrichment #2: enrich data by year")
print("---------------------------------------")
enrich_by_year(revenue_dict)
print()
# extra credit 3: enriching data set (sort by month)
print("data enrichment #3: enrich data by month")
print("----------------------------------------")
enrich_by_month(revenue_dict)
if __name__ == "__main__":
main()