Return a data structure from a function

Processing one line of surfer data was pretty straightforward. But now you have to work with all the lines of data in the file. Your program has to make the data available quickly so that a request to display the details of a particular surfer can be performed as soon as possible.

You already know enough to write a function that takes the surfer ID as a parameter, searches the file one line at a time for a matching ID, and then returns the found data to the caller:

There are really only two choices for how you return data from this function. Pass back the surfer's data either:

As a string

As a hash or

But which? Returning a string requires the calling code to further process the data to extract the information it needs, which (although possible) gets messy, because the calling code is then required to cut up the string using split () . This is something best left to the function, because it hides the complexity of manipulating the data from the calling code. Returning a hash allows the calling code to simply pick out the information it needs without too much fuss and without any further processing.

Return a hash from the function to keep the calling code simple.

Code Magnets

Rearrange the code magnets to complete the function required. The following functionality is implemented:

1. Accept a single parameter (the surfer ID).

2. Process the data file one line at a time, creating a hash from the line on each iteration.

3. Compare the parameter against the ID read from the file.

4. If the IDs match, return the hash to the caller.

5. If no match is found, return an empty hash to the caller.

def find_details(id2find):

for each line in surfers f:

surfers_f.close()

surfers_f.close()

return(s)

(s['id'], s['name'], s['country'], s['average'], s['board'], I I if id2find == int(sr-id.n L s [ ' age' ] ) = each_line.split(";")_I 1 ( [ ] ) : f process the parameter

Code Magnets Solution

You were to rearrange the code magnets to complete the function required. The following functionality is implemented:

1. Accept a single parameter (the surfer ID).

2. Process the data file one line at a time, creating a hash from the line on each iteration.

3. Compare the parameter against the ID read from the file.

4. If the IDs match, return the hash to the caller.

5. If no match is found, return an empty hash to the caller.

def find details(id2find):

Open the f 'le so that Yow tan read -the data-

^ "U" -to '00P through the |ihes "> the file.

def find details(id2find):

for each line in surfers_f:

for each line in surfers_f:

Make sure the hash starts out empty-

Check if the ID supplied as a parametev is the same as the one read from the file.

(s['id'], s['name'], s['country'], s['average'], s['board'], s['age']) = each_line.split(";")

surfers_f.close()

You have a match.' So, close the file then return the Current hash to the caller.

Cut up the line (using split()) and assign the data to the hash (using multiple-assignment).

return(s)

surfers f.close()

return(s)

surfers f.close()

Y<* pressed the entire +' e but tound HO MATCH. Close the file and return an e«*pty hash.

Load this!

Download surfing_data . csv from the Head First Programming website before continuing. Be sure to put the file in the same directory/folder as your code.

Reaps Bake

Reaps Bake

Here's some tester code that we've cooked up especially for you. This code will let you test your function:

Ask the user for the surfer ID t0 find. ^ lookup_id = int(input("Enter the id of the surfer:

Call wfind_details()

on the entered surfer ID if surfer:

If data was found, display the nicely formatted messages print("ID: print("Name: print("Country: print("Average: print("Board type: print("Age:

+ surfer['id']) + surfer['name']) + surfer['country']) + surfer['average' ] ) + surfer['board']) + surfer['age'])

Was this article helpful?

0 0

Post a comment