-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.py
55 lines (42 loc) · 1.4 KB
/
chain.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
class Chain:
TYPES = [int, str]
def __init__(self, value):
self.value = value
self._type = Chain.get_type(self.value)
def __call__(self, value):
_type = Chain.get_type(value)
if (_type != self._type) or (_type not in Chain.TYPES):
raise Exception('invalid operation')
elif _type == str:
self.value += f' {value}'
elif _type == int:
self.value += value
if self.value == int(self.value):
self.value = int(self.value)
return self
def __eq__(self, value):
if isinstance(value, Chain):
return self.value == value.value
return self.value == value
@staticmethod
def get_type(value):
if Chain.is_number(value):
return int
elif Chain.is_string(value):
return str
@staticmethod
def is_number(value):
if isinstance(value, int) or isinstance(value, float):
return True
return False
@staticmethod
def is_string(value):
if isinstance(value, str):
return True
return False
def __repr__(self):
return str(self.value)
if __name__ == "__main__":
print(Chain(3)(5)(2.5)) # 10.5
print(Chain('Python')('is')('the')('best')) # Python is the best
print(Chain(64) == 64) # True