root / lib / compat.py @ a535cef7
History | View | Annotate | Download (3.7 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 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=F0401
|
30 | cea881e5 | Michael Hanselmann | import functools |
31 | cea881e5 | Michael Hanselmann | except ImportError: |
32 | cea881e5 | Michael Hanselmann | functools = None
|
33 | cea881e5 | Michael Hanselmann | |
34 | 6396164f | Guido Trotter | try:
|
35 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=F0401
|
36 | 6396164f | Guido Trotter | import roman |
37 | 6396164f | Guido Trotter | except ImportError: |
38 | 6396164f | Guido Trotter | roman = None
|
39 | 6396164f | Guido Trotter | |
40 | cea881e5 | Michael Hanselmann | |
41 | 716a32cb | Guido Trotter | # compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
|
42 | 716a32cb | Guido Trotter | # sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
|
43 | 716a32cb | Guido Trotter | # went on. compat.sha1 is python-version specific and is used for python
|
44 | 716a32cb | Guido Trotter | # modules (hmac, for example) which have changed their behavior as well from
|
45 | 716a32cb | Guido Trotter | # one version to the other.
|
46 | 716a32cb | Guido Trotter | try:
|
47 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=F0401
|
48 | 716a32cb | Guido Trotter | # Yes, we're not using the imports in this module.
|
49 | 716a32cb | Guido Trotter | # pylint: disable-msg=W0611
|
50 | 716a32cb | Guido Trotter | from hashlib import md5 as md5_hash |
51 | 716a32cb | Guido Trotter | from hashlib import sha1 as sha1_hash |
52 | 716a32cb | Guido Trotter | # this additional version is needed for compatibility with the hmac module
|
53 | 716a32cb | Guido Trotter | sha1 = sha1_hash |
54 | 716a32cb | Guido Trotter | except ImportError: |
55 | 716a32cb | Guido Trotter | from md5 import new as md5_hash |
56 | 716a32cb | Guido Trotter | import sha |
57 | 716a32cb | Guido Trotter | sha1 = sha |
58 | 716a32cb | Guido Trotter | sha1_hash = sha.new |
59 | 716a32cb | Guido Trotter | |
60 | 716a32cb | Guido Trotter | |
61 | 403f5172 | Guido Trotter | def _all(seq): |
62 | 403f5172 | Guido Trotter | """Returns True if all elements in the iterable are True.
|
63 | cea881e5 | Michael Hanselmann |
|
64 | cea881e5 | Michael Hanselmann | """
|
65 | 403f5172 | Guido Trotter | for _ in itertools.ifilterfalse(bool, seq): |
66 | cea881e5 | Michael Hanselmann | return False |
67 | cea881e5 | Michael Hanselmann | return True |
68 | cea881e5 | Michael Hanselmann | |
69 | 403f5172 | Guido Trotter | def _any(seq): |
70 | 403f5172 | Guido Trotter | """Returns True if any element of the iterable are True.
|
71 | cea881e5 | Michael Hanselmann |
|
72 | cea881e5 | Michael Hanselmann | """
|
73 | 403f5172 | Guido Trotter | for _ in itertools.ifilter(bool, seq): |
74 | cea881e5 | Michael Hanselmann | return True |
75 | cea881e5 | Michael Hanselmann | return False |
76 | cea881e5 | Michael Hanselmann | |
77 | 403f5172 | Guido Trotter | try:
|
78 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=E0601
|
79 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=W0622
|
80 | 23e0ef8c | Guido Trotter | all = all
|
81 | 403f5172 | Guido Trotter | except NameError: |
82 | 403f5172 | Guido Trotter | all = _all |
83 | 403f5172 | Guido Trotter | |
84 | 403f5172 | Guido Trotter | try:
|
85 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=E0601
|
86 | 23e0ef8c | Guido Trotter | # pylint: disable-msg=W0622
|
87 | 23e0ef8c | Guido Trotter | any = any
|
88 | 403f5172 | Guido Trotter | except NameError: |
89 | 403f5172 | Guido Trotter | any = _any |
90 | cea881e5 | Michael Hanselmann | |
91 | cea881e5 | Michael Hanselmann | def partition(seq, pred=bool): # pylint: disable-msg=W0622 |
92 | cea881e5 | Michael Hanselmann | """Partition a list in two, based on the given predicate.
|
93 | cea881e5 | Michael Hanselmann |
|
94 | cea881e5 | Michael Hanselmann | """
|
95 | cea881e5 | Michael Hanselmann | return (list(itertools.ifilter(pred, seq)), |
96 | cea881e5 | Michael Hanselmann | list(itertools.ifilterfalse(pred, seq)))
|
97 | cea881e5 | Michael Hanselmann | |
98 | cea881e5 | Michael Hanselmann | |
99 | cea881e5 | Michael Hanselmann | # Even though we're using Python's built-in "partial" function if available,
|
100 | cea881e5 | Michael Hanselmann | # this one is always defined for testing.
|
101 | cea881e5 | Michael Hanselmann | def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
102 | cea881e5 | Michael Hanselmann | """Decorator with partial application of arguments and keywords.
|
103 | cea881e5 | Michael Hanselmann |
|
104 | cea881e5 | Michael Hanselmann | This function was copied from Python's documentation.
|
105 | cea881e5 | Michael Hanselmann |
|
106 | cea881e5 | Michael Hanselmann | """
|
107 | cea881e5 | Michael Hanselmann | def newfunc(*fargs, **fkeywords): |
108 | cea881e5 | Michael Hanselmann | newkeywords = keywords.copy() |
109 | cea881e5 | Michael Hanselmann | newkeywords.update(fkeywords) |
110 | cea881e5 | Michael Hanselmann | return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
111 | cea881e5 | Michael Hanselmann | |
112 | cea881e5 | Michael Hanselmann | newfunc.func = func |
113 | cea881e5 | Michael Hanselmann | newfunc.args = args |
114 | cea881e5 | Michael Hanselmann | newfunc.keywords = keywords |
115 | cea881e5 | Michael Hanselmann | return newfunc
|
116 | cea881e5 | Michael Hanselmann | |
117 | cea881e5 | Michael Hanselmann | |
118 | 403f5172 | Guido Trotter | if functools is None: |
119 | 403f5172 | Guido Trotter | partial = _partial |
120 | 403f5172 | Guido Trotter | else:
|
121 | 403f5172 | Guido Trotter | partial = functools.partial |
122 | 403f5172 | Guido Trotter | |
123 | 403f5172 | Guido Trotter | |
124 | 6396164f | Guido Trotter | def TryToRoman(val, convert=True): |
125 | 6396164f | Guido Trotter | """Try to convert a value to roman numerals
|
126 | 6396164f | Guido Trotter |
|
127 | 6396164f | Guido Trotter | If the roman module could be loaded convert the given value to a roman
|
128 | 6396164f | Guido Trotter | numeral. Gracefully fail back to leaving the value untouched.
|
129 | 6396164f | Guido Trotter |
|
130 | 6396164f | Guido Trotter | @type val: integer
|
131 | 6396164f | Guido Trotter | @param val: value to convert
|
132 | 6396164f | Guido Trotter | @type convert: boolean
|
133 | 6396164f | Guido Trotter | @param convert: if False, don't try conversion at all
|
134 | 6396164f | Guido Trotter | @rtype: string or typeof(val)
|
135 | 6396164f | Guido Trotter | @return: roman numeral for val, or val if conversion didn't succeed
|
136 | 6396164f | Guido Trotter |
|
137 | 6396164f | Guido Trotter | """
|
138 | 6396164f | Guido Trotter | if roman is not None and convert: |
139 | 6396164f | Guido Trotter | try:
|
140 | 6396164f | Guido Trotter | return roman.toRoman(val)
|
141 | 6396164f | Guido Trotter | except roman.RomanError:
|
142 | 6396164f | Guido Trotter | return val
|
143 | 6396164f | Guido Trotter | else:
|
144 | 6396164f | Guido Trotter | return val
|