mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-19 05:24:06 +08:00
* Convert all text strings to f-strings Reformats all the text from the old "%-formatted" and .format(...) format to the newer f-string format, as defined in PEP 498. This requires Python 3.6+. Flynt 0.69 was used to reformat the strings. 120 f-strings were created in 51 files. F-strings are in general more readable, concise and performant. See also: https://www.python.org/dev/peps/pep-0498/#rationale * revert pyextra changes * revert ublox.py Co-authored-by: Willem Melching <willem.melching@gmail.com>
28 lines
699 B
Python
28 lines
699 B
Python
import signal
|
|
|
|
class TimeoutException(Exception):
|
|
pass
|
|
|
|
class Timeout:
|
|
"""
|
|
Timeout context manager.
|
|
For example this code will raise a TimeoutException:
|
|
with Timeout(seconds=5, error_msg="Sleep was too long"):
|
|
time.sleep(10)
|
|
"""
|
|
def __init__(self, seconds, error_msg=None):
|
|
if error_msg is None:
|
|
error_msg = f'Timed out after {seconds} seconds'
|
|
self.seconds = seconds
|
|
self.error_msg = error_msg
|
|
|
|
def handle_timeout(self, signume, frame):
|
|
raise TimeoutException(self.error_msg)
|
|
|
|
def __enter__(self):
|
|
signal.signal(signal.SIGALRM, self.handle_timeout)
|
|
signal.alarm(self.seconds)
|
|
|
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
signal.alarm(0)
|