/historical/filtertools.py
Python | 65 lines | 9 code | 6 blank | 50 comment | 2 complexity | 452c1d1f95136d3be67a7b18ace9a804 MD5 | raw file
1# 2# $LicenseInfo:firstyear=2010&license=mit$ 3# 4# Copyright (c) 2010, Linden Research, Inc. 5# 6# Permission is hereby granted, free of charge, to any person obtaining a copy 7# of this software and associated documentation files (the "Software"), to deal 8# in the Software without restriction, including without limitation the rights 9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10# copies of the Software, and to permit persons to whom the Software is 11# furnished to do so, subject to the following conditions: 12# 13# The above copyright notice and this permission notice shall be included in 14# all copies or substantial portions of the Software. 15# 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22# THE SOFTWARE. 23# $/LicenseInfo$ 24# 25 26"""filtertools - Filter utilities 27 28Functions: 29filterthru - Transform a sequence through a stack of filters 30 31""" 32 33__all__ = [ 34 'filterthru', 35 ] 36 37 38def _append_lists(a, b): 39 return a + b 40 41def filterthru(data, stack): 42 """Return a sequence transformed through a stack of filters 43 44 The data argument is a list of zero or more items. 45 46 The stack argument is a list of zero or more filters. Each filter is a 47 function that should take a single data item, and return a list of zero 48 or more data items. Filters may simply transform the data, returning a 49 single valued list, may add values by returning multiple valued list, or 50 eliminate an item from further processing by return an empty list. 51 52 For each filter in the stack, this function takes each item in the data 53 and passes it through the filter, concatenating the results. This new, 54 transformed data list becomes the input for processing with the next filter 55 in the stack. The final transformed list is the result. 56 57 Note: The order of the results is maintained when concatenating. 58 Note: If at any stage, there resulting list is empty, the final result 59 will be empty. 60 61 """ 62 63 for filter in stack: 64 data = reduce(_append_lists, map(filter, data), []) 65 return data