Skip to content

Commit 7ebe2b9

Browse files
cclaussgithub-actions
and
github-actions
authored
Test the exception conditions (#1853)
* Text exception conditions These are ValueErrors, not AttributeErrors. * fixup! Format Python code with psf/black push * Update perceptron.py * Update perceptron.py * Update perceptron.py * Revert the test Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 3735e74 commit 7ebe2b9

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

neural_network/perceptron.py

+17-6
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@ def __init__(self, sample, target, learning_rate=0.01, epoch_number=1000, bias=-
1919
:param learning_rate: learning rate used in optimizing.
2020
:param epoch_number: number of epochs to train network on.
2121
:param bias: bias value for the network.
22+
23+
>>> p = Perceptron([], (0, 1, 2))
24+
Traceback (most recent call last):
25+
...
26+
ValueError: Sample data can not be empty
27+
>>> p = Perceptron(([0], 1, 2), [])
28+
Traceback (most recent call last):
29+
...
30+
ValueError: Target data can not be empty
31+
>>> p = Perceptron(([0], 1, 2), (0, 1))
32+
Traceback (most recent call last):
33+
...
34+
ValueError: Sample data and Target data do not have matching lengths
2235
"""
2336
self.sample = sample
2437
if len(self.sample) == 0:
25-
raise AttributeError("Sample data can not be empty")
38+
raise ValueError("Sample data can not be empty")
2639
self.target = target
2740
if len(self.target) == 0:
28-
raise AttributeError("Target data can not be empty")
41+
raise ValueError("Target data can not be empty")
2942
if len(self.sample) != len(self.target):
30-
raise AttributeError(
31-
"Sample data and Target data do not have matching lengths"
32-
)
43+
raise ValueError("Sample data and Target data do not have matching lengths")
3344
self.learning_rate = learning_rate
3445
self.epoch_number = epoch_number
3546
self.bias = bias
@@ -98,7 +109,7 @@ def sort(self, sample) -> None:
98109
classification: P...
99110
"""
100111
if len(self.sample) == 0:
101-
raise AttributeError("Sample data can not be empty")
112+
raise ValueError("Sample data can not be empty")
102113
sample.insert(0, self.bias)
103114
u = 0
104115
for i in range(self.col_sample + 1):

0 commit comments

Comments
 (0)