cython

Форк
0
/
ext_auto_richcmp.py 
555 строк · 9.6 Кб
1
# mode: run
2

3
import cython
4
compiled = cython.compiled
5

6

7
@cython.cclass
8
class X(object):
9
    x = cython.declare(cython.int)
10

11
    def __init__(self, x):
12
        self.x = x
13

14
    def __repr__(self):
15
        return "<%d>" % self.x
16

17

18
@cython.cfunc
19
@cython.locals(x=X)
20
def x_of(x):
21
    return x.x
22

23

24
@cython.cclass
25
class ClassEq(X):
26
    """
27
    >>> a = ClassEq(1)
28
    >>> b = ClassEq(2)
29
    >>> c = ClassEq(1)
30
    >>> a == a
31
    True
32
    >>> a != a
33
    False
34

35
    >>> a == b
36
    False
37
    >>> a != b
38
    True
39

40
    >>> a == c
41
    True
42
    >>> a != c
43
    False
44

45
    >>> b == c
46
    False
47
    >>> b != c
48
    True
49

50
    >>> c == a
51
    True
52
    >>> c != a
53
    False
54

55
    >>> b == a
56
    False
57
    >>> b != a
58
    True
59

60
    >>> a < b  # doctest: +ELLIPSIS
61
    Traceback (most recent call last):
62
    TypeError...
63
    >>> a > b  # doctest: +ELLIPSIS
64
    Traceback (most recent call last):
65
    TypeError...
66
    >>> a <= b  # doctest: +ELLIPSIS
67
    Traceback (most recent call last):
68
    TypeError...
69
    >>> a >= b  # doctest: +ELLIPSIS
70
    Traceback (most recent call last):
71
    TypeError...
72

73
    >>> print(a.__eq__.__doc__)
74
    EQ
75
    """
76
    def __eq__(self, other):
77
        """EQ"""
78
        assert 1 <= self.x <= 2
79
        assert isinstance(self, ClassEq), type(self)
80
        if isinstance(other, X):
81
            return self.x == x_of(other)
82
        elif isinstance(other, int):
83
            return self.x < other
84
        return NotImplemented
85

86

87
@cython.cclass
88
class ClassEqNe(ClassEq):
89
    """
90
    >>> a = ClassEqNe(1)
91
    >>> b = ClassEqNe(2)
92
    >>> c = ClassEqNe(1)
93
    >>> a == a
94
    True
95
    >>> a != a
96
    False
97

98
    >>> a == b
99
    False
100
    >>> a != b
101
    True
102

103
    >>> a == c
104
    True
105
    >>> a != c
106
    False
107

108
    >>> b == c
109
    False
110
    >>> b != c
111
    True
112

113
    >>> c == a
114
    True
115
    >>> c != a
116
    False
117

118
    >>> b == a
119
    False
120
    >>> b != a
121
    True
122

123
    >>> a < b  # doctest: +ELLIPSIS
124
    Traceback (most recent call last):
125
    TypeError...
126
    >>> a > b  # doctest: +ELLIPSIS
127
    Traceback (most recent call last):
128
    TypeError...
129
    >>> a <= b  # doctest: +ELLIPSIS
130
    Traceback (most recent call last):
131
    TypeError...
132
    >>> a >= b  # doctest: +ELLIPSIS
133
    Traceback (most recent call last):
134
    TypeError...
135

136
    #>>> print(a.__eq__.__doc__)
137
    #EQ
138
    >>> print(a.__ne__.__doc__)
139
    NE
140
    """
141
    def __ne__(self, other):
142
        """NE"""
143
        assert 1 <= self.x <= 2
144
        assert isinstance(self, ClassEqNe), type(self)
145
        if isinstance(other, X):
146
            return self.x != x_of(other)
147
        elif isinstance(other, int):
148
            return self.x < other
149
        return NotImplemented
150

151

