root / lib / compat.py @ ef628379
History | View | Annotate | Download (3.1 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 | cea881e5 | Michael Hanselmann | import functools |
30 | cea881e5 | Michael Hanselmann | except ImportError: |
31 | cea881e5 | Michael Hanselmann | functools = None
|
32 | cea881e5 | Michael Hanselmann | |
33 | 6396164f | Guido Trotter | try:
|
34 | 6396164f | Guido Trotter | import roman |
35 | 6396164f | Guido Trotter | except ImportError: |
36 | 6396164f | Guido Trotter | roman = None
|
37 | 6396164f | Guido Trotter | |
38 | cea881e5 | Michael Hanselmann | |
39 | cea881e5 | Michael Hanselmann | def all(seq, pred=bool): # pylint: disable-msg=W0622 |
40 | cea881e5 | Michael Hanselmann | """Returns True if pred(x) is True for every element in the iterable.
|
41 | cea881e5 | Michael Hanselmann |
|
42 | cea881e5 | Michael Hanselmann | Please note that this function provides a C{pred} parameter which isn't
|
43 | cea881e5 | Michael Hanselmann | available in the version included in Python 2.5 and above.
|
44 | cea881e5 | Michael Hanselmann |
|
45 | cea881e5 | Michael Hanselmann | """
|
46 | cea881e5 | Michael Hanselmann | for _ in itertools.ifilterfalse(pred, seq): |
47 | cea881e5 | Michael Hanselmann | return False |
48 | cea881e5 | Michael Hanselmann | return True |
49 | cea881e5 | Michael Hanselmann | |
50 | cea881e5 | Michael Hanselmann | |
51 | cea881e5 | Michael Hanselmann | def any(seq, pred=bool): # pylint: disable-msg=W0622 |
52 | cea881e5 | Michael Hanselmann | """Returns True if pred(x) is True for at least one element in the iterable.
|
53 | cea881e5 | Michael Hanselmann |
|
54 | cea881e5 | Michael Hanselmann | Please note that this function provides a C{pred} parameter which isn't
|
55 | cea881e5 | Michael Hanselmann | available in the version included in Python 2.5 and above.
|
56 | cea881e5 | Michael Hanselmann |
|
57 | cea881e5 | Michael Hanselmann | """
|
58 | cea881e5 | Michael Hanselmann | for _ in itertools.ifilter(pred, seq): |
59 | cea881e5 | Michael Hanselmann | return True |
60 | cea881e5 | Michael Hanselmann | return False |
61 | cea881e5 | Michael Hanselmann | |
62 | cea881e5 | Michael Hanselmann | |
63 | cea881e5 | Michael Hanselmann | def partition(seq, pred=bool): # pylint: disable-msg=W0622 |
64 | cea881e5 | Michael Hanselmann | """Partition a list in two, based on the given predicate.
|
65 | cea881e5 | Michael Hanselmann |
|
66 | cea881e5 | Michael Hanselmann | """
|
67 | cea881e5 | Michael Hanselmann | return (list(itertools.ifilter(pred, seq)), |
68 | cea881e5 | Michael Hanselmann | list(itertools.ifilterfalse(pred, seq)))
|
69 | cea881e5 | Michael Hanselmann | |
70 | cea881e5 | Michael Hanselmann | |
71 | cea881e5 | Michael Hanselmann | # Even though we're using Python's built-in "partial" function if available,
|
72 | cea881e5 | Michael Hanselmann | # this one is always defined for testing.
|
73 | cea881e5 | Michael Hanselmann | def _partial(func, *args, **keywords): # pylint: disable-msg=W0622 |
74 | cea881e5 | Michael Hanselmann | """Decorator with partial application of arguments and keywords.
|
75 | cea881e5 | Michael Hanselmann |
|
76 | cea881e5 | Michael Hanselmann | This function was copied from Python's documentation.
|
77 | cea881e5 | Michael Hanselmann |
|
78 | cea881e5 | Michael Hanselmann | """
|
79 | cea881e5 | Michael Hanselmann | def newfunc(*fargs, **fkeywords): |
80 | cea881e5 | Michael Hanselmann | newkeywords = keywords.copy() |
81 | cea881e5 | Michael Hanselmann | newkeywords.update(fkeywords) |
82 | cea881e5 | Michael Hanselmann | return func(*(args + fargs), **newkeywords) # pylint: disable-msg=W0142 |
83 | cea881e5 | Michael Hanselmann | |
84 | cea881e5 | Michael Hanselmann | newfunc.func = func |
85 | cea881e5 | Michael Hanselmann | newfunc.args = args |
86 | cea881e5 | Michael Hanselmann | newfunc.keywords = keywords |
87 | cea881e5 | Michael Hanselmann | return newfunc
|
88 | cea881e5 | Michael Hanselmann | |
89 | cea881e5 | Michael Hanselmann | |
90 | 6396164f | Guido Trotter | def TryToRoman(val, convert=True): |
91 | 6396164f | Guido Trotter | """Try to convert a value to roman numerals
|
92 | 6396164f | Guido Trotter |
|
93 | 6396164f | Guido Trotter | If the roman module could be loaded convert the given value to a roman
|
94 | 6396164f | Guido Trotter | numeral. Gracefully fail back to leaving the value untouched.
|
95 | 6396164f | Guido Trotter |
|
96 | 6396164f | Guido Trotter | @type val: integer
|
97 | 6396164f | Guido Trotter | @param val: value to convert
|
98 | 6396164f | Guido Trotter | @type convert: boolean
|
99 | 6396164f | Guido Trotter | @param convert: if False, don't try conversion at all
|
100 | 6396164f | Guido Trotter | @rtype: string or typeof(val)
|
101 | 6396164f | Guido Trotter | @return: roman numeral for val, or val if conversion didn't succeed
|
102 | 6396164f | Guido Trotter |
|
103 | 6396164f | Guido Trotter | """
|
104 | 6396164f | Guido Trotter | if roman is not None and convert: |
105 | 6396164f | Guido Trotter | try:
|
106 | 6396164f | Guido Trotter | return roman.toRoman(val)
|
107 | 6396164f | Guido Trotter | except roman.RomanError:
|
108 | 6396164f | Guido Trotter | return val
|
109 | 6396164f | Guido Trotter | else:
|
110 | 6396164f | Guido Trotter | return val
|
111 | 6396164f | Guido Trotter | |
112 | 6396164f | Guido Trotter | |
113 | cea881e5 | Michael Hanselmann | if functools is None: |
114 | cea881e5 | Michael Hanselmann | partial = _partial |
115 | cea881e5 | Michael Hanselmann | else:
|
116 | cea881e5 | Michael Hanselmann | partial = functools.partial |