forked from kiat/Elements-of-Software-Design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_025_python_profiling_parts.py
173 lines (126 loc) · 4.2 KB
/
example_025_python_profiling_parts.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
import sys
import argparse
# import numpy as np
import cProfile
import pstats
import io
class Part:
# 'PARTKEY', 'NAME', 'MFGR', 'BRAND', 'TYPE', 'SIZE', 'CONTAINER', 'RETAILPRICE', 'COMMENT'
def __init__(self, **kwargs):
self.partkey = kwargs.get('partkey', "0")
self.name = kwargs.get('name', "0")
self.mfgr = kwargs.get('mfgr', None)
self.brand = kwargs.get('brand', None)
self.type = kwargs.get('type', "")
self.size = kwargs.get('size', "0")
self.container = kwargs.get('container', "")
self.retailprice = kwargs.get('retailprice', "0")
self.comment = kwargs.get('comment', "")
def __str__(self):
return "CUSTOMER : ("+str(self.partkey) + " - " + self.name + " - " + \
self.mfgr + " - " + self.brand + " - " + self.type + " - " + \
str(self.size) + " - " + self.container + " - " + \
str(self.retailprice) + " - " + self.comment + ")"
def __eq__(self, other):
return self.partkey == other.partkey
def read_lines(filename):
'''This functions reads each line of a file and returns a list of string lines.'''
file = open(filename, 'r')
lines = []
count = 0
while True:
count += 1
# Get next line from file
line = file.readline()
# if line is empty end of file is reached
if not line:
break
else:
lines.append(line)
return lines
def parse_lines(lines):
parsed_values = []
for line in lines:
values = line.split("|")
parsed_values.append(values)
return parsed_values
def convert_toNumbers(parsed_values):
'''Drop the first line which is the header and convert the remaining lines'''
values = []
count = 0
for i in range(1, len(parsed_values)):
values.append(
[int(parsed_values[i][0]), parsed_values[i][1], parsed_values[i][2],
parsed_values[i][3], parsed_values[i][4], int(
parsed_values[i][5]),
parsed_values[i][6], float(parsed_values[i][7]), parsed_values[i][8]])
i += 1
return values
def convert_to_parts(values):
''' Find the max in a Part
'''
parts = []
for item in values:
p = Part(partkey=item[0],
name=item[1],
mfgr=item[2],
brand=item[3],
type=item[4],
size=item[5],
container=item[6],
retailprice=item[7],
comment=item[8]
)
parts.append(p)
return parts
def find_max_price(parts):
''' Find the max in a Part List '''
max_retailprice = 0.0
max_part = None
# We do linear search in the list
for part in parts:
if(part.retailprice > max_retailprice):
max_retailprice = part.retailprice
max_part = part
return max_part
def find_duplicates(parts):
duplicates=[]
while parts:
part = parts.pop()
if part in parts:
duplicates.append(part)
return duplicates
def profile(fnc):
"""A decorator that uses cProfile to profile a function"""
def inner(*args, **kwargs):
pr = cProfile.Profile()
pr.enable()
retval = fnc(*args, **kwargs)
pr.disable()
s = io.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())
return retval
return inner
@profile
def main():
# filename = sys.stdin.read()
parser = argparse.ArgumentParser(description='Example Program')
parser.add_argument('--filename', type=str, default='./datasets/part.tbl',
help='use the correct input file.')
args = parser.parse_args()
lines = read_lines(args.filename)
values = parse_lines(lines)
converted_results = convert_toNumbers(values)
# print(converted_results[0])
parts = convert_to_parts(converted_results)
# print(parts[0].retailprice)
max_part = find_max_price(parts)
print(max_part)
duplicates = find_duplicates(parts)
for part in duplicates:
print(part)
if __name__ == "__main__":
main()