Page 222 -
P. 222
reviews are in
You were to take the time to apply the code review comments to your
code. There were four suggestions that you needed to adjust your code
to support:
1. Create the dictionary all in one go.
2. Move the dictionary creation code into the get_coach_data()
function, returning a dictionary as opposed to a list.
3. Move the code that determines the top three times for each athlete
into the get_coach_data() function.
4. Adjust the invocations within the main code to the new version of the
get_coach_data() function to support its new mode of operation.
You were to grab your pencil and write your new get_coach_data()
function in the space provided below, as well as provide the four calls
that you’d make to process the data for each of the athletes and provide
four amended print() statements:
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
1. Create a temporary templ = data.strip().split(‘,’)
list to hold the data
BEFORE creating the return({‘Name’ : templ.pop(0), 2. The dictionary creation code is
now part of the function.
dictionary all in one go. ‘DOB’ : templ.pop(0),
‘Times’: str(sorted(set([sanitize(t) for t in templ]))[0:3])})
except IOError as ioerr: 3. The code that determines the
print(‘File error: ‘ + str(ioerr)) top three scores is part of the
function, too.
return(None)
4. Call the function We are showing only these two lines of
for an athlete and james = get_coach_data(‘james2.txt’) code for one athlete (because repeating it
adjust the “print()” for the other three is a trivial exercise).
statement as needed.
print(james[‘Name’] + “’s fastest times are: “ + james[‘Times’])
186 Chapter 6