From b8b26349d3bd71df434ee2de6443d6d1d8166317 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Mon, 25 Mar 2019 11:02:19 +1300 Subject: [PATCH 1/4] Prevent ZeroDivisionError in xrandom, add doctests --- lib/cylc/xtriggers/xrandom.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/cylc/xtriggers/xrandom.py b/lib/cylc/xtriggers/xrandom.py index 43f4ca4cb5b..9cba1df4ddc 100644 --- a/lib/cylc/xtriggers/xrandom.py +++ b/lib/cylc/xtriggers/xrandom.py @@ -22,7 +22,7 @@ """ -from random import randint +from random import random, randint from time import sleep @@ -38,10 +38,34 @@ def xrandom(percent, secs=0, _=None, debug=False): The '_' argument is not used in the function code, but can be used to specialize the function signature to cycle point or task. + If the percent is zero, it returns that the trigger condition was + not satisfied, and an empty dictionary. + >>> xrandom(0, 0) + (False, {}) + + If the percent is not zero, but the random percent success is not met, + then it also returns that the trigger condition was not satisfied, + and an empty dictionary. + >>> import sys + >>> mocked_random = lambda: 0.3 + >>> sys.modules[__name__].random = mocked_random + >>> xrandom(20, 0) + (False, {}) + + Finally, if the percent is not zero, and the random percent success is + met, then it returns that the trigger condition was satisfied, and a + dictionary containing random color and size as result. + >>> import sys + >>> mocked_random = lambda: 0.9 + >>> sys.modules[__name__].random = mocked_random + >>> mocked_randint = lambda x, y: 1 + >>> sys.modules[__name__].randint = mocked_randint + >>> xrandom(99, 0) + (True, {'COLOR': 'orange', 'SIZE': 'small'}) """ sleep(float(secs)) results = {} - satisfied = (1 == randint(1, 100 / int(percent))) + satisfied = random() < int(percent) / 100 if satisfied: results = { 'COLOR': COLORS[randint(0, len(COLORS) - 1)], From a6127d8c03a544fea9391925f5bc19c054226114 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Mon, 25 Mar 2019 11:37:29 +1300 Subject: [PATCH 2/4] Add docstrings --- lib/cylc/xtriggers/xrandom.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/cylc/xtriggers/xrandom.py b/lib/cylc/xtriggers/xrandom.py index 9cba1df4ddc..0f60d05cb93 100644 --- a/lib/cylc/xtriggers/xrandom.py +++ b/lib/cylc/xtriggers/xrandom.py @@ -62,6 +62,17 @@ def xrandom(percent, secs=0, _=None, debug=False): >>> sys.modules[__name__].randint = mocked_randint >>> xrandom(99, 0) (True, {'COLOR': 'orange', 'SIZE': 'small'}) + + Args: + percent (int): percent likelihood. + secs (int): seconds to sleep before starting the trigger. + _ (object): used to allow users to specialize the trigger + with extra parameters. + debug (bool): flag to enable debug information. + Returns: + A tuple with the trigger flag (bool) which is set to True if the + trigger was evaluated successful, False otherwise, and a random + color and size (dict). """ sleep(float(secs)) results = {} From 9a4ff6500f33af7a0262669bb981bb30d82d2dbb Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Tue, 26 Mar 2019 11:22:15 +1300 Subject: [PATCH 3/4] Add nosec markers for bandit --- lib/cylc/xtriggers/xrandom.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/cylc/xtriggers/xrandom.py b/lib/cylc/xtriggers/xrandom.py index 0f60d05cb93..4cecc0ceb73 100644 --- a/lib/cylc/xtriggers/xrandom.py +++ b/lib/cylc/xtriggers/xrandom.py @@ -76,10 +76,10 @@ def xrandom(percent, secs=0, _=None, debug=False): """ sleep(float(secs)) results = {} - satisfied = random() < int(percent) / 100 + satisfied = random() < int(percent) / 100 # 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) From f52e1e2966cb9381a03f1c12b0ca69fa8a0b35c0 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Tue, 26 Mar 2019 16:51:50 +1300 Subject: [PATCH 4/4] Make percent a float --- lib/cylc/xtriggers/xrandom.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/cylc/xtriggers/xrandom.py b/lib/cylc/xtriggers/xrandom.py index 4cecc0ceb73..f340f4e6c66 100644 --- a/lib/cylc/xtriggers/xrandom.py +++ b/lib/cylc/xtriggers/xrandom.py @@ -49,7 +49,7 @@ def xrandom(percent, secs=0, _=None, debug=False): >>> import sys >>> mocked_random = lambda: 0.3 >>> sys.modules[__name__].random = mocked_random - >>> xrandom(20, 0) + >>> xrandom(15.5, 0) (False, {}) Finally, if the percent is not zero, and the random percent success is @@ -60,11 +60,11 @@ def xrandom(percent, secs=0, _=None, debug=False): >>> sys.modules[__name__].random = mocked_random >>> mocked_randint = lambda x, y: 1 >>> sys.modules[__name__].randint = mocked_randint - >>> xrandom(99, 0) + >>> xrandom(99.99, 0) (True, {'COLOR': 'orange', 'SIZE': 'small'}) Args: - percent (int): percent likelihood. + percent (float): percent likelihood. secs (int): seconds to sleep before starting the trigger. _ (object): used to allow users to specialize the trigger with extra parameters. @@ -76,7 +76,7 @@ def xrandom(percent, secs=0, _=None, debug=False): """ sleep(float(secs)) results = {} - satisfied = random() < int(percent) / 100 # nosec + satisfied = random() < float(percent) / 100 # nosec if satisfied: results = { 'COLOR': COLORS[randint(0, len(COLORS) - 1)], # nosec