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