From 7ebe2b9593725dd1ca431bab98e7f4307d976768 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Mon, 13 Apr 2020 02:10:21 +0200 Subject: [PATCH] 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> --- neural_network/perceptron.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/neural_network/perceptron.py b/neural_network/perceptron.py index 2a1c46b35..23b409b22 100644 --- a/neural_network/perceptron.py +++ b/neural_network/perceptron.py @@ -19,17 +19,28 @@ class Perceptron: :param learning_rate: learning rate used in optimizing. :param epoch_number: number of epochs to train network on. :param bias: bias value for the network. + + >>> p = Perceptron([], (0, 1, 2)) + Traceback (most recent call last): + ... + ValueError: Sample data can not be empty + >>> p = Perceptron(([0], 1, 2), []) + Traceback (most recent call last): + ... + ValueError: Target data can not be empty + >>> p = Perceptron(([0], 1, 2), (0, 1)) + Traceback (most recent call last): + ... + ValueError: Sample data and Target data do not have matching lengths """ self.sample = sample if len(self.sample) == 0: - raise AttributeError("Sample data can not be empty") + raise ValueError("Sample data can not be empty") self.target = target if len(self.target) == 0: - raise AttributeError("Target data can not be empty") + raise ValueError("Target data can not be empty") if len(self.sample) != len(self.target): - raise AttributeError( - "Sample data and Target data do not have matching lengths" - ) + raise ValueError("Sample data and Target data do not have matching lengths") self.learning_rate = learning_rate self.epoch_number = epoch_number self.bias = bias @@ -98,7 +109,7 @@ class Perceptron: classification: P... """ if len(self.sample) == 0: - raise AttributeError("Sample data can not be empty") + raise ValueError("Sample data can not be empty") sample.insert(0, self.bias) u = 0 for i in range(self.col_sample + 1):