152
@cython.cclass
153
class ClassEqNeGe(ClassEqNe):
154
    """
155
    >>> a = ClassEqNeGe(1)
156
    >>> b = ClassEqNeGe(2)
157
    >>> c = ClassEqNeGe(1)
158
    >>> a == a
159
    True
160
    >>> a != a
161
    False
162
    >>> a >= a
163
    True
164
    >>> a <= a
165
    True
166

167
    >>> a == b
168
    False
169
    >>> a != b
170
    True
171
    >>> a >= b
172
    False
173
    >>> b <= a
174
    False
175

176
    >>> a == c
177
    True
178
    >>> a != c
179
    False
180
    >>> a >= c
181
    True
182
    >>> c <= a
183
    True
184

185
    >>> b == c
186
    False
187
    >>> b != c
188
    True
189
    >>> b >= c
190
    True
191
    >>> c <= b
192
    True
193

194
    >>> c == a
195
    True
196
    >>> c != a
197
    False
198
    >>> c >= a
199
    True
200
    >>> a <= c
201
    True
202

203
    >>> b == a
204
    False
205
    >>> b != a
206
    True
207
    >>> b >= a
208
    True
209
    >>> a <= b
210
    True
211

212
    >>> a < b  # doctest: +ELLIPSIS
213
    Traceback (most recent call last):
214
    TypeError...
215
    >>> a > b  # doctest: +ELLIPSIS
216
    Traceback (most recent call last):
217
    TypeError...
218

219
    >>> 2 <= a
220
    False
221
    >>> a >= 2
222
    False
223
    >>> 1 <= a
224
    True
225
    >>> a >= 1
226
    True
227
    >>> a >= 2
228
    False
229

230
    >>> 'x' <= a  # doctest: +ELLIPSIS
231
    Traceback (most recent call last):
232
    TypeError...
233
    >>> a >= 'x'  # doctest: +ELLIPSIS
234
    Traceback (most recent call last):
235
    TypeError...
236

237
    #>>> print(a.__eq__.__doc__)
238
    #EQ
239
    #>>> print(a.__ne__.__doc__)
240
    #NE
241
    >>> print(a.__ge__.__doc__)
242
    GE
243
   """
244
    def __ge__(self, other):
245
        """GE"""
246
        assert 1 <= self.x <= 2
247
        assert isinstance(self, ClassEqNeGe), type(self)
248
        if isinstance(other, X):
249
            return self.x >= x_of(other)
250
        elif isinstance(other, int):
251
            return self.x >= other
252
        return NotImplemented
253

254

255
@cython.cclass
256
class ClassRichcmpOverride(ClassEqNeGe):
257
    """
258
    >>> a = ClassRichcmpOverride(1)
259
    >>> b = ClassRichcmpOverride(1)
260

261
    >>> a == a
262
    True
263
    >>> a != a
264
    False
265

266
    >>> a != b if compiled else a == b  # Python ignores __richcmp__()
267
    True
268
    >>> a == b if compiled else a != b  # Python ignores __richcmp__()
269
    False
270

271
    >>> if not compiled: raise TypeError  # doctest: +ELLIPSIS
272
    ... else: a >= b  # should no longer work when __richcmp__ is overwritten
273
    Traceback (most recent call last):
274
    TypeError...
275
    """
276
    def __richcmp__(self, other, op):
277
        return NotImplemented
278

279

280
@cython.cclass
281
class ClassLe(X):
282
    """
283
    >>> a = ClassLe(1)
284
    >>> b = ClassLe(2)
285
    >>> c = ClassLe(1)
286

287
    >>> a <= b
288
    True
289
    >>> b >= a
290
    True
291
    >>> b <= a
292
    False
293
    >>> a >= b
294
    False
295

296
    >>> a <= c
297
    True
298
    >>> c >= a
299
    True
300
    >>> c <= a
301
    True
302
    >>> a >= c
303
    True
304

305
    >>> b <= c
306
    False
307
    >>> c >= b
308
    False
309
    >>> c <= b
310
    True
311
    >>> b >= c
312
    True
313

314
    >>> 2 >= a
315
    True
316
    >>> a <= 2
317
    True
318
    >>> 1 >= a
319
    True
320
    >>> a <= 1
321
    True
322
    >>> a <= 0
323
    False
324

325
    >>> 'x' >= a  # doctest: +ELLIPSIS
326
    Traceback (most recent call last):
327
    TypeError...
328
    >>> a <= 'x'  # doctest: +ELLIPSIS
329
    Traceback (most recent call last):
330
    TypeError...
331
    """
332
    def __le__(self, other):
333
        assert 1 <= self.x <= 2
334
        assert isinstance(self, ClassLe), type(self)
335
        if isinstance(other, X):
336
            return self.x <= x_of(other)
337
        elif isinstance(other, int):
338
            return self.x <= other
339
        return NotImplemented
340

341

342
@cython.cclass
343
class ClassLt(X):
344
    """
345
    >>> a = ClassLt(1)
346
    >>> b = ClassLt(2)
347
    >>> c = ClassLt(1)
348

349
    >>> a < b
350
    True
351
    >>> b > a
352
    True
353
    >>> b < a
354
    False
355
    >>> a > b
356
    False
357

358
    >>> a < c
359
    False
360
    >>> c > a
361
    False
362
    >>> c < a
363
    False
364
    >>> a > c
365
    False
366

367
    >>> b < c
368
    False
369
    >>> c > b
370
    False
371
    >>> c < b
372
    True
373
    >>> b > c
374
    True
375

376
    >>> sorted([a, b, c])
377
    [<1>, <1>, <2>]
378
    >>> sorted([b, a, c])
379
    [<1>, <1>, <2>]
380

381
    >>> 2 > a
382
    True
383
    >>> a < 2
384
    True
385
    >>> 1 > a
386
    False
387
    >>> a < 1
388
    False
389

390
    >>> 1 < a  # doctest: +ELLIPSIS
391
    Traceback (most recent call last):
392
    TypeError...
393

394
    >>> 'x' > a  # doctest: +ELLIPSIS
395
    Traceback (most recent call last):
396
    TypeError...
397
    >>> a < 'x'  # doctest: +ELLIPSIS
398
    Traceback (most recent call last):
399
    TypeError...
400
    """
