Functional Python Programming

(Wang) #1

The Multiprocessing and Threading Modules


if not any(path):
continue
if any(p in name_exclude for p in path):
continue
final= path[-1]
if any(final.endswith(ext) for ext in ext_exclude):
continue
yield detail


For each individual AccessDetails object, we'll apply three filter tests. If the path
is essentially empty or the part includes one of the excluded names or the path's
final name has an excluded extension, the item is quietly ignored. If the path doesn't
match any of these criteria, it's potentially interesting and is part of the results
yielded by the path_filter() function.


This is an optimization because all of the tests are applied using an imperative style
for loop body.


The design started with each test as a separate first-class filter-style function.
For example, we might have a function like the following to handle empty paths:


def non_empty_path(detail):
path = detail.url.path.split('/')
return any(path)


This function simply assures that the path contains a name. We can use the filter()
function as follows:


filter(non_empty_path, access_details_iter)


We can write similar tests for the non_excluded_names() and non_excluded_ext()
functions. The entire sequence of filter() functions will look as follows:


filter(non_excluded_ext,
filter(non_excluded_names,
filter(non_empty_path, access_details_iter)))


This applies each filter() function to the results of the previous filter()
function. The empty paths are rejected; from this subset, the excluded names and the
excluded extensions are rejected. We can also state the preceding example as a series
of assignment statements as follows:


ne= filter(non_empty_path, access_details_iter)
nx_name= filter(non_excluded_names, ne)
nx_ext= filter(non_excluded_ext, nx_name)
return nx_ext

Free download pdf