-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf2016_cs8_jzc22_fp.py
251 lines (215 loc) · 7.96 KB
/
f2016_cs8_jzc22_fp.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
242
243
244
245
246
247
248
249
250
251
import os
import string
#
#
# name : Jessica Chen
# email : [email protected]
# date : 12/8/2016
# class : CS0008-f2016
# instructor : Max Novelli
#
# Description:
# Final Project
#
# This program reads the master input file which contains list of data files.
# It processes each data file to extract runners and his/her running distance.
# The extracted data are stored in lists and dictionaries which are processed
# to produce the required results.
# Defines a participant.
class participant:
# initializer method. set name and initial distance if provided. If initial distance is not specified, it should be
# set to zero
def __init__(self, name, distance=0):
# set name
self.name = name
# set distance if non zero
if distance > 0:
# set distance
self.distance = distance
# set number of runs accordingly
self.runs = 1
else:
# set distance
self.distance = 0
# set number of runs
self.runs = 0
# add single distance to the distance accumulator and increments runs by 1. Argument d is a single float.
def addDistance(self, distance):
if distance > 0:
self.distance += distance
self.runs += 1
# end if
# add an array of distances to distance accumulator. Argument 1d is a list of floats
def addDistances(self, distances):
# loops over list
for distance in distances:
if distance > 0:
self.distance += distance
self.runs += 1
# get the name of the participant of the current instance
def getName(self):
return self.name
# get the current value of the distance accumulator.
def getDistance(self):
return self.distance
# Get number of runs
def getRuns(self):
return self.runs
# Return a string with name, total distance run, and how many distances the object added
def __str__(self):
return \
"Name : " + format(self.name, '>20s') + \
". Distance run : " + format(self.distance, '9.4f') + \
". Runs : " + format(self.runs, '4d')
# convert to csv
def tocsv(self):
return ','.join([self.name, str(self.runs), str(self.distance)])
# This function accepts one argument: the master input file handle.
# It reads all data files and process each data file.
# Data file processed will be stored in data_file_list.
# Information read from data files will be stored in participants_dict.
#
# Arguments:
# fh - master file handle
# data_file_list - list to store data file names.
# participants_dict - name to participant dictionary.
def processMasterFile(fh, data_file_list, participants_dict):
for line in fh:
try:
# Strip the \n to get data file name
data_file = line.rstrip()
# Add to data_file_list
data_file_list.append(data_file)
# Process the data file
if os.path.isfile(data_file):
data_fh = open(data_file, 'r')
processDataFile(data_fh, participants_dict)
data_fh.close
except ValueError:
pass
# This function reads data line by line from a file to extract
# runner name and his/her distance. Extracted data are used to update
# the participants_dict.
#
# Arguments:
# fh - the data file handle
# participants_dict - name to participant dictionary.
def processDataFile(fh, participants_dict):
for line in fh:
try:
# Strip the \n
line = line.rstrip()
# Split the names and distances (string and float)
stt = line.split(",")
name = stt[0].strip() # strip to trim leading and trailing space
distance = float(stt[1])
# Update the participant dictionary
if name in participants_dict:
participants_dict[name].addDistance(distance)
else:
participants_dict[name] = participant(name, distance)
except ValueError:
pass
# This function accepts 2 mandatory arguments: key and value and an optional third klen (key length). If klen is not
# passed when called, it defaults to 0. It prints the key and value with desired format.
#
# Arguments:
# key - key string.
# value - value object, this can be int, float, string.
# klen - optional key length, default is 0.
def printKV(key, value, klen=0):
kn = len(key)
space = klen
# Get the max space for key
if kn > klen:
space = kn
# Format of value changes according to the type of the value contained in the variable
value_str = ''
if isinstance(value, int):
value_str = format(value, '10d')
elif isinstance(value, float):
value_str = format(value, '10.3f')
else:
value_str = format(value, '30s')
format_str = '{0:' + str(space) + 's} : {1:s}'
print(format_str.format(key, value_str))
def main():
# The master input file
master_input_list_file = 'f2016_cs8_fp.data.txt'
# The output file that contains runners, records in data files and the total distance run
output_file = 'f2016_cs8_jzc22_fp.data.output.csv'
# List to store all data files (read from master input list)
data_file_list = []
# Map from name to participant object
participants_dict = {}
##################################
# Process files
##################################
if os.path.isfile(master_input_list_file):
fh = open(master_input_list_file, 'r')
processMasterFile(fh, data_file_list, participants_dict)
fh.close
##################################
# Process data
##################################
# Total number of data files
number_of_files = len(data_file_list)
# Total number of lines
total_number_of_lines = 0
# Total number of runners
total_number_of_runner = 0
# Total distance run by all runners
total_distance = 0.0
# Max distance run by a runner
max_distance_run = 0.0
# The runner with max distance
max_distance_runner = ''
# Min distance run by a runner
min_distance_run = 9999999.0
# The runner with min distance
min_distance_runner = ''
# The number of runners with multiple records
number_of_runner_with_multiple_records = 0
output_fh = open(output_file, 'w')
output_fh.write('name,records,distance\n')
for runner, participant in participants_dict.items():
distance = participant.getDistance()
runs = participant.getRuns()
total_distance += distance
total_number_of_lines += runs
total_number_of_runner += 1
if distance > max_distance_run:
max_distance_run = distance
max_distance_runner = runner
if distance < min_distance_run:
min_distance_run = distance
min_distance_runner = runner
if runs > 1:
number_of_runner_with_multiple_records += 1
output_fh.write(participant.tocsv() + "\n\r")
output_fh.close()
##################################
# Print results
##################################
klen = 30
# Print out the number of input files
printKV('Number of Input files read', number_of_files, klen)
# Print out total number of lines read
printKV('Total number of lines read', total_number_of_lines, klen)
print('')
# Print out total distance run
printKV('total distance run', total_distance, klen)
print('')
printKV('max distance run', max_distance_run, klen)
printKV('by participant', max_distance_runner, klen)
print('')
printKV('min distance run', min_distance_run, klen)
printKV('by participant', min_distance_runner, klen)
print('')
printKV('Total number of participants', total_number_of_runner, klen)
print('')
print('Total number of participants ')
printKV('with multiple records', number_of_runner_with_multiple_records, klen)
print('')
# use the main() function
main()