|
|
@ -21,6 +21,9 @@ class Regression: |
|
|
|
|
|
|
|
|
|
|
|
def predict(self, *, x: int | float = None, y: int | float = None) -> float: |
|
|
|
def predict(self, *, x: int | float = None, y: int | float = None) -> float: |
|
|
|
"""predict a value if x or y is given""" |
|
|
|
"""predict a value if x or y is given""" |
|
|
|
|
|
|
|
if x is not None and y is not None: |
|
|
|
|
|
|
|
msg = "predict() expects one keyword argument 'x' or 'y', got both" |
|
|
|
|
|
|
|
raise TypeError(msg) |
|
|
|
if x is not None: |
|
|
|
if x is not None: |
|
|
|
return self.intercept + x * self.coefficient |
|
|
|
return self.intercept + x * self.coefficient |
|
|
|
if y is not None: |
|
|
|
if y is not None: |
|
|
@ -77,6 +80,9 @@ def test_regression_predict_exceptions(example_data): |
|
|
|
with pytest.raises(TypeError, match="expects a keyword"): |
|
|
|
with pytest.raises(TypeError, match="expects a keyword"): |
|
|
|
regression.predict() |
|
|
|
regression.predict() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="expects one keyword"): |
|
|
|
|
|
|
|
regression.predict(x=1, y=2) |
|
|
|
|
|
|
|
|
|
|
|
with pytest.raises(TypeError, match="takes 1 positional argument but"): |
|
|
|
with pytest.raises(TypeError, match="takes 1 positional argument but"): |
|
|
|
regression.predict(1) |
|
|
|
regression.predict(1) |
|
|
|
|
|
|
|
|
|
|
|