Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/translate-toolkit-1.3.0/translate/filters/helpers.py
blob: 857d73b5b0d5624765a9e057e1c0ce786d20eed2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 
# Copyright 2004-2006 Zuza Software Foundation
# 
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with translate; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""a set of helper functions for filters..."""

import operator

def countmatch(str1, str2, countstr):
    """checks whether countstr occurs the same number of times in str1 and str2"""
    return str1.count(countstr) == str2.count(countstr)

def funcmatch(str1, str2, func, *args):
    """returns whether the result of func is the same for str1 and str2"""
    return func(str1, *args) == func(str2, *args)

def countsmatch(str1, str2, countlist):
    """checks whether each element in countlist occurs the same number of times in str1 and str2"""
    return reduce(operator.and_, [countmatch(str1, str2, countstr) for countstr in countlist], True)

def funcsmatch(str1, str2, funclist):
    """checks whether the results of each func in funclist match for str1 and str2"""
    return reduce(operator.and_, [funcmatch(str1, str2, funcstr) for funcstr in funclist], True)

def filtercount(str1, func):
    """returns the number of characters in str1 that pass func"""
    return len(filter(func, str1))

def filtertestmethod(testmethod, strfilter):
    """returns a version of the testmethod that operates on filtered strings using strfilter"""
    def filteredmethod(str1, str2):
        return testmethod(strfilter(str1), strfilter(str2))
    filteredmethod.__doc__ = testmethod.__doc__
    filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
    return filteredmethod

def multifilter(str1, strfilters, *args):
    """passes str1 through a list of filters"""
    for strfilter in strfilters:
        str1 = strfilter(str1, *args)
    return str1

def multifiltertestmethod(testmethod, strfilters):
    """returns a version of the testmethod that operates on filtered strings using strfilter"""
    def filteredmethod(str1, str2):
        return testmethod(multifilter(str1, strfilters), multifilter(str2, strfilters))
    filteredmethod.__doc__ = testmethod.__doc__
    filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__)
    return filteredmethod