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
    27 -def countmatch(str1, str2, countstr):
    28      """checks whether countstr occurs the same number of times in str1 and str2"""
    29      return str1.count(countstr) == str2.count(countstr)
    30
    31
    32 -def funcmatch(str1, str2, func, *args):
    33      """returns whether the result of func is the same for str1 and str2"""
    34      return func(str1, *args) == func(str2, *args)
    35
    36
    37 -def countsmatch(str1, str2, countlist):
    38      """checks whether each element in countlist occurs the same number of times in str1 and str2"""
    39      return reduce(operator.and_, [countmatch(str1, str2, countstr) for countstr in countlist], True)
    40
    41
    42 -def funcsmatch(str1, str2, funclist):
    43      """checks whether the results of each func in funclist match for str1 and str2"""
    44      return reduce(operator.and_, [funcmatch(str1, str2, funcstr) for funcstr in funclist], True)
    45
    46
    47 -def filtercount(str1, func):
    48      """returns the number of characters in str1 that pass func"""
    49      return len(filter(func, str1))
    50
    51
    52 -def filtertestmethod(testmethod, strfilter):
    53      """returns a version of the testmethod that operates on filtered strings using strfilter"""
    54
    55      def filteredmethod(str1, str2):
    56          return testmethod(strfilter(str1), strfilter(str2))
    57      filteredmethod.__doc__ = testmethod.__doc__
    58      filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
    59      return filteredmethod
    60
    61
    62 -def multifilter(str1, strfilters, *args):
    63      """passes str1 through a list of filters"""
    64      for strfilter in strfilters:
    65          str1 = strfilter(str1, *args)
    66      return str1
    67
    68
    69 -def multifiltertestmethod(testmethod, strfilters):
    70      """returns a version of the testmethod that operates on filtered strings using strfilter"""
    71
    72      def filteredmethod(str1, str2):
    73          return testmethod(multifilter(str1, strfilters), multifilter(str2, strfilters))
    74      filteredmethod.__doc__ = testmethod.__doc__
    75      filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
    76      return filteredmethod
    77