Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.1KB

1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import annotations
  2. import time
  3. from typing import Optional
  4. __all__ = ["Deadline"]
  5. class Deadline:
  6. """
  7. Manage timeouts across multiple steps.
  8. Args:
  9. timeout: Time available in seconds or :obj:`None` if there is no limit.
  10. """
  11. def __init__(self, timeout: Optional[float]) -> None:
  12. self.deadline: Optional[float]
  13. if timeout is None:
  14. self.deadline = None
  15. else:
  16. self.deadline = time.monotonic() + timeout
  17. def timeout(self, *, raise_if_elapsed: bool = True) -> Optional[float]:
  18. """
  19. Calculate a timeout from a deadline.
  20. Args:
  21. raise_if_elapsed (bool): Whether to raise :exc:`TimeoutError`
  22. if the deadline lapsed.
  23. Raises:
  24. TimeoutError: If the deadline lapsed.
  25. Returns:
  26. Time left in seconds or :obj:`None` if there is no limit.
  27. """
  28. if self.deadline is None:
  29. return None
  30. timeout = self.deadline - time.monotonic()
  31. if raise_if_elapsed and timeout <= 0:
  32. raise TimeoutError("timed out")
  33. return timeout