Functional Python Programming

(Wang) #1

Recursions and Reductions


Following is the conversion:


def float_none(data):


try:


data_f= float(data)


return data_f


except ValueError:


return None


This function handles the conversion of a single string to float values, converting
bad data to None value. We can embed this function in a mapping so that we convert
all columns of a row to a float or None value. The lambda looks as follows:


float_row = lambda row: list(map(float_none, row))


Following is a row-level validator based on the use of the all() function to assure
that all values are float (or none of the values are None):


all_numeric = lambda row: all(row) and len(row) == 8


Following is a higher-order function which combines the row-level conversion
and filtering:


def head_filter_map(validator, converter, validator, row_iter):


return filter(all_validator, map(converter, row_iter))


This function gives us a slightly more complete pattern for parsing an input file.
The foundation is a lower-level function that iterates over tuples of text. We can
then wrap this in functions to convert and validate the converted data. For the
cases where files are either in first normal form (all rows are the same) or a simple
validator can reject the other rows, this design works out nicely.


All parsing problems aren't quite this simple, however. Some files have important
data in header or trailer rows that must be preserved, even though it doesn't match
the format of the rest of the file. These non-normalized files will require a more
sophisticated parser design.


Parsing plain text files with headers

In Chapter 3, Functions, Iterators, and Generators, the Crayola.GPL file was presented
without showing the parser. This file looks as follows:


GIMP Palette
Name: Crayola
Columns: 16
Free download pdf