1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """Module containing backported language/library functionality.
32
33 """
34
35 import itertools
36 import operator
37
38 try:
39
40 import functools
41 except ImportError:
42 functools = None
43
44 try:
45
46 import roman
47 except ImportError:
48 roman = None
49
50
51
52
53
54
55
56 try:
57
58
59 from hashlib import md5 as md5_hash
60 from hashlib import sha1 as sha1_hash
61
62 sha1 = sha1_hash
63 except ImportError:
64 from md5 import new as md5_hash
65 import sha
66 sha1 = sha
67 sha1_hash = sha.new
68
69
71 """Returns True if all elements in the iterable are True.
72
73 """
74 for _ in itertools.ifilterfalse(bool, seq):
75 return False
76 return True
77
78
80 """Returns True if any element of the iterable are True.
81
82 """
83 for _ in itertools.ifilter(bool, seq):
84 return True
85 return False
86
87
88 try:
89
90
91 all = all
92 except NameError:
93 all = _all
94
95 try:
96
97
98 any = any
99 except NameError:
100 any = _any
101
102
104 """Partition a list in two, based on the given predicate.
105
106 """
107 return (list(itertools.ifilter(pred, seq)),
108 list(itertools.ifilterfalse(pred, seq)))
109
110
111
112
114 """Decorator with partial application of arguments and keywords.
115
116 This function was copied from Python's documentation.
117
118 """
119 def newfunc(*fargs, **fkeywords):
120 newkeywords = keywords.copy()
121 newkeywords.update(fkeywords)
122 return func(*(args + fargs), **newkeywords)
123
124 newfunc.func = func
125 newfunc.args = args
126 newfunc.keywords = keywords
127 return newfunc
128
129
130 if functools is None:
131 partial = _partial
132 else:
133 partial = functools.partial
134
135
137 """Try to convert a value to roman numerals
138
139 If the roman module could be loaded convert the given value to a roman
140 numeral. Gracefully fail back to leaving the value untouched.
141
142 @type val: integer
143 @param val: value to convert
144 @type convert: boolean
145 @param convert: if False, don't try conversion at all
146 @rtype: string or typeof(val)
147 @return: roman numeral for val, or val if conversion didn't succeed
148
149 """
150 if roman is not None and convert:
151 try:
152 return roman.toRoman(val)
153 except roman.RomanError:
154 return val
155 else:
156 return val
157
158
160 """Makes C{frozenset} from sequence after checking for duplicate elements.
161
162 @raise ValueError: When there are duplicate elements
163
164 """
165 if isinstance(seq, (list, tuple)):
166 items = seq
167 else:
168 items = list(seq)
169
170 result = frozenset(items)
171
172 if len(items) != len(result):
173 raise ValueError("Duplicate values found")
174
175 return result
176
177
178
179 fst = operator.itemgetter(0)
180
181
182 snd = operator.itemgetter(1)
183