-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
172 lines (139 loc) · 4.73 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# Copyright 2021 Constantin Zaharia <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import math
import datetime
import subprocess
from typing import Tuple
nsec_per_sec = 1000000000
class Dictionary(object):
# Time
# __time_dict = {
# 'Ys': math.pow(10, 24),
# 'Zs': math.pow(10, 21),
# 'Es': math.pow(10, 18),
# 'Ps': math.pow(10, 15),
# 'Ts': math.pow(10, 12),
# 'Gs': math.pow(10, 9),
# ('yr', 'yrs', 'year', 'years'): 31540000,
# ('mo', 'mos', 'month', 'months'): 2628000,
# 'Ms': math.pow(10, 6),
# ('wk', 'week', 'weeks'): 604800,
# 'day': 86400,
# ('hr', 'hour', 'hours'): 3600,
# 'ks': 1000,
# ('min', 'minutes'): 60,
# ('s', 'sec', 'second', 'seconds'): 1,
# 'ds': .1,
# 'cs': .01,
# 'ms': .001,
# 'µs': math.pow(10, -6),
# 'ns': math.pow(10, -9),
# 'ps': math.pow(10, -12),
# 'fs': math.pow(10, -15),
# 'as': math.pow(10, -18),
# 'zs': math.pow(10, -21),
# 'ys': math.pow(10, -24)
# }
__time_dict = {
'Ys': math.pow(10, 24),
'Zs': math.pow(10, 21),
'Es': math.pow(10, 18),
'Ps': math.pow(10, 15),
'Ts': math.pow(10, 12),
'Gs': math.pow(10, 9),
'yr': 31540000,
'mo': 2628000,
'Ms': math.pow(10, 6),
'wk': 604800,
'day': 86400,
'hr': 3600,
'ks': 1000,
'min': 60,
's': 1,
'ds': .1,
'cs': .01,
'ms': .001,
'µs': math.pow(10, -6),
'ns': math.pow(10, -9),
'ps': math.pow(10, -12),
'fs': math.pow(10, -15),
'as': math.pow(10, -18),
'zs': math.pow(10, -21),
'ys': math.pow(10, -24)
}
def time_dict(self):
return self.__time_dict
# TIME CONVERSION
def convert_time(value, units_from_base, units_to_base):
return value * units_from_base / units_to_base
def check_time(value, units_from, units_to, decimal_places=1):
dictionary = Dictionary() # Dictionary object
time_dict = dictionary.time_dict() # Time unit dictionary
# Metric and Imperial distances
if units_from in time_dict and units_to in time_dict:
units_from_base = time_dict.get(
units_from, None) # Conversion to seconds
units_to_base = time_dict.get(
units_to, None) # Conversion from seconds
value = convert_time(value, units_from_base, units_to_base)
return round(value, decimal_places), units_to
return False
def normalize_timespec(ts):
# first convert to raw ns
timspec = check_time(ts, 's', 'ns', 0)
if abs(timspec[0]) >= 1000:
timspec = check_time(ts, 's', 'µs', 0)
if abs(timspec[0]) >= 1000:
timspec = check_time(ts, 's', 'ms', 0)
return timspec
def make_nice(value: float, unit: str = 's'):
prefixes = iter('µm ')
while value > 1000:
value /= 1000
unit = next(prefixes) + unit[-1]
return f'{value:g}{unit}'
def human_time(*args, **kwargs):
secs = float(datetime.timedelta(*args, **kwargs).total_seconds())
units = [("D", 86400), ("h", 3600), ("m", 60), ("s", 1)]
parts = []
for unit, mul in units:
if secs / mul >= 1 or mul == 1:
if mul > 1:
n = int(math.floor(secs / mul))
secs -= n * mul
else:
n = secs if secs != int(secs) else int(secs)
parts.append("%s%s" % (n, unit))
return ",".join(parts)
class SubprocessShell(object):
"""
"""
platform_settings = {'executable': '/bin/bash'}
def shell_run(self, cmd):
"""
:param cmd:
:return:
"""
proc = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True, **self.platform_settings)
return proc.stderr, proc.stdout
def humanize_file_size(filesize):
filesize = abs(filesize)
if filesize == 0:
return "0 Bytes"
p = int(math.floor(math.log(filesize, 2)/10))
return "%0.2f%s" % (filesize/math.pow(1024, p),
['Bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'][p])