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 isalnum_u(s):
27 """checks whether a string is all alphanumeric, allowing any unicode characters >= 0x80 to pass the test""" 28 return s.isalnum() or reduce(operator.and_, [c.isalnum() or c >= "\x80" for c in s], True)
29
30 -def countmatch(str1, str2, countstr):
31 """checks whether countstr occurs the same number of times in str1 and str2""" 32 return str1.count(countstr) == str2.count(countstr)
33
34 -def funcmatch(str1, str2, func, *args):
35 """returns whether the result of func is the same for str1 and str2""" 36 return func(str1, *args) == func(str2, *args)
37
38 -def countsmatch(str1, str2, countlist):
39 """checks whether each element in countlist occurs the same number of times in str1 and str2""" 40 return reduce(operator.and_, [countmatch(str1, str2, countstr) for countstr in countlist], True)
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 -def filtercount(str1, func):
47 """returns the number of characters in str1 that pass func""" 48 return len(filter(func, str1))
49
50 -def filtertestmethod(testmethod, strfilter):
51 """returns a version of the testmethod that operates on filtered strings using strfilter""" 52 def filteredmethod(str1, str2): 53 return testmethod(strfilter(str1), strfilter(str2))
54 filteredmethod.__doc__ = testmethod.__doc__ 55 filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__) 56 return filteredmethod 57
58 -def multifilter(str1, strfilters):
59 """passes str1 through a list of filters""" 60 for strfilter in strfilters: 61 str1 = strfilter(str1) 62 return str1
63
64 -def multifiltertestmethod(testmethod, strfilters):
65 """returns a version of the testmethod that operates on filtered strings using strfilter""" 66 def filteredmethod(str1, str2): 67 return testmethod(multifilter(str1, strfilters), multifilter(str2, strfilters))
68 filteredmethod.__doc__ = testmethod.__doc__ 69 filteredmethod.name = getattr(testmethod, 'name', testmethod.__name__) 70 return filteredmethod 71