root / lib / compat.py @ b41b3516
History | View | Annotate | Download (3.8 kB)
1 |
#
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2010 Google Inc.
|
5 |
#
|
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.
|
10 |
#
|
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.
|
15 |
#
|
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
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Module containing backported language/library functionality.
|
23 |
|
24 |
"""
|
25 |
|
26 |
import itertools |
27 |
|
28 |
try:
|
29 |
import functools |
30 |
except ImportError: |
31 |
functools = None
|
32 |
|
33 |
try:
|
34 |
import roman |
35 |
except ImportError: |
36 |
roman = None
|
37 |
|
38 |
|
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.
|
44 |
try:
|
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
|
50 |
sha1 = sha1_hash |
51 |
except ImportError: |
52 |
from md5 import new as md5_hash |
53 |
import sha |
54 |
sha1 = sha |
55 |
sha1_hash = sha.new |
56 |
|
57 |
|
58 |
def all(seq, pred=bool): # pylint: disable-msg=W0622 |
59 |
"""Returns True if pred(x) is True for every element in the iterable.
|
60 |
|
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.
|
63 |
|
64 |
"""
|
65 |
for _ in itertools.ifilterfalse(pred, seq): |
66 |
return False |
67 |
return True |
68 |
|
69 |
|
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.
|
72 |
|
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.
|
75 |
|
76 |
"""
|
77 |
for _ in itertools.ifilter(pred, seq): |
78 |
return True |
79 |
return False |
80 |
|
81 |
|
82 |
def partition(seq, pred=bool): # pylint: disable-msg=W0622 |
83 |
"""Partition a list in two, based on the given predicate.
|
84 |
|
85 |
"""
|
86 |
return (list(itertools.ifilter(pred, seq)), |
87 |
list(itertools.ifilterfalse(pred, seq)))
|
88 |
|
89 |
|
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.
|
94 |
|
95 |
This function was copied from Python's documentation.
|
96 |
|
97 |
"""
|
98 |
def newfunc(*fargs, **fkeywords): |
99 |
newkeywords = keywords.copy() |
100 |
newkeywords.update(fkeywords) |
101 |
return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
102 |
|
103 |
newfunc.func = func |
104 |
newfunc.args = args |
105 |
newfunc.keywords = keywords |
106 |
return newfunc
|
107 |
|
108 |
|
109 |
def TryToRoman(val, convert=True): |
110 |
"""Try to convert a value to roman numerals
|
111 |
|
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.
|
114 |
|
115 |
@type val: integer
|
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
|
121 |
|
122 |
"""
|
123 |
if roman is not None and convert: |
124 |
try:
|
125 |
return roman.toRoman(val)
|
126 |
except roman.RomanError:
|
127 |
return val
|
128 |
else:
|
129 |
return val
|
130 |
|
131 |
|
132 |
if functools is None: |
133 |
partial = _partial |
134 |
else:
|
135 |
partial = functools.partial |