-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabstract_structures.py
73 lines (57 loc) · 1.41 KB
/
abstract_structures.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from abc import ABC, abstractmethod
class AbelianGroup(ABC):
"""Abstract class of an abelian group.
Implements __init__, __add__, __neg__, __sub__, __eq__,
and all the r- variations.
"""
@abstractmethod
def __init__(self):
pass
@abstractmethod
def __add__(self, other):
pass
@abstractmethod
def __radd__(self, other):
pass
@abstractmethod
def __neg__(self):
pass
@abstractmethod
def __sub__(self, other):
pass
@abstractmethod
def __rsub__(self, other):
pass
@abstractmethod
def __eq__(self, other):
pass
class Ring(AbelianGroup):
"""Abstract class of a ring. Inherits AbelianGroup.
Implements __mul__ and __rmul__.
"""
@abstractmethod
def __mul__(self, other):
pass
@abstractmethod
def __rmul__(self, other):
pass
@classmethod
def __subclasshook__(cls, subclass):
if subclass is int:
return True
return NotImplemented
class Field(Ring):
"""Abstract class of a field. Inherits Ring.
Implements __truediv__ and __rtruediv__.
"""
@abstractmethod
def __truediv__(self, other):
pass
@abstractmethod
def __rtruediv__(self, other):
pass
@classmethod
def __subclasshook__(cls, subclass):
if subclass is float:
return True
return NotImplemented