-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
80 lines (69 loc) · 2.18 KB
/
utils.py
File metadata and controls
80 lines (69 loc) · 2.18 KB
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
74
75
76
77
78
79
import math
import numpy as np
def get_percentile(N, percent, key=lambda x:x):
if not N:
return 0
k = (len(N) - 1) * percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0 + d1
def plot_cdf(values, filename):
values.sort()
f = open(filename, "w")
for percent in range(100):
fraction = percent / 100.
f.write("%s\t%s\n" % (fraction, get_percentile(values, fraction)))
f.close()
# expected mem in GB and time in /hours (not millisecond)
def lambda_cost(num, mem, time):
return float(num*0.0000002 + (num*mem*time*0.00001667))
import time
from dataclasses import dataclass, field
@dataclass(order=True)
class PrioritizedItem:
current_time: float
timestamp: float = field(init=False, default_factory=time.time)
data: object = field(compare=False)
def _mkdir(newdir):
"""
works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if type(newdir) is not str:
newdir = str(newdir)
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
_mkdir(head)
if tail:
os.mkdir(newdir)
def autolog(message):
"Automatically log the current function details."
import inspect, logging
# Get the previous frame in the stack, otherwise it would
# be this function!!!
func = inspect.currentframe().f_back.f_code
# Dump the message + the name of this function to the log.
# logging.debug("%s: %s in %s:%i" % (
# message,
# func.co_name,
# func.co_filename,
# func.co_firstlineno
# ))
logging.getLogger('sim').debug("%s: %s in %s:%i" % (
message,
func.co_name,
func.co_filename,
func.co_firstlineno
))