Skip to content

Commit d1c6146

Browse files
committed
Fix some bugs+Adding colors on prompt
Adding a third arg for restoring the last resutl, and fixing deviding by zero issue
1 parent 48c076e commit d1c6146

3 files changed

Lines changed: 142 additions & 53 deletions

File tree

cmpx/Complex.py

Lines changed: 141 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,122 +4,211 @@
44
55
@author: Omar Belghaouti
66
"""
7+
from termcolor import colored
78
from math import sqrt
89
# Complex class for complex number manipulations
910
class Complex():
1011
# Constructor
11-
def __init__(self, re=0, im=0):
12+
# re : is the real part of complex number
13+
# im : is the imaginary part of complex number
14+
# restore : whenever an error occurs on any operation of a complex number, the last instance will be restored (By default it's true)
15+
def __init__(self, re=0, im=0, restore=True):
1216
self.re = re
1317
self.im = im
18+
self.restore = restore
1419
# Operator overloading 1 : +
1520
def __add__(self, other):
16-
if not isinstance(other, Complex):
17-
other = Complex(other)
18-
return Complex(self.re + other.re, self.im + other.im)
21+
try:
22+
if other is None:
23+
raise ValueError('The second number is None')
24+
if not isinstance(other, Complex):
25+
other = Complex(other)
26+
return Complex(self.re + other.re, self.im + other.im)
27+
except ValueError as err:
28+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
1929
# Operator overloading 2 : -
2030
def __sub__(self, other):
21-
if not isinstance(other, Complex):
22-
other = Complex(other)
23-
return Complex(self.re - other.re, self.im - other.im)
31+
try:
32+
if other is None:
33+
raise ValueError('The second number is None')
34+
if not isinstance(other, Complex):
35+
other = Complex(other)
36+
return Complex(self.re - other.re, self.im - other.im)
37+
except ValueError as err:
38+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
2439
# Operator overloading 3 : *
2540
def __mul__(self, other):
26-
if not isinstance(other, Complex):
27-
other = Complex(other)
28-
return Complex(self.re * other.re - self.im * other.im, self.re * other.im + self.im * other.re)
41+
try:
42+
if other is None:
43+
raise ValueError('The second number is None')
44+
if not isinstance(other, Complex):
45+
other = Complex(other)
46+
return Complex(self.re * other.re - self.im * other.im, self.re * other.im + self.im * other.re)
47+
except ValueError as err:
48+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
2949
# Operator overloading 4 : /
3050
def __truediv__(self, other):
3151
try:
52+
if other is None:
53+
raise ValueError('The second number is None')
3254
if not isinstance(other, Complex):
3355
other = Complex(other)
3456
den = other * other.con()
3557
num = self * other.con()
58+
if den.re == 0 and self.restore:
59+
print(colored('Float division by zero', 'red'))
60+
print(colored('Restoring last number', 'green'))
61+
return Complex(self.re, self.im)
3662
return Complex(num.re / den.re, num.im / den.re)
37-
except ZeroDivisionError as err:
38-
print('Error: {}'.format(err))
63+
except (ZeroDivisionError, ValueError) as err:
64+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
3965
# Operator overloading 5 : //
4066
def __floordiv__(self, other):
4167
try:
68+
if other is None:
69+
raise ValueError('The second number is None')
4270
if not isinstance(other, Complex):
4371
other = Complex(other)
4472
den = other * other.con()
4573
num = self * other.con()
74+
if den.re == 0 and self.restore:
75+
print(colored('Float division by zero', 'red'))
76+
print(colored('Restoring last number', 'green'))
77+
return Complex(self.re, self.im)
4678
return Complex(num.re // den.re, num.im // den.re)
47-
except ZeroDivisionError as err:
48-
print('Error: {}'.format(err))
79+
except (ZeroDivisionError, ValueError) as err:
80+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
4981
def __gt__(self, other):
50-
if not isinstance(other, Complex):
51-
other = Complex(other)
52-
return self.mod() > other.mod()
82+
try:
83+
if other is None:
84+
raise ValueError('The second number is None')
85+
if not isinstance(other, Complex):
86+
other = Complex(other)
87+
return self.mod() > other.mod()
88+
except ValueError as err:
89+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
5390
# Operator overloading 7 : >=
5491
def __ge__(self, other):
55-
if not isinstance(other, Complex):
56-
other = Complex(other)
57-
return self.mod() >= other.mod()
92+
try:
93+
if other is None:
94+
raise ValueError('The second number is None')
95+
if not isinstance(other, Complex):
96+
other = Complex(other)
97+
return self.mod() >= other.mod()
98+
except ValueError as err:
99+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
58100
# Operator overloading 8: <
59101
def __lt__(self, other):
60-
if not isinstance(other, Complex):
61-
other = Complex(other)
62-
return not self >= other
102+
try:
103+
if other is None:
104+
raise ValueError('The second number is None')
105+
if not isinstance(other, Complex):
106+
other = Complex(other)
107+
return not self >= other
108+
except ValueError as err:
109+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
63110
# Operator overloading 9: <=
64111
def __le__(self, other):
65-
if not isinstance(other, Complex):
66-
other = Complex(other)
67-
return not self > other
112+
try:
113+
if other is None:
114+
raise ValueError('The second number is None')
115+
if not isinstance(other, Complex):
116+
other = Complex(other)
117+
return not self > other
118+
except ValueError as err:
119+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
68120
# Operator overloading 10: ==
69121
def __eq__(self, other):
70-
if not isinstance(other, Complex):
71-
other = Complex(other)
72-
return (self.re == other.re) and (self.im == other.im)
122+
try:
123+
if other is None:
124+
raise ValueError('The second number is None')
125+
if not isinstance(other, Complex):
126+
other = Complex(other)
127+
return (self.re == other.re) and (self.im == other.im)
128+
except ValueError:
129+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
73130
# Operator overloading 11: !=
74131
def __ne__(self, other):
75-
if not isinstance(other, Complex):
76-
other = Complex(other)
77-
return not self == other
132+
try:
133+
if other is None:
134+
raise ValueError('The second number is None')
135+
if not isinstance(other, Complex):
136+
other = Complex(other)
137+
return not self == other
138+
except ValueError as err:
139+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
78140
# Operator overloading 12: +=
79141
def __iadd__(self, other):
80-
if not isinstance(other, Complex):
81-
other = Complex(other)
82-
self.re += other.re
83-
self.im += other.im
84-
return Complex(self.re, self.im)
142+
try:
143+
if other is None:
144+
raise ValueError('The second number is None')
145+
if not isinstance(other, Complex):
146+
other = Complex(other)
147+
self.re += other.re
148+
self.im += other.im
149+
return Complex(self.re, self.im)
150+
except ValueError as err:
151+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
85152
# Operator overloading 13: -=
86153
def __isub__(self, other):
87-
if not isinstance(other, Complex):
88-
other = Complex(other)
89-
self.re -= other.re
90-
self.im -= other.im
91-
return Complex(self.re, self.im)
154+
try:
155+
if other is None:
156+
raise ValueError('The second number is None')
157+
if not isinstance(other, Complex):
158+
other = Complex(other)
159+
self.re -= other.re
160+
self.im -= other.im
161+
return Complex(self.re, self.im)
162+
except ValueError as err:
163+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
92164
# Operator overloading 14: *=
93165
def __imul__(self, other):
94-
if not isinstance(other, Complex):
95-
other = Complex(other)
96-
self.re = self.re * other.re - self.im * other.im
97-
self.im = self.re * other.im + self.im * other.re
98-
return Complex(self.re, self.im)
166+
try:
167+
if other is None:
168+
raise ValueError('The second number is None')
169+
if not isinstance(other, Complex):
170+
other = Complex(other)
171+
self.re = self.re * other.re - self.im * other.im
172+
self.im = self.re * other.im + self.im * other.re
173+
return Complex(self.re, self.im)
174+
except ValueError as err:
175+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
99176
# Operator overloading 15: /=
100177
def __idiv__(self, other):
101178
try:
179+
if other is None:
180+
raise ValueError('The second number is None')
102181
if not isinstance(other, Complex):
103182
other = Complex(other)
104183
den = other * other.con()
105184
num = self * other.con()
185+
if den.re == 0 and self.restore:
186+
print(colored('Float division by zero', 'red'))
187+
print(colored('Restoring last number', 'green'))
188+
return Complex(self.re, self.im)
106189
self.re = num.re / den.re
107190
self.im = num.im / den.re
108-
return Complex(self.re, self.im)
109-
except ZeroDivisionError as err:
110-
print('Error: {}'.format(err))
191+
return Complex(re, im)
192+
except (ZeroDivisionError, ValueError) as err:
193+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
111194
# Operator overloading 16: //=
112195
def __ifloordiv__(self, other):
113196
try:
197+
if other is None:
198+
raise ValueError('The second number is None')
114199
if not isinstance(other, Complex):
115200
other = Complex(other)
116201
den = other * other.con()
117202
num = self * other.con()
203+
if den.re == 0 and self.restore:
204+
print(colored('Float division by zero', 'red'))
205+
print(colored('Restoring last number', 'green'))
206+
return Complex(self.re, self.im)
118207
self.re = num.re // den.re
119208
self.im = num.im // den.re
120209
return Complex(self.re, self.im)
121-
except ZeroDivisionError as err:
122-
print('Error: {}'.format(err))
210+
except (ZeroDivisionError, ValueError) as err:
211+
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
123212
## Helper functions
124213
# Module function to calculate the modulus of a complex number
125214
def mod(self):
7.32 KB
Binary file not shown.

tests/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
@author: Omar Belghaouti
66
"""
7-
from cmpx import Complex
7+
from cmpx.Complex import Complex
88

99
def main():
1010
# Instanciation 1

0 commit comments

Comments
 (0)