Adds a __repr__ to graph.
[pyutils.git] / src / pyutils / misc_utils.py
index 669b3ef98a20d2b4144da67299c463f73ee46851..f2f098f30959b281815388dd46bf1804bd855353 100644 (file)
@@ -1,15 +1,18 @@
 #!/usr/bin/env python3
 
-# © Copyright 2021-2022, Scott Gasch
+# © Copyright 2021-2023, Scott Gasch
 
 """Miscellaneous utilities."""
 
 import os
+import random
 import sys
 
 
 def is_running_as_root() -> bool:
-    """Returns True if running as root.
+    """
+    Returns:
+        True if running as root, False otherwise.
 
     >>> is_running_as_root()
     False
@@ -18,13 +21,33 @@ def is_running_as_root() -> bool:
 
 
 def debugger_is_attached() -> bool:
-    """Return if the debugger is attached"""
-
-    gettrace = getattr(sys, 'gettrace', lambda: None)
+    """
+    Returns:
+        True if a debugger is attached, False otherwise.
+    """
+    gettrace = getattr(sys, "gettrace", lambda: None)
     return gettrace() is not None
 
 
-if __name__ == '__main__':
+def execute_probabilistically(probability_to_execute: float) -> bool:
+    """
+    Args:
+        probability_to_execute: the probability of returning True.
+
+    Returns:
+        True with a given probability.
+
+    >>> random.seed(22)
+    >>> execute_probabilistically(50.0)
+    False
+    >>> execute_probabilistically(50.0)
+    True
+
+    """
+    return random.uniform(0.0, 100.0) < probability_to_execute
+
+
+if __name__ == "__main__":
     import doctest
 
     doctest.testmod()