4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Module containing backported language/library functionality.
34 def all(seq, pred=bool): # pylint: disable-msg=W0622
35 """Returns True if pred(x) is True for every element in the iterable.
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.
41 for _ in itertools.ifilterfalse(pred, seq):
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.
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.
53 for _ in itertools.ifilter(pred, seq):
58 def partition(seq, pred=bool): # pylint: disable-msg=W0622
59 """Partition a list in two, based on the given predicate.
62 return (list(itertools.ifilter(pred, seq)),
63 list(itertools.ifilterfalse(pred, seq)))
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.
71 This function was copied from Python's documentation.
74 def newfunc(*fargs, **fkeywords):
75 newkeywords = keywords.copy()
76 newkeywords.update(fkeywords)
77 return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142
81 newfunc.keywords = keywords
88 partial = functools.partial