root / lib / compat.py @ 9f7b4967
History | View | Annotate | Download (3.8 kB)
1 | cea881e5 | Michael Hanselmann | #
|
---|---|---|---|
2 | cea881e5 | Michael Hanselmann | #
|
3 | cea881e5 | Michael Hanselmann | |
4 | cea881e5 | Michael Hanselmann | # Copyright (C) 2010 Google Inc.
|
5 | cea881e5 | Michael Hanselmann | #
|
6 | cea881e5 | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | cea881e5 | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | cea881e5 | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | cea881e5 | Michael Hanselmann | # (at your option) any later version.
|
10 | cea881e5 | Michael Hanselmann | #
|
11 | cea881e5 | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | cea881e5 | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | cea881e5 | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | cea881e5 | Michael Hanselmann | # General Public License for more details.
|
15 | cea881e5 | Michael Hanselmann | #
|
16 | cea881e5 | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | cea881e5 | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | cea881e5 | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | cea881e5 | Michael Hanselmann | # 02110-1301, USA.
|
20 | cea881e5 | Michael Hanselmann | |
21 | cea881e5 | Michael Hanselmann | |
22 | cea881e5 | Michael Hanselmann | """Module containing backported language/library functionality.
|
23 | cea881e5 | Michael Hanselmann |
|
24 | cea881e5 | Michael Hanselmann | """
|
25 | cea881e5 | Michael Hanselmann | |
26 | cea881e5 | Michael Hanselmann | import itertools |
27 | cea881e5 | Michael Hanselmann | |
28 | cea881e5 | Michael Hanselmann | try:
|
29 | cea881e5 | Michael Hanselmann | import functools |
30 | cea881e5 | Michael Hanselmann | except ImportError: |
31 | cea881e5 | Michael Hanselmann | functools = None
|
32 | cea881e5 | Michael Hanselmann | |
33 | 6396164f | Guido Trotter | try:
|
34 | 6396164f | Guido Trotter | import roman |
35 | 6396164f | Guido Trotter | except ImportError: |
36 | 6396164f | Guido Trotter | roman = None
|
37 | 6396164f | Guido Trotter | |
38 | cea881e5 | Michael Hanselmann | |
39 | 716a32cb | Guido Trotter | # compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
|
40 | 716a32cb | Guido Trotter | # sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
|
41 | 716a32cb | Guido Trotter | # went on. compat.sha1 is python-version specific and is used for python
|
42 | 716a32cb | Guido Trotter | # modules (hmac, for example) which have changed their behavior as well from
|
43 | 716a32cb | Guido Trotter | # one version to the other.
|
44 | 716a32cb | Guido Trotter | try:
|
45 | 716a32cb | Guido Trotter | # Yes, we're not using the imports in this module.
|
46 | 716a32cb | Guido Trotter | # pylint: disable-msg=W0611
|
47 | 716a32cb | Guido Trotter | from hashlib import md5 as md5_hash |
48 | 716a32cb | Guido Trotter | from hashlib import sha1 as sha1_hash |
49 | 716a32cb | Guido Trotter | # this additional version is needed for compatibility with the hmac module
|
50 | 716a32cb | Guido Trotter | sha1 = sha1_hash |
51 | 716a32cb | Guido Trotter | except ImportError: |
52 | 716a32cb | Guido Trotter | from md5 import new as md5_hash |
53 | 716a32cb | Guido Trotter | import sha |
54 | 716a32cb | Guido Trotter | sha1 = sha |
55 | 716a32cb | Guido Trotter | sha1_hash = sha.new |
56 | 716a32cb | Guido Trotter | |
57 | 716a32cb | Guido Trotter | |
58 | cea881e5 | Michael Hanselmann | def all(seq, pred=bool): # pylint: disable-msg=W0622 |
59 | cea881e5 | Michael Hanselmann | """Returns True if pred(x) is True for every element in the iterable.
|
60 | cea881e5 | Michael Hanselmann |
|
61 | cea881e5 | Michael Hanselmann | Please note that this function provides a C{pred} parameter which isn't
|
62 | cea881e5 | Michael Hanselmann | available in the version included in Python 2.5 and above.
|
63 | cea881e5 | Michael Hanselmann |
|
64 | cea881e5 | Michael Hanselmann | """
|
65 | cea881e5 | Michael Hanselmann | for _ in itertools.ifilterfalse(pred, seq): |
66 | cea881e5 | Michael Hanselmann | return False |
67 | cea881e5 | Michael Hanselmann | return True |
68 | cea881e5 | Michael Hanselmann | |
69 | cea881e5 | Michael Hanselmann | |
70 | cea881e5 | Michael Hanselmann | def any(seq, pred=bool): # pylint: disable-msg=W0622 |
71 | cea881e5 | Michael Hanselmann | """Returns True if pred(x) is True for at least one element in the iterable.
|
72 | cea881e5 | Michael Hanselmann |
|
73 | cea881e5 | Michael Hanselmann | Please note that this function provides a C{pred} parameter which isn't
|
74 | cea881e5 | Michael Hanselmann | available in the version included in Python 2.5 and above.
|
75 | cea881e5 | Michael Hanselmann |
|
76 | cea881e5 | Michael Hanselmann | """
|
77 | cea881e5 | Michael Hanselmann | for _ in itertools.ifilter(pred, seq): |
78 | cea881e5 | Michael Hanselmann | return True |
79 | cea881e5 | Michael Hanselmann | return False |
80 | cea881e5 | Michael Hanselmann | |
81 | cea881e5 | Michael Hanselmann | |
82 | cea881e5 | Michael Hanselmann | def partition(seq, pred=bool): # pylint: disable-msg=W0622 |
83 | cea881e5 | Michael Hanselmann | """Partition a list in two, based on the given predicate.
|
84 | cea881e5 | Michael Hanselmann |
|
85 | cea881e5 | Michael Hanselmann | """
|
86 | cea881e5 | Michael Hanselmann | return (list(itertools.ifilter(pred, seq)), |
87 | cea881e5 | Michael Hanselmann | list(itertools.ifilterfalse(pred, seq)))
|
88 | cea881e5 | Michael Hanselmann | |
89 | cea881e5 | Michael Hanselmann | |
90 | cea881e5 | Michael Hanselmann | # Even though we're using Python's built-in "partial" function if available,
|
91 | cea881e5 | Michael Hanselmann | # this one is always defined for testing.
|
92 | cea881e5 | Michael Hanselmann | def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
93 | cea881e5 | Michael Hanselmann | """Decorator with partial application of arguments and keywords.
|
94 | cea881e5 | Michael Hanselmann |
|
95 | cea881e5 | Michael Hanselmann | This function was copied from Python's documentation.
|
96 | cea881e5 | Michael Hanselmann |
|
97 | cea881e5 | Michael Hanselmann | """
|
98 | cea881e5 | Michael Hanselmann | def newfunc(*fargs, **fkeywords): |
99 | cea881e5 | Michael Hanselmann | newkeywords = keywords.copy() |
100 | cea881e5 | Michael Hanselmann | newkeywords.update(fkeywords) |
101 | cea881e5 | Michael Hanselmann | return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
102 | cea881e5 | Michael Hanselmann | |
103 | cea881e5 | Michael Hanselmann | newfunc.func = func |
104 | cea881e5 | Michael Hanselmann | newfunc.args = args |
105 | cea881e5 | Michael Hanselmann | newfunc.keywords = keywords |
106 | cea881e5 | Michael Hanselmann | return newfunc
|
107 | cea881e5 | Michael Hanselmann | |
108 | cea881e5 | Michael Hanselmann | |
109 | 6396164f | Guido Trotter | def TryToRoman(val, convert=True): |
110 | 6396164f | Guido Trotter | """Try to convert a value to roman numerals
|
111 | 6396164f | Guido Trotter |
|
112 | 6396164f | Guido Trotter | If the roman module could be loaded convert the given value to a roman
|
113 | 6396164f | Guido Trotter | numeral. Gracefully fail back to leaving the value untouched.
|
114 | 6396164f | Guido Trotter |
|
115 | 6396164f | Guido Trotter | @type val: integer
|
116 | 6396164f | Guido Trotter | @param val: value to convert
|
117 | 6396164f | Guido Trotter | @type convert: boolean
|
118 | 6396164f | Guido Trotter | @param convert: if False, don't try conversion at all
|
119 | 6396164f | Guido Trotter | @rtype: string or typeof(val)
|
120 | 6396164f | Guido Trotter | @return: roman numeral for val, or val if conversion didn't succeed
|
121 | 6396164f | Guido Trotter |
|
122 | 6396164f | Guido Trotter | """
|
123 | 6396164f | Guido Trotter | if roman is not None and convert: |
124 | 6396164f | Guido Trotter | try:
|
125 | 6396164f | Guido Trotter | return roman.toRoman(val)
|
126 | 6396164f | Guido Trotter | except roman.RomanError:
|
127 | 6396164f | Guido Trotter | return val
|
128 | 6396164f | Guido Trotter | else:
|
129 | 6396164f | Guido Trotter | return val
|
130 | 6396164f | Guido Trotter | |
131 | 6396164f | Guido Trotter | |
132 | cea881e5 | Michael Hanselmann | if functools is None: |
133 | cea881e5 | Michael Hanselmann | partial = _partial |
134 | cea881e5 | Michael Hanselmann | else:
|
135 | cea881e5 | Michael Hanselmann | partial = functools.partial |