Functional Python Programming

(Wang) #1

Working with Collections


We can use this function as follows:


def lat_lon_kml(row_iter):
return (pick_lat_lon(*row) for row in row_iter)


This function will apply the pick_lat_lon() function to each row. We've used
*row to assign each element of the row three tuple to separate parameters of the
pick_lat_lon() function. The function can then extract and reorder the two
relevant values from each three tuple.


It's important to note that a good functional design allows us to freely replace any
function with its equivalent, which makes refactoring quite simple. We've tried
to achieve this goal when we provide alternative implementations of the various
functions. In principle, a clever functional language compiler might do some
replacements as part of an optimization pass.


We'll use the following kind of processing to parse the file and build a structure
we can use, such as the following code snippet:


with urllib.request.urlopen("file:./Winter%202012-2013.kml") as
source:
v1= tuple(lat_lon_kml(row_iter_kml(source)))
print(v1)


We've used the urllib command to open a source. In this case, it's a local file.
However, we can also open a KML file on a remote server. Our objective with
using this kind of file opening is to assure that our processing is uniform no
matter what the source of the data is.


We've shown the two functions that do low-level parsing of the KML source.
The row_iter_kml(source) expression produces a sequence of text columns.
The lat_lon_kml() function will extract and reorder the latitude and longitude
values. This creates an intermediate result that sets the stage for further processing.
The subsequent processing is independent of the original format.


When we run this, we see results like the following:


(('37.54901619777347', '-76.33029518659048'),
('37.840832', '-76.27383399999999'), ('38.331501', '-76.459503'),
('38.330166', '-76.458504'), ('38.976334', '-76.47350299999999'))


We've extracted just the latitude and longitude values from a complex XML
file using an almost purely functional approach. As the result is an iterable, we can
continue to use functional programming techniques to process each point that we
retrieve from the file.

Free download pdf