Package translate :: Package filters :: Module helpers

[hide private]
[frames] | no frames]

# Source Code for Module translate.filters.helpers

     1  #!/usr/bin/env python
     2  # -*- coding: utf-8 -*-
     3  #
     4  # Copyright 2004-2006 Zuza Software Foundation
     5  #
     6  # This file is part of translate.
     7  #
     8  # translate is free software; you can redistribute it and/or modify
     9  # it under the terms of the GNU General Public License as published by
    10  # the Free Software Foundation; either version 2 of the License, or
    11  # (at your option) any later version.
    12  #
    13  # translate is distributed in the hope that it will be useful,
    14  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  # GNU General Public License for more details.
    17  #
    18  # You should have received a copy of the GNU General Public License
    19  # along with translate; if not, write to the Free Software
    20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    21
    22  """a set of helper functions for filters..."""
    23
    24  import operator
    25
    26 -def countmatch(str1, str2, countstr):
    27      """checks whether countstr occurs the same number of times in str1 and str2"""
    28      return str1.count(countstr) == str2.count(countstr)
    29
    30 -def funcmatch(str1, str2, func, *args):
    31      """returns whether the result of func is the same for str1 and str2"""
    32      return func(str1, *args) == func(str2, *args)
    33
    34 -def countsmatch(str1, str2, countlist):
    35      """checks whether each element in countlist occurs the same number of times in str1 and str2"""
    36      return reduce(operator.and_, [countmatch(str1, str2, countstr) for countstr in countlist], True)
    37
    38 -def funcsmatch(str1, str2, funclist):
    39      """checks whether the results of each func in funclist match for str1 and str2"""
    40      return reduce(operator.and_, [funcmatch(str1, str2, funcstr) for funcstr in funclist], True)
    41
    42 -def filtercount(str1, func):
    43      """returns the number of characters in str1 that pass func"""
    44      return len(filter(func, str1))
    45
    46 -def filtertestmethod(testmethod, strfilter):
    47      """returns a version of the testmethod that operates on filtered strings using strfilter"""
    48      def filteredmethod(str1, str2):
    49          return testmethod(strfilter(str1), strfilter(str2))
    50      filteredmethod.__doc__ = testmethod.__doc__
    51      filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
    52      return filteredmethod
    53
    54 -def multifilter(str1, strfilters, *args):
    55      """passes str1 through a list of filters"""
    56      for strfilter in strfilters:
    57          str1 = strfilter(str1, *args)
    58      return str1
    59
    60 -def multifiltertestmethod(testmethod, strfilters):
    61      """returns a version of the testmethod that operates on filtered strings using strfilter"""
    62      def filteredmethod(str1, str2):
    63          return testmethod(multifilter(str1, strfilters), multifilter(str2, strfilters))
    64      filteredmethod.__doc__ = testmethod.__doc__
    65      filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
    66      return filteredmethod
    67