-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·58 lines (46 loc) · 1.41 KB
/
utils.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
from functools import wraps
from threading import Thread
import time
import colorsys
import numpy as np
def memoize(function):
"""Provides a decorator for memoizing functions"""
memo = {}
@wraps(function)
def wrapper(*args):
if args in memo:
return memo[args]
else:
rv = function(*args)
memo[args] = rv
return rv
return wrapper
def setTimeout (callback, delay):
def timerExecutor ():
time.sleep(delay)
callback()
timerThread = Thread(target=timerExecutor, daemon=False)
timerThread.start()
def setInterval (callback, interval):
cancelled = False
def timerExecutor ():
nonlocal cancelled
nextCallTimestamp = time.time()
while cancelled != True:
nextCallTimestamp = nextCallTimestamp + interval
# try:
callback()
# except Exception as e:
# print(f'Exception while executing interval callback: {e}')
time.sleep(max(0, nextCallTimestamp - time.time()))
def cancelTimer ():
nonlocal cancelled
cancelled = True
timerThread = Thread(target=timerExecutor, daemon=False)
timerThread.start()
return cancelTimer
def runAsync (callback):
thread = Thread(target=callback, daemon=False)
thread.start()
def hsv2rgb(h,s,v):
return np.array([i for i in colorsys.hsv_to_rgb(h,s,v)])