4 # Copyright (C) 2010 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Module containing backported language/library functionality.
39 # compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
40 # sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
41 # went on. compat.sha1 is python-version specific and is used for python
42 # modules (hmac, for example) which have changed their behavior as well from
43 # one version to the other.
45 # Yes, we're not using the imports in this module.
46 # pylint: disable-msg=W0611
47 from hashlib import md5 as md5_hash
48 from hashlib import sha1 as sha1_hash
49 # this additional version is needed for compatibility with the hmac module
52 from md5 import new as md5_hash
58 def all(seq, pred=bool): # pylint: disable-msg=W0622
59 """Returns True if pred(x) is True for every element in the iterable.
61 Please note that this function provides a C{pred} parameter which isn't
62 available in the version included in Python 2.5 and above.
65 for _ in itertools.ifilterfalse(pred, seq):
70 def any(seq, pred=bool): # pylint: disable-msg=W0622
71 """Returns True if pred(x) is True for at least one element in the iterable.
73 Please note that this function provides a C{pred} parameter which isn't
74 available in the version included in Python 2.5 and above.
77 for _ in itertools.ifilter(pred, seq):
82 def partition(seq, pred=bool): # pylint: disable-msg=W0622
83 """Partition a list in two, based on the given predicate.
86 return (list(itertools.ifilter(pred, seq)),
87 list(itertools.ifilterfalse(pred, seq)))
90 # Even though we're using Python's built-in "partial" function if available,
91 # this one is always defined for testing.
92 def _partial(func, *args, **keywords): # pylint: disable-msg=W0622
93 """Decorator with partial application of arguments and keywords.
95 This function was copied from Python's documentation.
98 def newfunc(*fargs, **fkeywords):
99 newkeywords = keywords.copy()
100 newkeywords.update(fkeywords)
101 return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142
105 newfunc.keywords = keywords
109 def TryToRoman(val, convert=True):
110 """Try to convert a value to roman numerals
112 If the roman module could be loaded convert the given value to a roman
113 numeral. Gracefully fail back to leaving the value untouched.
116 @param val: value to convert
117 @type convert: boolean
118 @param convert: if False, don't try conversion at all
119 @rtype: string or typeof(val)
120 @return: roman numeral for val, or val if conversion didn't succeed
123 if roman is not None and convert:
125 return roman.toRoman(val)
126 except roman.RomanError:
132 if functools is None:
135 partial = functools.partial