-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
73 lines (50 loc) · 1.65 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
""" Grabbag collection of little tools """
from contextlib import contextmanager
from time import sleep
from signal import SIGALRM, signal, getsignal
from math import sin
_sleep = sleep
def sleep(seconds, *args, **kwargs):
try:
_sleep(seconds, *args, **kwargs)
except TypeError:
_sleep(seconds.total_seconds(), *args, **kwargs)
@contextmanager
def running_once(program_name: str, per_user: bool=False):
from fcntl import lockf, LOCK_EX, LOCK_NB, LOCK_UN
from os import getpid, getuid, SEEK_SET
if per_user:
program_name += f'_{getuid()}'
with open(f'/tmp/{program_name}.pid', 'a') as f:
lockf(f, LOCK_EX | LOCK_NB) # EXclusive, Non-Blocking
# Clear the file at this point. We have acquired the lock, and since
# we're opening in r+ instead of w this isn't done automatically for
# us. This is the right point to do it manually.
f.seek(0, SEEK_SET)
f.truncate()
f.write(str(getpid()))
f.flush()
yield
lockf(f, LOCK_UN)
@contextmanager
def signal_interruptable(sig=SIGALRM):
class AlarmException(Exception):
pass
prev_handler = getsignal(sig)
def handler(signal, frame):
raise AlarmException()
try:
signal(sig, handler)
yield
except AlarmException:
# Ignore it, but we are out of the block.
pass
finally:
# Restore the saved handler.
signal(sig, prev_handler)
def between(lower, x, upper):
return min(max(lower, x), upper)
def pos_sin(θ):
return (sin(θ) + 1) / 2
def linearScale(x, lower, upper):
return x * (upper - lower) + lower