Introduction
Objectives:
- Higher order functions
Files Modified: reader.py
Objectives:
Files Modified: reader.py
At the moment, the reader.py
program consists of two core functions, csv_as_dicts()
and csv_as_instances()
. The code in these two functions is almost identical. For example:
def csv_as_dicts(lines, types, *, headers=None):
'''
Convert lines of CSV data into a list of dictionaries
'''
records = []
rows = csv.reader(lines)
if headers is None:
headers = next(rows)
for row in rows:
record = { name: func(val)
for name, func, val in zip(headers, types, row) }
records.append(record)
return records
def csv_as_instances(lines, cls, *, headers=None):
'''
Convert lines of CSV data into a list of instances
'''
records = []
rows = csv.reader(lines)
if headers is None:
headers = next(rows)
for row in rows:
record = cls.from_row(row)
records.append(record)
return records
Unify the core of these functions into a single function convert_csv()
that accepts a user-defined conversion function as an argument. For example:
>>> def make_dict(headers, row):
return dict(zip(headers, row))
>>> lines = open('portfolio.csv')
>>> convert_csv(lines, make_dict)
[{'name': 'AA', 'shares': '100', 'price': '32.20'}, {'name': 'IBM', 'shares': '50', 'price': '91.10'},
{'name': 'CAT', 'shares': '150', 'price': '83.44'}, {'name': 'MSFT', 'shares': '200', 'price': '51.23'},
{'name': 'GE', 'shares': '95', 'price': '40.37'}, {'name': 'MSFT', 'shares': '50', 'price': '65.10'},
{'name': 'IBM', 'shares': '100', 'price': '70.44'}]
>>>
Rewrite the csv_as_dicts()
and csv_as_instances()
functions in terms of the new convert_csv()
function.
One of the most common operations in functional programming is the map()
operation that maps a function to the values in a sequence. Python has a built-in map()
function that does this. For example:
>>> nums = [1,2,3,4]
>>> squares = map(lambda x: x*x, nums)
>>> for n in squares:
print(n)
1
4
9
16
>>>
map()
produces an iterator so if you want a list, you'll need to create it explicitly:
>>> squares = list(map(lambda x: x*x, nums))
>>> squares
[1, 4, 9, 16]
>>>
Try to use map()
in your convert_csv()
function.
Congratulations! You have completed the Higher Order Functions lab. You can practice more labs in LabEx to improve your skills.