root / lib / compat.py @ cea881e5
History | View | Annotate | Download (2.4 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 |
def all(seq, pred=bool): # pylint: disable-msg=W0622 |
35 |
"""Returns True if pred(x) is True for every element in the iterable.
|
36 |
|
37 |
Please note that this function provides a C{pred} parameter which isn't
|
38 |
available in the version included in Python 2.5 and above.
|
39 |
|
40 |
"""
|
41 |
for _ in itertools.ifilterfalse(pred, seq): |
42 |
return False |
43 |
return True |
44 |
|
45 |
|
46 |
def any(seq, pred=bool): # pylint: disable-msg=W0622 |
47 |
"""Returns True if pred(x) is True for at least one element in the iterable.
|
48 |
|
49 |
Please note that this function provides a C{pred} parameter which isn't
|
50 |
available in the version included in Python 2.5 and above.
|
51 |
|
52 |
"""
|
53 |
for _ in itertools.ifilter(pred, seq): |
54 |
return True |
55 |
return False |
56 |
|
57 |
|
58 |
def partition(seq, pred=bool): # pylint: disable-msg=W0622 |
59 |
"""Partition a list in two, based on the given predicate.
|
60 |
|
61 |
"""
|
62 |
return (list(itertools.ifilter(pred, seq)), |
63 |
list(itertools.ifilterfalse(pred, seq)))
|
64 |
|
65 |
|
66 |
# Even though we're using Python's built-in "partial" function if available,
|
67 |
# this one is always defined for testing.
|
68 |
def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
69 |
"""Decorator with partial application of arguments and keywords.
|
70 |
|
71 |
This function was copied from Python's documentation.
|
72 |
|
73 |
"""
|
74 |
def newfunc(*fargs, **fkeywords): |
75 |
newkeywords = keywords.copy() |
76 |
newkeywords.update(fkeywords) |
77 |
return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
78 |
|
79 |
newfunc.func = func |
80 |
newfunc.args = args |
81 |
newfunc.keywords = keywords |
82 |
return newfunc
|
83 |
|
84 |
|
85 |
if functools is None: |
86 |
partial = _partial |
87 |
else:
|
88 |
partial = functools.partial |