401
    def __lt__(self, other):
402
        assert 1 <= self.x <= 2
403
        assert isinstance(self, ClassLt), type(self)
404
        if isinstance(other, X):
405
            return self.x < x_of(other)
406
        elif isinstance(other, int):
407
            return self.x < other
408
        return NotImplemented
409

410

411
@cython.cclass
412
class ClassLtGtInherited(X):
413
    """
414
    >>> a = ClassLtGtInherited(1)
415
    >>> b = ClassLtGtInherited(2)
416
    >>> c = ClassLtGtInherited(1)
417

418
    >>> a < b
419
    True
420
    >>> b > a
421
    True
422
    >>> b < a
423
    False
424
    >>> a > b
425
    False
426

427
    >>> a < c
428
    False
429
    >>> c > a
430
    False
431
    >>> c < a
432
    False
433
    >>> a > c
434
    False
435

436
    >>> b < c
437
    False
438
    >>> c > b
439
    False
440
    >>> c < b
441
    True
442
    >>> b > c
443
    True
444

445
    >>> sorted([a, b, c])
446
    [<1>, <1>, <2>]
447
    >>> sorted([b, a, c])
448
    [<1>, <1>, <2>]
449
    """
450
    def __gt__(self, other):
451
        assert 1 <= self.x <= 2
452
        assert isinstance(self, ClassLtGtInherited), type(self)
453
        if isinstance(other, X):
454
            return self.x > x_of(other)
455
        elif isinstance(other, int):
456
            return self.x > other
457
        return NotImplemented
458

459

460
@cython.cclass
461
class ClassLtGt(X):
462
    """
463
    >>> a = ClassLtGt(1)
464
    >>> b = ClassLtGt(2)
465
    >>> c = ClassLtGt(1)
466

467
    >>> a < b
468
    True
469
    >>> b > a
470
    True
471
    >>> b < a
472
    False
473
    >>> a > b
474
    False
475

476
    >>> a < c
477
    False
478
    >>> c > a
479
    False
480
    >>> c < a
481
    False
482
    >>> a > c
483
    False
484

485
    >>> b < c
486
    False
487
    >>> c > b
488
    False
489
    >>> c < b
490
    True
491
    >>> b > c
492
    True
493

494
    >>> sorted([a, b, c])
495
    [<1>, <1>, <2>]
496
    >>> sorted([b, a, c])
497
    [<1>, <1>, <2>]
498

499
    >>> 2 > a
500
    True
501
    >>> 2 < a
502
    False
503
    >>> a < 2
504
    True
505
    >>> a > 2
506
    False
507

508
    >>> 'x' > a  # doctest: +ELLIPSIS
509
    Traceback (most recent call last):
510
    TypeError...
511
    >>> 'x' < a  # doctest: +ELLIPSIS
512
    Traceback (most recent call last):
513
    TypeError...
514
    >>> a < 'x'  # doctest: +ELLIPSIS
515
    Traceback (most recent call last):
516
    TypeError...
517
    >>> a > 'x'  # doctest: +ELLIPSIS
518
    Traceback (most recent call last):
519
    TypeError...
520
    """
521
    def __lt__(self, other):
522
        assert 1 <= self.x <= 2
523
        assert isinstance(self, ClassLtGt), type(self)
524
        if isinstance(other, X):
525
            return self.x < x_of(other)
526
        elif isinstance(other, int):
527
            return self.x < other
528
        return NotImplemented
529

530
    def __gt__(self, other):
531
        assert 1 <= self.x <= 2
532
        assert isinstance(self, ClassLtGt), type(self)
533
        if isinstance(other, X):
534
            return self.x > x_of(other)
535
        elif isinstance(other, int):
536
            return self.x > other
537
        return NotImplemented
538

539

540
@cython.cclass
541
class List(list):
542
    """
543
    >>> l = [1, 2, 3, 4]
544
    >>> notl = List(l)
545
    >>> notl == l
546
    False
547
    >>> notl != l     # implemented by base type
548
    False
549
    >>> notl == notl
550
    True
551
    >>> notl != notl  # implemented by base type
552
    False
553
    """
554
    def __eq__(self, other):
555
        return self is other or list(self) != list(other)
556

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.