Skip to content

Commit d7964de

Browse files
committed
Colored error outputs+Adding restore argument
Used colorama for colored outputs for different platforms (Windows, Linux, OSX). Resotre arguement added to prevent assigning None to the current object (if necessary).
1 parent 1d9c740 commit d7964de

2 files changed

Lines changed: 51 additions & 33 deletions

File tree

cmpx/Complex.py

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,24 @@
44
55
@author: Omar Belghaouti
66
"""
7-
from termcolor import colored
7+
import os
8+
from colorama import init, Fore, Style
89
from math import sqrt
10+
11+
# Check the os to make output colorful in any platform
12+
if os.name == 'nt':
13+
init(convert=True)
14+
elif os.name == 'linux' or 'linux2' or 'darwin':
15+
init(convert=False)
16+
917
# Complex class for complex number manipulations
1018
class Complex():
1119
# Constructor
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)
20+
"""
21+
re : is the real part of complex number
22+
im : is the imaginary part of complex number
23+
restore : whenever an error occurs on any operation of a complex number, the last instance will be restored (By default it's true)
24+
"""
1525
def __init__(self, re=0, im=0, restore=True):
1626
self.re = re
1727
self.im = im
@@ -25,7 +35,7 @@ def __add__(self, other):
2535
other = Complex(other)
2636
return Complex(self.re + other.re, self.im + other.im)
2737
except ValueError as err:
28-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
38+
self.print_err(err)
2939
# Operator overloading 2 : -
3040
def __sub__(self, other):
3141
try:
@@ -35,7 +45,7 @@ def __sub__(self, other):
3545
other = Complex(other)
3646
return Complex(self.re - other.re, self.im - other.im)
3747
except ValueError as err:
38-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
48+
self.print_err(err)
3949
# Operator overloading 3 : *
4050
def __mul__(self, other):
4151
try:
@@ -45,7 +55,7 @@ def __mul__(self, other):
4555
other = Complex(other)
4656
return Complex(self.re * other.re - self.im * other.im, self.re * other.im + self.im * other.re)
4757
except ValueError as err:
48-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
58+
self.print_err(err)
4959
# Operator overloading 4 : /
5060
def __truediv__(self, other):
5161
try:
@@ -56,12 +66,13 @@ def __truediv__(self, other):
5666
den = other * other.con()
5767
num = self * other.con()
5868
if den.re == 0 and self.restore:
59-
print(colored('Float division by zero', 'red'))
60-
print(colored('Restoring last number', 'green'))
69+
print(Fore.RED + 'Float division by zero')
70+
print(Fore.GREEN + 'Restoring last number')
71+
print(Style.RESET_ALL, end='')
6172
return Complex(self.re, self.im)
6273
return Complex(num.re / den.re, num.im / den.re)
6374
except (ZeroDivisionError, ValueError) as err:
64-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
75+
self.print_err(err)
6576
# Operator overloading 5 : //
6677
def __floordiv__(self, other):
6778
try:
@@ -72,12 +83,13 @@ def __floordiv__(self, other):
7283
den = other * other.con()
7384
num = self * other.con()
7485
if den.re == 0 and self.restore:
75-
print(colored('Float division by zero', 'red'))
76-
print(colored('Restoring last number', 'green'))
86+
print(Fore.RED + 'Float division by zero')
87+
print(Fore.GREEN + 'Restoring last number')
88+
print(Style.RESET_ALL, end='')
7789
return Complex(self.re, self.im)
7890
return Complex(num.re // den.re, num.im // den.re)
7991
except (ZeroDivisionError, ValueError) as err:
80-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
92+
self.print_err(err)
8193
def __gt__(self, other):
8294
try:
8395
if other is None:
@@ -86,7 +98,7 @@ def __gt__(self, other):
8698
other = Complex(other)
8799
return self.mod() > other.mod()
88100
except ValueError as err:
89-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
101+
self.print_err(err)
90102
# Operator overloading 7 : >=
91103
def __ge__(self, other):
92104
try:
@@ -96,7 +108,7 @@ def __ge__(self, other):
96108
other = Complex(other)
97109
return self.mod() >= other.mod()
98110
except ValueError as err:
99-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
111+
self.print_err(err)
100112
# Operator overloading 8: <
101113
def __lt__(self, other):
102114
try:
@@ -106,7 +118,7 @@ def __lt__(self, other):
106118
other = Complex(other)
107119
return not self >= other
108120
except ValueError as err:
109-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
121+
self.print_err(err)
110122
# Operator overloading 9: <=
111123
def __le__(self, other):
112124
try:
@@ -116,7 +128,7 @@ def __le__(self, other):
116128
other = Complex(other)
117129
return not self > other
118130
except ValueError as err:
119-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
131+
self.print_err(err)
120132
# Operator overloading 10: ==
121133
def __eq__(self, other):
122134
try:
@@ -125,8 +137,8 @@ def __eq__(self, other):
125137
if not isinstance(other, Complex):
126138
other = Complex(other)
127139
return (self.re == other.re) and (self.im == other.im)
128-
except ValueError:
129-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
140+
except ValueError as err:
141+
self.print_err(err)
130142
# Operator overloading 11: !=
131143
def __ne__(self, other):
132144
try:
@@ -136,7 +148,7 @@ def __ne__(self, other):
136148
other = Complex(other)
137149
return not self == other
138150
except ValueError as err:
139-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
151+
self.print_err(err)
140152
# Operator overloading 12: +=
141153
def __iadd__(self, other):
142154
try:
@@ -148,7 +160,7 @@ def __iadd__(self, other):
148160
self.im += other.im
149161
return Complex(self.re, self.im)
150162
except ValueError as err:
151-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
163+
self.print_err(err)
152164
# Operator overloading 13: -=
153165
def __isub__(self, other):
154166
try:
@@ -160,7 +172,7 @@ def __isub__(self, other):
160172
self.im -= other.im
161173
return Complex(self.re, self.im)
162174
except ValueError as err:
163-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
175+
self.print_err(err)
164176
# Operator overloading 14: *=
165177
def __imul__(self, other):
166178
try:
@@ -172,7 +184,7 @@ def __imul__(self, other):
172184
self.im = self.re * other.im + self.im * other.re
173185
return Complex(self.re, self.im)
174186
except ValueError as err:
175-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
187+
self.print_err(err)
176188
# Operator overloading 15: /=
177189
def __idiv__(self, other):
178190
try:
@@ -183,14 +195,15 @@ def __idiv__(self, other):
183195
den = other * other.con()
184196
num = self * other.con()
185197
if den.re == 0 and self.restore:
186-
print(colored('Float division by zero', 'red'))
187-
print(colored('Restoring last number', 'green'))
198+
print(Fore.RED + 'Float division by zero')
199+
print(Fore.GREEN + 'Restoring last number')
200+
print(Style.RESET_ALL, end='')
188201
return Complex(self.re, self.im)
189202
self.re = num.re / den.re
190203
self.im = num.im / den.re
191-
return Complex(re, im)
204+
return Complex(self.re, self.im)
192205
except (ZeroDivisionError, ValueError) as err:
193-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
206+
self.print_err(err)
194207
# Operator overloading 16: //=
195208
def __ifloordiv__(self, other):
196209
try:
@@ -201,23 +214,28 @@ def __ifloordiv__(self, other):
201214
den = other * other.con()
202215
num = self * other.con()
203216
if den.re == 0 and self.restore:
204-
print(colored('Float division by zero', 'red'))
205-
print(colored('Restoring last number', 'green'))
217+
print(Fore.RED + 'Float division by zero')
218+
print(Fore.GREEN + 'Restoring last number')
219+
print(Style.RESET_ALL, end='')
206220
return Complex(self.re, self.im)
207221
self.re = num.re // den.re
208222
self.im = num.im // den.re
209223
return Complex(self.re, self.im)
210224
except (ZeroDivisionError, ValueError) as err:
211-
print(colored('{}: {}'.format(err.__class__.__name__, err), 'red'))
225+
self.print_err(err)
212226
## Helper functions
213227
# Module function to calculate the modulus of a complex number
214228
def mod(self):
215229
return sqrt(self.re**2 + self.im**2)
216230
# Conjugated of a complex number
217231
def con(self):
218232
return Complex(self.re, - self.im)
219-
# String function
220-
def __str__(self):
233+
# function to print the error message
234+
def print_err(self, err):
235+
print(Fore.RED + '{}: {}'.format(err.__class__.__name__, err))
236+
print(Style.RESET_ALL, end='')
237+
# Representation function for representing a complex number
238+
def __repr__(self):
221239
output = str(self.re) + ' + ' + str(self.im) + 'j' if(self.im != 0) else str(self.re)
222240
output = str(self.re) + ' - ' + str(-self.im) + 'j' if (self.im < 0) else output
223241
return output

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup_args = dict(
77
name='cmpx',
8-
version='0.2',
8+
version='0.3',
99
description='Complex class for different operations on complex numbers',
1010
long_description_content_type='text/markdown',
1111
long_description=README,

0 commit comments

Comments
 (0)