Adds execute_probabilistically.
authorScott Gasch <[email protected]>
Sat, 18 Feb 2023 19:51:51 +0000 (11:51 -0800)
committerScott Gasch <[email protected]>
Sat, 18 Feb 2023 19:51:51 +0000 (11:51 -0800)
src/pyutils/misc_utils.py

index 632f1796a80126fbcc3fb17a062dcb34acb8570f..0449cd5e130ccb5b6abfcf404a49fd5f0f9cfff2 100644 (file)
@@ -5,6 +5,7 @@
 """Miscellaneous utilities."""
 
 import os
+import random
 import sys
 
 
@@ -24,11 +25,19 @@ def debugger_is_attached() -> bool:
     Returns:
         True if a debugger is attached, False otherwise.
     """
-    gettrace = getattr(sys, 'gettrace', lambda: None)
+    gettrace = getattr(sys, "gettrace", lambda: None)
     return gettrace() is not None
 
 
-if __name__ == '__main__':
+def execute_probabilistically(probability_to_execute: float) -> bool:
+    """
+    Returns:
+        True with a given probability.
+    """
+    return random.uniform(0.0, 100.0) < probability_to_execute
+
+
+if __name__ == "__main__":
     import doctest
 
     doctest.testmod()