added type hints and doctests to arithmetic_analysis/newton_method.py#2259
added type hints and doctests to arithmetic_analysis/newton_method.py#2259cclauss merged 4 commits intoTheAlgorithms:masterfrom spamegg1:master
Conversation
Continuing #2128 Also changed some variable names, made them more descriptive.
arithmetic_analysis/newton_method.py
Outdated
|
|
||
| # function is the f(x) and derivative is the f'(x) | ||
| def newton( | ||
| function: Callable[[float], float], |
There was a problem hiding this comment.
Is there a way to defined a type for Callable[[float], float] and then use that type to simplify the call parameters?
There was a problem hiding this comment.
Hmm... I think there is. I could try defining a type hint alias as described here:
https://docs.python.org/3/library/typing.html#type-aliases
Let me try and see if it passes mypy.
There was a problem hiding this comment.
What do you know? It does work!
arithmetic_analysis/newton_method.py
Outdated
| if derivative(prev_guess) == 0: | ||
| raise ZeroDivisionError("float division by zero, could not find root") | ||
|
|
||
| next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess) |
There was a problem hiding this comment.
| if derivative(prev_guess) == 0: | |
| raise ZeroDivisionError("float division by zero, could not find root") | |
| next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess) | |
| try: | |
| next_guess: float = prev_guess - function(prev_guess) / derivative(prev_guess) | |
| except ZeroDivisionError: | |
| raise ZeroDivisionError("Could not find root") |
Avoid calculating the derivative twice. The error type already says that we have a division by zero -- we do not need to repeat.
There was a problem hiding this comment.
Makes sense, got it.
There was a problem hiding this comment.
Hey @spamegg1, from OSSU here. I was just going through this issue and looking at the changes made and this will create a problem where if there is a ZeroDivisionError then two exceptions will be raised. This PR is merged so it's better for @cclauss to make this small change.
>>> try:
... a = 1/0
... except ZeroDivisionError:
... raise ZeroDivisionError("Not possible")
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ZeroDivisionError: Not possibleWhy? the except block is used to handle the exceptions, if you want to raise an exception inside it you need to use the from clause like so:
>>> try:
... a = 1/0
... except ZeroDivisionError:
... raise ZeroDivisionError("Not possible") from None
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ZeroDivisionError: Not possibleYou can also use from in other ways which I won't go into detail much here but just wanted to point this out. 😁
added a type alias for Callable[[float], float] and cleaned up the exception handling
arithmetic_analysis/newton_method.py
Outdated
| if abs(prev_guess - next_guess) < 10 ** -5: | ||
| return next_guess | ||
| prev_guess = next_guess |
There was a problem hiding this comment.
Lines 36-38 can not raise a ZeroDivisionError so they should not be in the try block as discussed in PEP8.
There was a problem hiding this comment.
OK, didn't know that. Understood.
improved exception handling
|
Hey @spamegg1, TravisCI finished with status TravisBuddy Request Identifier: 7f7e1bf0-d3f9-11ea-9b7b-6b7bbdba586d |
|
Hi @cclauss I know this got merged, but something weird must have happened during the merging process. In your last commit d55491a, Line 30 changed from |
|
It was my mistake... It is now fixed on master. Thanks! |
…TheAlgorithms#2259) * added type hints and doctests to arithmetic_analysis/newton_method.py Continuing TheAlgorithms#2128 Also changed some variable names, made them more descriptive. * Added type hints and doctests to arithmetic_analysis/newton_method.py added a type alias for Callable[[float], float] and cleaned up the exception handling * added type hints and doctests to arithmetic_analysis/newton_method.py improved exception handling * Update newton_method.py Co-authored-by: Christian Clauss <cclauss@me.com>
Continuing #2128
Also changed some variable names, made them more descriptive.
Describe your change:
Checklist: