1 | n/a | """ |
---|
2 | n/a | Operator Interface |
---|
3 | n/a | |
---|
4 | n/a | This module exports a set of functions corresponding to the intrinsic |
---|
5 | n/a | operators of Python. For example, operator.add(x, y) is equivalent |
---|
6 | n/a | to the expression x+y. The function names are those used for special |
---|
7 | n/a | methods; variants without leading and trailing '__' are also provided |
---|
8 | n/a | for convenience. |
---|
9 | n/a | |
---|
10 | n/a | This is the pure Python implementation of the module. |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | __all__ = ['abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', |
---|
14 | n/a | 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', |
---|
15 | n/a | 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', |
---|
16 | n/a | 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', |
---|
17 | n/a | 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', |
---|
18 | n/a | 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', |
---|
19 | n/a | 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', |
---|
20 | n/a | 'setitem', 'sub', 'truediv', 'truth', 'xor'] |
---|
21 | n/a | |
---|
22 | n/a | from builtins import abs as _abs |
---|
23 | n/a | |
---|
24 | n/a | |
---|
25 | n/a | # Comparison Operations *******************************************************# |
---|
26 | n/a | |
---|
27 | n/a | def lt(a, b): |
---|
28 | n/a | "Same as a < b." |
---|
29 | n/a | return a < b |
---|
30 | n/a | |
---|
31 | n/a | def le(a, b): |
---|
32 | n/a | "Same as a <= b." |
---|
33 | n/a | return a <= b |
---|
34 | n/a | |
---|
35 | n/a | def eq(a, b): |
---|
36 | n/a | "Same as a == b." |
---|
37 | n/a | return a == b |
---|
38 | n/a | |
---|
39 | n/a | def ne(a, b): |
---|
40 | n/a | "Same as a != b." |
---|
41 | n/a | return a != b |
---|
42 | n/a | |
---|
43 | n/a | def ge(a, b): |
---|
44 | n/a | "Same as a >= b." |
---|
45 | n/a | return a >= b |
---|
46 | n/a | |
---|
47 | n/a | def gt(a, b): |
---|
48 | n/a | "Same as a > b." |
---|
49 | n/a | return a > b |
---|
50 | n/a | |
---|
51 | n/a | # Logical Operations **********************************************************# |
---|
52 | n/a | |
---|
53 | n/a | def not_(a): |
---|
54 | n/a | "Same as not a." |
---|
55 | n/a | return not a |
---|
56 | n/a | |
---|
57 | n/a | def truth(a): |
---|
58 | n/a | "Return True if a is true, False otherwise." |
---|
59 | n/a | return True if a else False |
---|
60 | n/a | |
---|
61 | n/a | def is_(a, b): |
---|
62 | n/a | "Same as a is b." |
---|
63 | n/a | return a is b |
---|
64 | n/a | |
---|
65 | n/a | def is_not(a, b): |
---|
66 | n/a | "Same as a is not b." |
---|
67 | n/a | return a is not b |
---|
68 | n/a | |
---|
69 | n/a | # Mathematical/Bitwise Operations *********************************************# |
---|
70 | n/a | |
---|
71 | n/a | def abs(a): |
---|
72 | n/a | "Same as abs(a)." |
---|
73 | n/a | return _abs(a) |
---|
74 | n/a | |
---|
75 | n/a | def add(a, b): |
---|
76 | n/a | "Same as a + b." |
---|
77 | n/a | return a + b |
---|
78 | n/a | |
---|
79 | n/a | def and_(a, b): |
---|
80 | n/a | "Same as a & b." |
---|
81 | n/a | return a & b |
---|
82 | n/a | |
---|
83 | n/a | def floordiv(a, b): |
---|
84 | n/a | "Same as a // b." |
---|
85 | n/a | return a // b |
---|
86 | n/a | |
---|
87 | n/a | def index(a): |
---|
88 | n/a | "Same as a.__index__()." |
---|
89 | n/a | return a.__index__() |
---|
90 | n/a | |
---|
91 | n/a | def inv(a): |
---|
92 | n/a | "Same as ~a." |
---|
93 | n/a | return ~a |
---|
94 | n/a | invert = inv |
---|
95 | n/a | |
---|
96 | n/a | def lshift(a, b): |
---|
97 | n/a | "Same as a << b." |
---|
98 | n/a | return a << b |
---|
99 | n/a | |
---|
100 | n/a | def mod(a, b): |
---|
101 | n/a | "Same as a % b." |
---|
102 | n/a | return a % b |
---|
103 | n/a | |
---|
104 | n/a | def mul(a, b): |
---|
105 | n/a | "Same as a * b." |
---|
106 | n/a | return a * b |
---|
107 | n/a | |
---|
108 | n/a | def matmul(a, b): |
---|
109 | n/a | "Same as a @ b." |
---|
110 | n/a | return a @ b |
---|
111 | n/a | |
---|
112 | n/a | def neg(a): |
---|
113 | n/a | "Same as -a." |
---|
114 | n/a | return -a |
---|
115 | n/a | |
---|
116 | n/a | def or_(a, b): |
---|
117 | n/a | "Same as a | b." |
---|
118 | n/a | return a | b |
---|
119 | n/a | |
---|
120 | n/a | def pos(a): |
---|
121 | n/a | "Same as +a." |
---|
122 | n/a | return +a |
---|
123 | n/a | |
---|
124 | n/a | def pow(a, b): |
---|
125 | n/a | "Same as a ** b." |
---|
126 | n/a | return a ** b |
---|
127 | n/a | |
---|
128 | n/a | def rshift(a, b): |
---|
129 | n/a | "Same as a >> b." |
---|
130 | n/a | return a >> b |
---|
131 | n/a | |
---|
132 | n/a | def sub(a, b): |
---|
133 | n/a | "Same as a - b." |
---|
134 | n/a | return a - b |
---|
135 | n/a | |
---|
136 | n/a | def truediv(a, b): |
---|
137 | n/a | "Same as a / b." |
---|
138 | n/a | return a / b |
---|
139 | n/a | |
---|
140 | n/a | def xor(a, b): |
---|
141 | n/a | "Same as a ^ b." |
---|
142 | n/a | return a ^ b |
---|
143 | n/a | |
---|
144 | n/a | # Sequence Operations *********************************************************# |
---|
145 | n/a | |
---|
146 | n/a | def concat(a, b): |
---|
147 | n/a | "Same as a + b, for a and b sequences." |
---|
148 | n/a | if not hasattr(a, '__getitem__'): |
---|
149 | n/a | msg = "'%s' object can't be concatenated" % type(a).__name__ |
---|
150 | n/a | raise TypeError(msg) |
---|
151 | n/a | return a + b |
---|
152 | n/a | |
---|
153 | n/a | def contains(a, b): |
---|
154 | n/a | "Same as b in a (note reversed operands)." |
---|
155 | n/a | return b in a |
---|
156 | n/a | |
---|
157 | n/a | def countOf(a, b): |
---|
158 | n/a | "Return the number of times b occurs in a." |
---|
159 | n/a | count = 0 |
---|
160 | n/a | for i in a: |
---|
161 | n/a | if i == b: |
---|
162 | n/a | count += 1 |
---|
163 | n/a | return count |
---|
164 | n/a | |
---|
165 | n/a | def delitem(a, b): |
---|
166 | n/a | "Same as del a[b]." |
---|
167 | n/a | del a[b] |
---|
168 | n/a | |
---|
169 | n/a | def getitem(a, b): |
---|
170 | n/a | "Same as a[b]." |
---|
171 | n/a | return a[b] |
---|
172 | n/a | |
---|
173 | n/a | def indexOf(a, b): |
---|
174 | n/a | "Return the first index of b in a." |
---|
175 | n/a | for i, j in enumerate(a): |
---|
176 | n/a | if j == b: |
---|
177 | n/a | return i |
---|
178 | n/a | else: |
---|
179 | n/a | raise ValueError('sequence.index(x): x not in sequence') |
---|
180 | n/a | |
---|
181 | n/a | def setitem(a, b, c): |
---|
182 | n/a | "Same as a[b] = c." |
---|
183 | n/a | a[b] = c |
---|
184 | n/a | |
---|
185 | n/a | def length_hint(obj, default=0): |
---|
186 | n/a | """ |
---|
187 | n/a | Return an estimate of the number of items in obj. |
---|
188 | n/a | This is useful for presizing containers when building from an iterable. |
---|
189 | n/a | |
---|
190 | n/a | If the object supports len(), the result will be exact. Otherwise, it may |
---|
191 | n/a | over- or under-estimate by an arbitrary amount. The result will be an |
---|
192 | n/a | integer >= 0. |
---|
193 | n/a | """ |
---|
194 | n/a | if not isinstance(default, int): |
---|
195 | n/a | msg = ("'%s' object cannot be interpreted as an integer" % |
---|
196 | n/a | type(default).__name__) |
---|
197 | n/a | raise TypeError(msg) |
---|
198 | n/a | |
---|
199 | n/a | try: |
---|
200 | n/a | return len(obj) |
---|
201 | n/a | except TypeError: |
---|
202 | n/a | pass |
---|
203 | n/a | |
---|
204 | n/a | try: |
---|
205 | n/a | hint = type(obj).__length_hint__ |
---|
206 | n/a | except AttributeError: |
---|
207 | n/a | return default |
---|
208 | n/a | |
---|
209 | n/a | try: |
---|
210 | n/a | val = hint(obj) |
---|
211 | n/a | except TypeError: |
---|
212 | n/a | return default |
---|
213 | n/a | if val is NotImplemented: |
---|
214 | n/a | return default |
---|
215 | n/a | if not isinstance(val, int): |
---|
216 | n/a | msg = ('__length_hint__ must be integer, not %s' % |
---|
217 | n/a | type(val).__name__) |
---|
218 | n/a | raise TypeError(msg) |
---|
219 | n/a | if val < 0: |
---|
220 | n/a | msg = '__length_hint__() should return >= 0' |
---|
221 | n/a | raise ValueError(msg) |
---|
222 | n/a | return val |
---|
223 | n/a | |
---|
224 | n/a | # Generalized Lookup Objects **************************************************# |
---|
225 | n/a | |
---|
226 | n/a | class attrgetter: |
---|
227 | n/a | """ |
---|
228 | n/a | Return a callable object that fetches the given attribute(s) from its operand. |
---|
229 | n/a | After f = attrgetter('name'), the call f(r) returns r.name. |
---|
230 | n/a | After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date). |
---|
231 | n/a | After h = attrgetter('name.first', 'name.last'), the call h(r) returns |
---|
232 | n/a | (r.name.first, r.name.last). |
---|
233 | n/a | """ |
---|
234 | n/a | __slots__ = ('_attrs', '_call') |
---|
235 | n/a | |
---|
236 | n/a | def __init__(self, attr, *attrs): |
---|
237 | n/a | if not attrs: |
---|
238 | n/a | if not isinstance(attr, str): |
---|
239 | n/a | raise TypeError('attribute name must be a string') |
---|
240 | n/a | self._attrs = (attr,) |
---|
241 | n/a | names = attr.split('.') |
---|
242 | n/a | def func(obj): |
---|
243 | n/a | for name in names: |
---|
244 | n/a | obj = getattr(obj, name) |
---|
245 | n/a | return obj |
---|
246 | n/a | self._call = func |
---|
247 | n/a | else: |
---|
248 | n/a | self._attrs = (attr,) + attrs |
---|
249 | n/a | getters = tuple(map(attrgetter, self._attrs)) |
---|
250 | n/a | def func(obj): |
---|
251 | n/a | return tuple(getter(obj) for getter in getters) |
---|
252 | n/a | self._call = func |
---|
253 | n/a | |
---|
254 | n/a | def __call__(self, obj): |
---|
255 | n/a | return self._call(obj) |
---|
256 | n/a | |
---|
257 | n/a | def __repr__(self): |
---|
258 | n/a | return '%s.%s(%s)' % (self.__class__.__module__, |
---|
259 | n/a | self.__class__.__qualname__, |
---|
260 | n/a | ', '.join(map(repr, self._attrs))) |
---|
261 | n/a | |
---|
262 | n/a | def __reduce__(self): |
---|
263 | n/a | return self.__class__, self._attrs |
---|
264 | n/a | |
---|
265 | n/a | class itemgetter: |
---|
266 | n/a | """ |
---|
267 | n/a | Return a callable object that fetches the given item(s) from its operand. |
---|
268 | n/a | After f = itemgetter(2), the call f(r) returns r[2]. |
---|
269 | n/a | After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3]) |
---|
270 | n/a | """ |
---|
271 | n/a | __slots__ = ('_items', '_call') |
---|
272 | n/a | |
---|
273 | n/a | def __init__(self, item, *items): |
---|
274 | n/a | if not items: |
---|
275 | n/a | self._items = (item,) |
---|
276 | n/a | def func(obj): |
---|
277 | n/a | return obj[item] |
---|
278 | n/a | self._call = func |
---|
279 | n/a | else: |
---|
280 | n/a | self._items = items = (item,) + items |
---|
281 | n/a | def func(obj): |
---|
282 | n/a | return tuple(obj[i] for i in items) |
---|
283 | n/a | self._call = func |
---|
284 | n/a | |
---|
285 | n/a | def __call__(self, obj): |
---|
286 | n/a | return self._call(obj) |
---|
287 | n/a | |
---|
288 | n/a | def __repr__(self): |
---|
289 | n/a | return '%s.%s(%s)' % (self.__class__.__module__, |
---|
290 | n/a | self.__class__.__name__, |
---|
291 | n/a | ', '.join(map(repr, self._items))) |
---|
292 | n/a | |
---|
293 | n/a | def __reduce__(self): |
---|
294 | n/a | return self.__class__, self._items |
---|
295 | n/a | |
---|
296 | n/a | class methodcaller: |
---|
297 | n/a | """ |
---|
298 | n/a | Return a callable object that calls the given method on its operand. |
---|
299 | n/a | After f = methodcaller('name'), the call f(r) returns r.name(). |
---|
300 | n/a | After g = methodcaller('name', 'date', foo=1), the call g(r) returns |
---|
301 | n/a | r.name('date', foo=1). |
---|
302 | n/a | """ |
---|
303 | n/a | __slots__ = ('_name', '_args', '_kwargs') |
---|
304 | n/a | |
---|
305 | n/a | def __init__(*args, **kwargs): |
---|
306 | n/a | if len(args) < 2: |
---|
307 | n/a | msg = "methodcaller needs at least one argument, the method name" |
---|
308 | n/a | raise TypeError(msg) |
---|
309 | n/a | self = args[0] |
---|
310 | n/a | self._name = args[1] |
---|
311 | n/a | if not isinstance(self._name, str): |
---|
312 | n/a | raise TypeError('method name must be a string') |
---|
313 | n/a | self._args = args[2:] |
---|
314 | n/a | self._kwargs = kwargs |
---|
315 | n/a | |
---|
316 | n/a | def __call__(self, obj): |
---|
317 | n/a | return getattr(obj, self._name)(*self._args, **self._kwargs) |
---|
318 | n/a | |
---|
319 | n/a | def __repr__(self): |
---|
320 | n/a | args = [repr(self._name)] |
---|
321 | n/a | args.extend(map(repr, self._args)) |
---|
322 | n/a | args.extend('%s=%r' % (k, v) for k, v in self._kwargs.items()) |
---|
323 | n/a | return '%s.%s(%s)' % (self.__class__.__module__, |
---|
324 | n/a | self.__class__.__name__, |
---|
325 | n/a | ', '.join(args)) |
---|
326 | n/a | |
---|
327 | n/a | def __reduce__(self): |
---|
328 | n/a | if not self._kwargs: |
---|
329 | n/a | return self.__class__, (self._name,) + self._args |
---|
330 | n/a | else: |
---|
331 | n/a | from functools import partial |
---|
332 | n/a | return partial(self.__class__, self._name, **self._kwargs), self._args |
---|
333 | n/a | |
---|
334 | n/a | |
---|
335 | n/a | # In-place Operations *********************************************************# |
---|
336 | n/a | |
---|
337 | n/a | def iadd(a, b): |
---|
338 | n/a | "Same as a += b." |
---|
339 | n/a | a += b |
---|
340 | n/a | return a |
---|
341 | n/a | |
---|
342 | n/a | def iand(a, b): |
---|
343 | n/a | "Same as a &= b." |
---|
344 | n/a | a &= b |
---|
345 | n/a | return a |
---|
346 | n/a | |
---|
347 | n/a | def iconcat(a, b): |
---|
348 | n/a | "Same as a += b, for a and b sequences." |
---|
349 | n/a | if not hasattr(a, '__getitem__'): |
---|
350 | n/a | msg = "'%s' object can't be concatenated" % type(a).__name__ |
---|
351 | n/a | raise TypeError(msg) |
---|
352 | n/a | a += b |
---|
353 | n/a | return a |
---|
354 | n/a | |
---|
355 | n/a | def ifloordiv(a, b): |
---|
356 | n/a | "Same as a //= b." |
---|
357 | n/a | a //= b |
---|
358 | n/a | return a |
---|
359 | n/a | |
---|
360 | n/a | def ilshift(a, b): |
---|
361 | n/a | "Same as a <<= b." |
---|
362 | n/a | a <<= b |
---|
363 | n/a | return a |
---|
364 | n/a | |
---|
365 | n/a | def imod(a, b): |
---|
366 | n/a | "Same as a %= b." |
---|
367 | n/a | a %= b |
---|
368 | n/a | return a |
---|
369 | n/a | |
---|
370 | n/a | def imul(a, b): |
---|
371 | n/a | "Same as a *= b." |
---|
372 | n/a | a *= b |
---|
373 | n/a | return a |
---|
374 | n/a | |
---|
375 | n/a | def imatmul(a, b): |
---|
376 | n/a | "Same as a @= b." |
---|
377 | n/a | a @= b |
---|
378 | n/a | return a |
---|
379 | n/a | |
---|
380 | n/a | def ior(a, b): |
---|
381 | n/a | "Same as a |= b." |
---|
382 | n/a | a |= b |
---|
383 | n/a | return a |
---|
384 | n/a | |
---|
385 | n/a | def ipow(a, b): |
---|
386 | n/a | "Same as a **= b." |
---|
387 | n/a | a **=b |
---|
388 | n/a | return a |
---|
389 | n/a | |
---|
390 | n/a | def irshift(a, b): |
---|
391 | n/a | "Same as a >>= b." |
---|
392 | n/a | a >>= b |
---|
393 | n/a | return a |
---|
394 | n/a | |
---|
395 | n/a | def isub(a, b): |
---|
396 | n/a | "Same as a -= b." |
---|
397 | n/a | a -= b |
---|
398 | n/a | return a |
---|
399 | n/a | |
---|
400 | n/a | def itruediv(a, b): |
---|
401 | n/a | "Same as a /= b." |
---|
402 | n/a | a /= b |
---|
403 | n/a | return a |
---|
404 | n/a | |
---|
405 | n/a | def ixor(a, b): |
---|
406 | n/a | "Same as a ^= b." |
---|
407 | n/a | a ^= b |
---|
408 | n/a | return a |
---|
409 | n/a | |
---|
410 | n/a | |
---|
411 | n/a | try: |
---|
412 | n/a | from _operator import * |
---|
413 | n/a | except ImportError: |
---|
414 | n/a | pass |
---|
415 | n/a | else: |
---|
416 | n/a | from _operator import __doc__ |
---|
417 | n/a | |
---|
418 | n/a | # All of these "__func__ = func" assignments have to happen after importing |
---|
419 | n/a | # from _operator to make sure they're set to the right function |
---|
420 | n/a | __lt__ = lt |
---|
421 | n/a | __le__ = le |
---|
422 | n/a | __eq__ = eq |
---|
423 | n/a | __ne__ = ne |
---|
424 | n/a | __ge__ = ge |
---|
425 | n/a | __gt__ = gt |
---|
426 | n/a | __not__ = not_ |
---|
427 | n/a | __abs__ = abs |
---|
428 | n/a | __add__ = add |
---|
429 | n/a | __and__ = and_ |
---|
430 | n/a | __floordiv__ = floordiv |
---|
431 | n/a | __index__ = index |
---|
432 | n/a | __inv__ = inv |
---|
433 | n/a | __invert__ = invert |
---|
434 | n/a | __lshift__ = lshift |
---|
435 | n/a | __mod__ = mod |
---|
436 | n/a | __mul__ = mul |
---|
437 | n/a | __matmul__ = matmul |
---|
438 | n/a | __neg__ = neg |
---|
439 | n/a | __or__ = or_ |
---|
440 | n/a | __pos__ = pos |
---|
441 | n/a | __pow__ = pow |
---|
442 | n/a | __rshift__ = rshift |
---|
443 | n/a | __sub__ = sub |
---|
444 | n/a | __truediv__ = truediv |
---|
445 | n/a | __xor__ = xor |
---|
446 | n/a | __concat__ = concat |
---|
447 | n/a | __contains__ = contains |
---|
448 | n/a | __delitem__ = delitem |
---|
449 | n/a | __getitem__ = getitem |
---|
450 | n/a | __setitem__ = setitem |
---|
451 | n/a | __iadd__ = iadd |
---|
452 | n/a | __iand__ = iand |
---|
453 | n/a | __iconcat__ = iconcat |
---|
454 | n/a | __ifloordiv__ = ifloordiv |
---|
455 | n/a | __ilshift__ = ilshift |
---|
456 | n/a | __imod__ = imod |
---|
457 | n/a | __imul__ = imul |
---|
458 | n/a | __imatmul__ = imatmul |
---|
459 | n/a | __ior__ = ior |
---|
460 | n/a | __ipow__ = ipow |
---|
461 | n/a | __irshift__ = irshift |
---|
462 | n/a | __isub__ = isub |
---|
463 | n/a | __itruediv__ = itruediv |
---|
464 | n/a | __ixor__ = ixor |
---|