Skip to content

Commit 87d44f9

Browse files
committed
Made all Exceptions public so they can be handled in client code
1 parent 5f49e24 commit 87d44f9

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

matrix_operations.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
class MatrixOperations:
8-
class __IncompatibleDimensionsError(Exception):
8+
class IncompatibleDimensionsError(Exception):
99
def __str__(self) -> str:
1010
return "Matrices are not compatible.\n" \
1111
"No. of Columns in Matrix 1 have to be the same as No. of Rows in Matrix 2."
@@ -35,7 +35,7 @@ def addition(matrix_1: matrix, matrix_2: matrix) -> matrix:
3535
compatible_dim: bool
3636
dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2)
3737
if not same_dim:
38-
raise MatrixOperations.__IncompatibleDimensionsError
38+
raise MatrixOperations.IncompatibleDimensionsError
3939
sum_m: matrix = [[0 for cols in range(dim_1[1])] for rows in range(dim_1[0])]
4040
for i in range(0, dim_1[0]):
4141
for j in range(0, dim_1[1]):
@@ -51,7 +51,7 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix:
5151
compatible_dim: bool
5252
dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2)
5353
if not same_dim:
54-
raise MatrixOperations.__IncompatibleDimensionsError
54+
raise MatrixOperations.IncompatibleDimensionsError
5555
difference_m: matrix = [[0 for cols in range(dim_1[1])] for rows in range(dim_1[0])]
5656
for i in range(0, dim_1[0]):
5757
for j in range(0, dim_1[1]):
@@ -67,7 +67,7 @@ def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix:
6767
compatible_dim: bool
6868
dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2)
6969
if not compatible_dim:
70-
raise MatrixOperations.__IncompatibleDimensionsError
70+
raise MatrixOperations.IncompatibleDimensionsError
7171
product_m: matrix = [[0 for cols in range(dim_2[1])] for rows in range(dim_1[0])]
7272
for i in range(0, dim_1[0]):
7373
for j in range(0, dim_2[1]):

queue.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
class _BaseQueue:
5-
class _EmptyQueueError(Exception):
5+
class EmptyQueueError(Exception):
66
def __str__(self) -> str:
77
return "Queue is empty."
88

@@ -36,7 +36,7 @@ def __eq__(self, other: 'SingleEndedQueue') -> bool:
3636

3737
def first(self) -> Optional[object]:
3838
if self.is_empty():
39-
raise self._EmptyQueueError
39+
raise self.EmptyQueueError
4040
return self._data[self._front]
4141

4242
def _resize(self, capacity: int) -> None:
@@ -57,7 +57,7 @@ def enqueue_back(self, element: Optional[object]) -> None:
5757

5858
def dequeue_front(self) -> Optional[object]:
5959
if self.is_empty():
60-
raise self._EmptyQueueError
60+
raise self.EmptyQueueError
6161
element: Optional[object] = self._data[self._front]
6262
self._data[self._front] = None
6363
self._front = (self._front + 1) % len(self._data)
@@ -78,7 +78,7 @@ def __eq__(self, other: 'DoubleEndedQueue') -> bool:
7878

7979
def last(self) -> Optional[object]:
8080
if self.is_empty():
81-
raise self._EmptyQueueError
81+
raise self.EmptyQueueError
8282
return self._data[self._back]
8383

8484
def _resize(self, capacity: int) -> None:
@@ -104,7 +104,7 @@ def enqueue_front(self, element: Optional[object]) -> None:
104104

105105
def dequeue_back(self) -> Optional[object]:
106106
if self.is_empty():
107-
raise self._EmptyQueueError
107+
raise self.EmptyQueueError
108108
back: int = (self._front + self._size - 1) % len(self._data)
109109
element: Optional[object] = self._data[back]
110110
self._data[back] = None

stack.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Stack:
2-
class __EmptyStackError(Exception):
2+
class EmptyStackError(Exception):
33
def __str__(self) -> str:
44
return "Stack is empty."
55

@@ -27,7 +27,7 @@ def is_empty(self) -> bool:
2727

2828
def top(self) -> object:
2929
if self.is_empty():
30-
raise Stack.__EmptyStackError
30+
raise Stack.EmptyStackError
3131
return self._data[-1]
3232

3333
def push(self, element: object) -> None:
@@ -36,7 +36,7 @@ def push(self, element: object) -> None:
3636

3737
def pop(self) -> object:
3838
if self.is_empty():
39-
raise Stack.__EmptyStackError
39+
raise Stack.EmptyStackError
4040
self._size -= 1
4141
return self._data.pop()
4242

tower_of_hanoi_stack.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33

44
class TowerOfHanoi:
5-
class __EmptySourceRodError(Exception):
5+
class EmptySourceRodError(Exception):
66
def __str__(self) -> str:
77
return "Number of disks in the source rod cannot be less than 1."
88

9-
class __AlreadySolvedError(Exception):
9+
class AlreadySolvedError(Exception):
1010
def __str__(self) -> str:
1111
return "The puzzle has already been solved. Please reset the puzzle before adding more disks."
1212

@@ -27,7 +27,7 @@ def add_disks(self, no_of_disks: int) -> None:
2727
for disk_id in reversed(range(1, self.__no_of_disks + 1)):
2828
self.__source_rod.push(f'disk {disk_id}')
2929
else:
30-
raise self.__AlreadySolvedError
30+
raise self.AlreadySolvedError
3131

3232
def display(self, show_title: bool = True) -> None:
3333
if show_title:
@@ -57,7 +57,7 @@ def __tower_of_hanoi(self, disks: int, source: Stack, destination: Stack, auxili
5757

5858
def solve(self, display_steps: bool = False) -> None:
5959
if self.__no_of_disks < 1:
60-
raise self.__EmptySourceRodError
60+
raise self.EmptySourceRodError
6161
if display_steps:
6262
print("Steps:")
6363
self.__tower_of_hanoi(len(self.__source_rod), self.__source_rod, self.__destination_rod, self.__auxiliary_rod,

0 commit comments

Comments
 (0)