-
-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cost zero division #20
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @accurrently, I appreciate you fixing this! I wonder if instead of adding an additional zvalue
keyword, could we just use 1
if nbytes
is 0
. I.e. something along the lines of your original comment (#19 (comment)):
def cost(nbytes, time):
return float(time) / max(nbytes, 1) / 1e9
What do you think about this approach?
We can. Though bytes should never be negative, part of used the if/else statement to protect against a zero value instead of having to worry about negative values. The reason I chose .5 is to differentiate between an object of 1 byte and a value that clearly indicates a zero/null. If the difference in outcome isn't significant, 1 should be fine. If nbytes can be guaranteed to be And frankly, Agree on elimination of the kwarg. |
Co-Authored-By: Matthew Rocklin <mrocklin@gmail.com>
Co-Authored-By: Matthew Rocklin <mrocklin@gmail.com>
…to cost-zero-division
cachey/tests/test_cache.py
Outdated
@@ -1,8 +1,17 @@ | |||
import sys | |||
from time import sleep | |||
|
|||
import pytest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we don't need this import.
cachey/tests/test_cache.py
Outdated
def test_cost(): | ||
|
||
assert cost(200, 500) == 2.5e-09 | ||
assert cost(0, 500) == 5e-07 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not assert that they have to be these values exactly. I think that it would be fine if things just worked, and that the cost was relatively low, like maybe
assert cost(0, 500) == 5e-07 | |
assert 0 <= cost(0, 500) <= 100 |
cachey/tests/test_cache.py
Outdated
from cachey import Cache, Scorer, nbytes | ||
|
||
from cachey.cache import cost | ||
|
||
def test_cost(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_cost(): | |
def test_cost_with_zero_bytes(): |
@accurrently , were you planning to finish this off? |
Thanks for the original work here, @accurrently |
Fix for bug #19.
Updated
cost()
to include a kwarg calledzvalue
which defaults to0.5
. Changes should have little effect on existing calls.It effectively prevents zero values for
nbytes
from creating aZeroDivisionError
by providing a nonzero value less than 1. The function still raisesZeroDivisionError
ifzvalue==0
.New return value: