4 # Copyright (C) 2010, 2011 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.
30 # pylint: disable=F0401
36 # pylint: disable=F0401
42 # compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
43 # sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
44 # went on. compat.sha1 is python-version specific and is used for python
45 # modules (hmac, for example) which have changed their behavior as well from
46 # one version to the other.
48 # Yes, these don't always exist, that's why we're testing
49 # Yes, we're not using the imports in this module.
50 from hashlib import md5 as md5_hash # pylint: disable=W0611,E0611,F0401
51 from hashlib import sha1 as sha1_hash # pylint: disable=W0611,E0611,F0401
52 # this additional version is needed for compatibility with the hmac module
55 from md5 import new as md5_hash
62 """Returns True if all elements in the iterable are True.
65 for _ in itertools.ifilterfalse(bool, seq):
71 """Returns True if any element of the iterable are True.
74 for _ in itertools.ifilter(bool, seq):
80 # pylint: disable=E0601
81 # pylint: disable=W0622
87 # pylint: disable=E0601
88 # pylint: disable=W0622
94 def partition(seq, pred=bool): # pylint: disable=W0622
95 """Partition a list in two, based on the given predicate.
98 return (list(itertools.ifilter(pred, seq)),
99 list(itertools.ifilterfalse(pred, seq)))
102 # Even though we're using Python's built-in "partial" function if available,
103 # this one is always defined for testing.
104 def _partial(func, *args, **keywords): # pylint: disable=W0622
105 """Decorator with partial application of arguments and keywords.
107 This function was copied from Python's documentation.
110 def newfunc(*fargs, **fkeywords):
111 newkeywords = keywords.copy()
112 newkeywords.update(fkeywords)
113 return func(*(args + fargs), **newkeywords) # pylint: disable=W0142
117 newfunc.keywords = keywords
121 if functools is None:
124 partial = functools.partial
127 def TryToRoman(val, convert=True):
128 """Try to convert a value to roman numerals
130 If the roman module could be loaded convert the given value to a roman
131 numeral. Gracefully fail back to leaving the value untouched.
134 @param val: value to convert
135 @type convert: boolean
136 @param convert: if False, don't try conversion at all
137 @rtype: string or typeof(val)
138 @return: roman numeral for val, or val if conversion didn't succeed
141 if roman is not None and convert:
143 return roman.toRoman(val)
144 except roman.RomanError:
149 #: returns the first element of a list-like value
150 fst = operator.itemgetter(0)
152 #: returns the second element of a list-like value
153 snd = operator.itemgetter(1)