root / lib / compat.py @ 716a32cb
History | View | Annotate | Download (3.1 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 |
|
34 |
# compat.md5_hash and compat.sha1_hash can be called to generate and md5 and a
|
35 |
# sha1 hashing modules, under python 2.4, 2.5 and 2.6, even though some changes
|
36 |
# went on. compat.sha1 is python-version specific and is used for python
|
37 |
# modules (hmac, for example) which have changed their behavior as well from
|
38 |
# one version to the other.
|
39 |
try:
|
40 |
# Yes, we're not using the imports in this module.
|
41 |
# pylint: disable-msg=W0611
|
42 |
from hashlib import md5 as md5_hash |
43 |
from hashlib import sha1 as sha1_hash |
44 |
# this additional version is needed for compatibility with the hmac module
|
45 |
sha1 = sha1_hash |
46 |
except ImportError: |
47 |
from md5 import new as md5_hash |
48 |
import sha |
49 |
sha1 = sha |
50 |
sha1_hash = sha.new |
51 |
|
52 |
|
53 |
def all(seq, pred=bool): # pylint: disable-msg=W0622 |
54 |
"""Returns True if pred(x) is True for every element in the iterable.
|
55 |
|
56 |
Please note that this function provides a C{pred} parameter which isn't
|
57 |
available in the version included in Python 2.5 and above.
|
58 |
|
59 |
"""
|
60 |
for _ in itertools.ifilterfalse(pred, seq): |
61 |
return False |
62 |
return True |
63 |
|
64 |
|
65 |
def any(seq, pred=bool): # pylint: disable-msg=W0622 |
66 |
"""Returns True if pred(x) is True for at least one element in the iterable.
|
67 |
|
68 |
Please note that this function provides a C{pred} parameter which isn't
|
69 |
available in the version included in Python 2.5 and above.
|
70 |
|
71 |
"""
|
72 |
for _ in itertools.ifilter(pred, seq): |
73 |
return True |
74 |
return False |
75 |
|
76 |
|
77 |
def partition(seq, pred=bool): # pylint: disable-msg=W0622 |
78 |
"""Partition a list in two, based on the given predicate.
|
79 |
|
80 |
"""
|
81 |
return (list(itertools.ifilter(pred, seq)), |
82 |
list(itertools.ifilterfalse(pred, seq)))
|
83 |
|
84 |
|
85 |
# Even though we're using Python's built-in "partial" function if available,
|
86 |
# this one is always defined for testing.
|
87 |
def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
88 |
"""Decorator with partial application of arguments and keywords.
|
89 |
|
90 |
This function was copied from Python's documentation.
|
91 |
|
92 |
"""
|
93 |
def newfunc(*fargs, **fkeywords): |
94 |
newkeywords = keywords.copy() |
95 |
newkeywords.update(fkeywords) |
96 |
return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
97 |
|
98 |
newfunc.func = func |
99 |
newfunc.args = args |
100 |
newfunc.keywords = keywords |
101 |
return newfunc
|
102 |
|
103 |
|
104 |
if functools is None: |
105 |
partial = _partial |
106 |
else:
|
107 |
partial = functools.partial |