Functional Python Programming

(Wang) #1
Chapter 6

It's important to note that this function is similar to other examples in that it actually
returns a function rather than a value. This is one of the defining characteristics of
higher-order functions, and is pleasantly simple to implement in Python.


Writing file parsers


We can often consider a file parser to be a kind of reduction. Many languages have
two levels of definition: the lower-level tokens in the language and the higher-level
structures built from those tokens. When looking at an XML file, the tags, tag names,
and attribute names form this lower-level syntax; the structures which are described
by XML form a higher-level syntax.


The lower-level lexical scanning is a kind of reduction that takes individual
characters and groups them into tokens. This fits well with Python's generator
function design pattern. We can often write functions that look as follows:


Def lexical_scan( some_source ):


for char in some_source:


if some_pattern completed: yield token


else: accumulate token


For our purposes, we'll rely on lower-level file parsers to handle this for us. We'll use
the CSV, JSON, and XML packages to manage these details. We'll write higher-level
parsers based on these packages.


We'll still rely on a two-level design pattern. A lower-level parser will produce a
useful canonical representation of the raw data. It will be an iterator over tuples
of text. This is compatible with many kinds of data files. The higher-level parser
will produce objects useful for our specific application. These might be tuples of
numbers, or namedtuples, or perhaps some other class of immutable Python objects.


We provided one example of a lower-level parser in Chapter 4, Working with
Collections. The input was a KML file; KML is an XML representation of geographic
information. The essential features of the parser look similar to the following
command snippet:


def comma_split(text):


return text.split(",")


def row_iter_kml(file_obj):


ns_map={


"ns0": "http://www.opengis.net/kml/2.2",


"ns1": "http://www.google.com/kml/ext/2.2"}


doc= XML.parse(file_obj)

Free download pdf