PageRenderTime 27ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/historical/filtertools.py

https://bitbucket.org/lindenlab/apiary/
Python | 65 lines | 9 code | 6 blank | 50 comment | 1 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. """filtertools - Filter utilities
  26. Functions:
  27. filterthru - Transform a sequence through a stack of filters
  28. """
  29. __all__ = [
  30. 'filterthru',
  31. ]
  32. def _append_lists(a, b):
  33. return a + b
  34. def filterthru(data, stack):
  35. """Return a sequence transformed through a stack of filters
  36. The data argument is a list of zero or more items.
  37. The stack argument is a list of zero or more filters. Each filter is a
  38. function that should take a single data item, and return a list of zero
  39. or more data items. Filters may simply transform the data, returning a
  40. single valued list, may add values by returning multiple valued list, or
  41. eliminate an item from further processing by return an empty list.
  42. For each filter in the stack, this function takes each item in the data
  43. and passes it through the filter, concatenating the results. This new,
  44. transformed data list becomes the input for processing with the next filter
  45. in the stack. The final transformed list is the result.
  46. Note: The order of the results is maintained when concatenating.
  47. Note: If at any stage, there resulting list is empty, the final result
  48. will be empty.
  49. """
  50. for filter in stack:
  51. data = reduce(_append_lists, map(filter, data), [])
  52. return data