-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession4.py
110 lines (86 loc) · 3.31 KB
/
session4.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import random
import decimal
from decimal import Decimal
class Qualean(int):
def __new__(Qualean, real_value):
if real_value not in (-1,0,1):
raise Exception("Sorry, enter a value between -1, 0, 1")
return int.__new__(Qualean, real_value)
def __init__(self, real_value):
if type(real_value) is str:
if real_value == 'True':
real_value = 1
elif real_value == 'False':
real_value = 0
elif real_value == 'Maybe':
real_value = -1
self.real_value = Decimal(real_value)
decimal.getcontext().prec = 10
self.random_num = Decimal(str(random.uniform(-1,1)))
self.imag_value = self.real_value * self.random_num
def __and__(self, other):
if isinstance(other, Qualean):
return self.imag_value and other.imag_value
else:
return False
def __or__(self, other):
if isinstance(other, Qualean):
return self.imag_value or other.imag_value
else:
return False
def __repr__(self):
return str(self.imag_value)
def __str__(self):
return "ObjectType: Qualean, State : " + ['False','True','Maybe'][int(self.real_value)] + ', Value: ' + str(self.imag_value)
def __add__(self, other):
if isinstance(other, Qualean):
return self.imag_value + other.imag_value
elif isinstance(other, Decimal):
return self.imag_value + other
else:
raise ValueError("Object not a Qualean")
def __eq__(self, other):
if isinstance(other, Qualean):
return self.imag_value == other.imag_value
elif isinstance(other, bool):
return self.__bool__() == other
else:
raise TypeError("'==' only supported between Qualean instances ")
def __float__(self):
return float(self.imag_value)
def __ge__(self, other):
if isinstance(other, Qualean):
return self.imag_value >= other.imag_value
else:
raise TypeError("'>=' only supported between Qualean instances ")
return self.imag_value >= other.imag_value
def __gt__(self, other):
if isinstance(other, Qualean):
return self.imag_value > other.imag_value
else:
raise TypeError("'>' only supported between Qualean instances ")
def __invertsign__(self):
return self.imag_value.copy_negate()
def __le__(self, other):
if isinstance(other, Qualean):
return self.imag_value <= other.imag_value
else:
raise TypeError("'<=' only supported between Qualean instances ")
def __lt__(self, other):
if isinstance(other, Qualean):
return self.imag_value < other.imag_value
else:
raise TypeError("'<' only supported between Qualean instances ")
def __mul__(self, other):
if isinstance(other, Qualean):
return self.imag_value * other.imag_value
else:
raise ValueError("Object not a Qualean")
def __sqrt__(self):
if self.imag_value < 0:
temp = abs(self.imag_value).sqrt()
return complex(0, temp)
else:
return self.imag_value.sqrt()
def __bool__(self):
return self.real_value != 0