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
51 from hashlib import md5 as md5_hash
52 from hashlib import sha1 as sha1_hash
53
54 sha1 = sha1_hash
55 except ImportError:
56 from md5 import new as md5_hash
57 import sha
58 sha1 = sha
59 sha1_hash = sha.new
60
61
63 """Returns True if all elements in the iterable are True.
64
65 """
66 for _ in itertools.ifilterfalse(bool, seq):
67 return False
68 return True
69
70
72 """Returns True if any element of the iterable are True.
73
74 """
75 for _ in itertools.ifilter(bool, seq):
76 return True
77 return False
78
79
80 try:
81
82
83 all = all
84 except NameError:
85 all = _all
86
87 try:
88
89
90 any = any
91 except NameError:
92 any = _any
93
94
96 """Partition a list in two, based on the given predicate.
97
98 """
99 return (list(itertools.ifilter(pred, seq)),
100 list(itertools.ifilterfalse(pred, seq)))
101
102
103
104
106 """Decorator with partial application of arguments and keywords.
107
108 This function was copied from Python's documentation.
109
110 """
111 def newfunc(*fargs, **fkeywords):
112 newkeywords = keywords.copy()
113 newkeywords.update(fkeywords)
114 return func(*(args + fargs), **newkeywords)
115
116 newfunc.func = func
117 newfunc.args = args
118 newfunc.keywords = keywords
119 return newfunc
120
121
122 if functools is None:
123 partial = _partial
124 else:
125 partial = functools.partial
126
127
129 """Try to convert a value to roman numerals
130
131 If the roman module could be loaded convert the given value to a roman
132 numeral. Gracefully fail back to leaving the value untouched.
133
134 @type val: integer
135 @param val: value to convert
136 @type convert: boolean
137 @param convert: if False, don't try conversion at all
138 @rtype: string or typeof(val)
139 @return: roman numeral for val, or val if conversion didn't succeed
140
141 """
142 if roman is not None and convert:
143 try:
144 return roman.toRoman(val)
145 except roman.RomanError:
146 return val
147 else:
148 return val
149
150
151 fst = operator.itemgetter(0)
152
153
154 snd = operator.itemgetter(1)
155