-
Notifications
You must be signed in to change notification settings - Fork 94
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
Add check for zero to xrandom #3040
Conversation
Output of Trying:
xrandom(0, 0)
Expecting:
(False, {})
ok
Trying:
import sys
Expecting nothing
ok
Trying:
mocked_randint = lambda x, y: 10
Expecting nothing
ok
Trying:
sys.modules[__name__].randint = mocked_randint
Expecting nothing
ok
Trying:
xrandom(20, 0)
Expecting:
(False, {})
ok
Trying:
import sys
Expecting nothing
ok
Trying:
mocked_randint = lambda x, y: 1
Expecting nothing
ok
Trying:
sys.modules[__name__].randint = mocked_randint
Expecting nothing
ok
Trying:
xrandom(1, 0)
Expecting:
(True, {'COLOR': 'orange', 'SIZE': 'small'})
ok
1 items had no tests:
xrandom
1 items passed all tests:
9 tests in xrandom.xrandom
9 tests in 2 items.
9 passed and 0 failed.
Test passed. |
I think it's not necessary to backport to 7.8.x, but happy to prepare another PR for the other branch if others prefer to include this fix there too. |
97b1cc4
to
350f191
Compare
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.
Looks good, thanks 👍
@kinow - as a bug fix for existing code, this probably should be back-ported if you don't mind. |
Codacy:
I wonder why Codacy sees this as a new issue? (same use of |
Sure thing, pull request coming!
I suspect they are simply reporting on the old issue, as if it were a new one? We could add a #noqa marker and see if it goes away, or move the new |
Can you try that? (or should it be #nosec in this case?). It's clearly not a security issue in this usage! |
Roger. Before
With (pushing in a commit in a bit): diff --git a/lib/cylc/xtriggers/xrandom.py b/lib/cylc/xtriggers/xrandom.py
index ea0c48fa9..1b1b1e995 100644
--- a/lib/cylc/xtriggers/xrandom.py
+++ b/lib/cylc/xtriggers/xrandom.py
@@ -74,10 +74,11 @@ def xrandom(percent, secs=0, _=None, debug=False):
"""
sleep(float(secs))
results = {}
- satisfied = int(percent) != 0 and (1 == randint(1, 100 / int(percent)))
+ satisfied = int(percent) != 0 and \
+ (1 == randint(1, 100 / int(percent))) # nosec
if satisfied:
results = {
- 'COLOR': COLORS[randint(0, len(COLORS) - 1)],
- 'SIZE': SIZES[randint(0, len(SIZES) - 1)]
+ 'COLOR': COLORS[randint(0, len(COLORS) - 1)], # nosec
+ 'SIZE': SIZES[randint(0, len(SIZES) - 1)] # nosec
}
return (satisfied, results) Results in:
Let's wait and see what Codacy says after the commit is pushed. |
Oh, and you were right, nosec, not noqa 😁 Thanks! |
73d50d9
to
2df86ba
Compare
@hjoliver looks it still complains about the added
If that looks OK I will cherry-pick in the |
That's great, plz go ahead with the back-port. Thanks! |
@dwsutherland - you've worked with external triggers recently, so can you provide 2nd review for this? (It'll only take a minute - just a small divide-by-zero bug fix, not really something that requires knowing about xtriggers actually). |
How about this (Python 3):
e.g. true with 20% likelihood, if a random float between 0 and 1 is less than 0.2. |
I did have a suggestion above which requires no division:
or
Or am I missing something XD |
It also incorporates the 0 |
Actually I do like your solution @hjoliver .. because if you remove the |
I missed your suggestion, sorry ... but what's wrong with division?! ( |
Yes yours is safe too! (and nothing wrong with non-zero division) |
Looks like |
2df86ba
to
9a4ff65
Compare
Done, ran And I think the code looks much nicer too! Reading the line that checks the likelihood is even easier to understand. 👏 great to have more 👀 reviewing the code. |
Yes, shows both how difficult and how critical reviewing is! (I guess didn't bother to read that bit properly because I wrote it myself some time ago and so assumed it was right, haha 😟 ). Thanks again to @dwsutherland for checking properly (I think he has a maths background 🎉 ) |
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.
Re-approving!
Both pull requests updated, should be ready for review if Travis-CI doesn't fail (which my indicate it wants another kick). |
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.
LGTM!
💥 |
Fix #3039
Prevents
ZeroDivisionError
inxrandom
. Adds docstrings and doctests (thought a unittest for it wouldn't be really necessary as it's an example only? We were running doctests anyway for coverage...)Cheers
Bruno