Skip to content

Commit 5611fa0

Browse files
committed
cubestat: lint fixes
1 parent 78e4e21 commit 5611fa0

File tree

7 files changed

+71
-36
lines changed

7 files changed

+71
-36
lines changed

cubestat/colors.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,34 @@
22

33
from cubestat.common import DisplayMode
44

5+
56
class ColorTheme(DisplayMode):
67
mono = 'mono'
7-
inv = 'inv'
8-
col = 'col'
8+
inv = 'inv'
9+
col = 'col'
10+
911

1012
colors_ansi256 = {
1113
'green': [-1, 150, 107, 22],
12-
'red' : [-1, 224, 181, 138],
13-
'blue' : [-1, 189, 146, 103],
14-
'pink' : [-1, 223, 180, 137],
15-
'gray' : [-1, 7, 8, 0],
16-
'white' : [-1, 8, 7, 15],
14+
'red': [-1, 224, 181, 138],
15+
'blue': [-1, 189, 146, 103],
16+
'pink': [-1, 223, 180, 137],
17+
'gray': [-1, 7, 8, 0],
18+
'white': [-1, 8, 7, 15],
1719
}
1820

1921
light_colormap = {
20-
'cpu': 'green',
21-
'ram': 'pink',
22-
'gpu': 'red',
23-
'ane': 'red',
24-
'disk': 'blue',
22+
'cpu': 'green',
23+
'ram': 'pink',
24+
'gpu': 'red',
25+
'ane': 'red',
26+
'disk': 'blue',
2527
'network': 'blue',
26-
'swap': 'pink',
27-
'power': 'red',
28+
'swap': 'pink',
29+
'power': 'red',
2830
}
2931

32+
3033
def prepare_cells():
3134
chrs = [' ', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█']
3235
cells = {}
@@ -39,10 +42,11 @@ def prepare_cells():
3942
colorpair += 1
4043
return cells
4144

45+
4246
def get_theme(metric, color_mode):
4347
res = {
44-
ColorTheme.mono : 'gray',
45-
ColorTheme.inv : 'white',
46-
ColorTheme.col : light_colormap.get(metric, 'green')
48+
ColorTheme.mono: 'gray',
49+
ColorTheme.inv: 'white',
50+
ColorTheme.col: light_colormap.get(metric, 'green')
4751
}
4852
return res[color_mode]

cubestat/common.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
import math
22
from enum import Enum
33

4+
45
class DisplayMode(Enum):
56
def __str__(self):
67
return self.value
78

89
def next(self):
910
values = list(self.__class__)
1011
return values[(values.index(self) + 1) % len(values)]
11-
12+
1213
def prev(self):
1314
values = list(self.__class__)
1415
return values[(values.index(self) + len(values) - 1) % len(values)]
15-
16+
17+
1618
class SimpleMode(DisplayMode):
1719
show = 'show'
1820
hide = 'hide'
1921

22+
2023
# buckets is a list of factor/label, e.g. [(1024*1024, 'Mb'), (1024, 'Kb'), (1, 'b')]
2124
def format_measurement(curr, mx, buckets):
2225
for lim, unit in buckets[:-1]:
2326
if mx > lim:
2427
return f'{curr / lim :3.0f} {unit}'
2528
return f'{curr :3.0f} {buckets[-1][1]}'
2629

30+
2731
def label2(slice, buckets, idxs):
2832
mx = max(slice)
2933
mx = float(1 if mx == 0 else 2 ** (int((mx - 1)).bit_length()))
3034
return mx, [format_measurement(slice[idx], mx, buckets) for idx in idxs]
3135

36+
3237
def label10(slice, buckets, idxs):
3338
mx = max(slice)
3439
mx = float(1 if mx <= 0 else 10 ** math.ceil(math.log10(mx)))
3540
return mx, [format_measurement(slice[idx], mx, buckets) for idx in idxs]
3641

42+
3743
class RateReader:
3844
def __init__(self, interval_ms):
3945
self.interval_s = interval_ms / 1000.0
@@ -46,10 +52,26 @@ def next(self, key, value):
4652
self.last[key] = value
4753
return res
4854

55+
4956
def label_bytes(values, idxs):
50-
buckets = [(1024 ** 5, 'PB'), (1024 ** 4, 'TB'), (1024 ** 3, 'GB'), (1024 ** 2, 'MB'), (1024, 'KB'), (1, 'Bytes')]
57+
buckets = [
58+
(1024 ** 5, 'PB'),
59+
(1024 ** 4, 'TB'),
60+
(1024 ** 3, 'GB'),
61+
(1024 ** 2, 'MB'),
62+
(1024, 'KB'),
63+
(1, 'Bytes')
64+
]
5165
return label2(values, buckets, idxs)
5266

67+
5368
def label_bytes_per_sec(values, idxs):
54-
buckets = [(1024 ** 5, 'PB/s'), (1024 ** 4, 'TB/s'), (1024 ** 3, 'GB/s'), (1024 ** 2, 'MB/s'), (1024, 'KB/s'), (1, 'Bytes/s')]
69+
buckets = [
70+
(1024 ** 5, 'PB/s'),
71+
(1024 ** 4, 'TB/s'),
72+
(1024 ** 3, 'GB/s'),
73+
(1024 ** 2, 'MB/s'),
74+
(1024, 'KB/s'),
75+
(1, 'Bytes/s')
76+
]
5577
return label2(values, buckets, idxs)

cubestat/cubestat.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@
1717
from cubestat.metrics.registry import get_metrics, metrics_configure_argparse
1818
from cubestat.metrics import cpu, gpu, memory, accel, swap, network, disk, power
1919

20+
2021
class ViewMode(DisplayMode):
2122
off = "off"
2223
one = "one"
2324
all = "all"
2425

26+
2527
class Cubestat:
2628
def __init__(self, stdscr, args):
2729
self.screen = Screen(stdscr)
28-
self.ruler_interval = 20 # chars
30+
self.ruler_interval = 20 # chars
2931

3032
self.lock = Lock()
3133

@@ -39,11 +41,11 @@ def __init__(self, stdscr, args):
3941
self.theme = ColorTheme.col
4042

4143
self.refresh_ms = args.refresh_ms
42-
self.step_s = args.refresh_ms / 1000.0
44+
self.step_s = args.refresh_ms / 1000.0
4345
self.metrics = get_metrics(args)
4446

4547
self.input_handler = InputHandler(self)
46-
self.data_manager = DataManager(args.buffer_size)
48+
self.data_manager = DataManager(args.buffer_size)
4749

4850
def do_read(self, context):
4951
updates = []
@@ -66,7 +68,7 @@ def _ruler_values(self, metric, title, idxs, data):
6668
if self.view == ViewMode.off:
6769
return []
6870
idxs = [idx for idx in idxs if idx < len(data)]
69-
data_indices = [-idx - 1 for idx in idxs]
71+
data_indices = [-idx - 1 for idx in idxs]
7072
_, formatted_values = metric.format(title, data, data_indices)
7173
if self.view == ViewMode.one and len(idxs) > 1:
7274
idxs = idxs[:1]
@@ -79,7 +81,7 @@ def render(self):
7981
exit(0)
8082
if self.snapshots_observed == self.snapshots_rendered and not self.settings_changed:
8183
return
82-
84+
8385
self.screen.render_start()
8486
cols = self.screen.cols
8587
ruler_indices = list(range(0, cols, self.ruler_interval))
@@ -94,7 +96,7 @@ def render(self):
9496

9597
if not show:
9698
continue
97-
99+
98100
if skip > 0:
99101
skip -= 1
100102
continue
@@ -105,12 +107,12 @@ def render(self):
105107
self.screen.render_ruler(indent, title, base_ruler, ruler_values, row)
106108

107109
max_value = self.max_val(metric, title, data_slice)
108-
theme = get_theme(metric_name, self.theme)
110+
theme = get_theme(metric_name, self.theme)
109111
self.screen.render_chart(theme, max_value, data_slice, row)
110112

111113
row += 2
112114
self.snapshots_rendered = self.snapshots_observed
113-
self.settings_changed = False
115+
self.settings_changed = False
114116
if self.view != ViewMode.off:
115117
ruler_times = [(i, f'-{(self.step_s * (i + self.h_shift)):.2f}s') for i in ruler_indices]
116118
self.screen.render_time(base_ruler, ruler_times, row)
@@ -124,10 +126,12 @@ def loop(self, platform):
124126
self.render()
125127
self.input_handler.handle_input()
126128

129+
127130
def start(stdscr, platform, args):
128131
h = Cubestat(stdscr, args)
129132
h.loop(platform)
130133

134+
131135
def main():
132136
logging.basicConfig(filename='/tmp/cubestat.log', level=logging.INFO)
133137
parser = argparse.ArgumentParser("cubestat")

cubestat/data.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import collections
22
import itertools
33

4+
45
class DataManager:
56
def __init__(self, buffer_size):
67
init_series = lambda: collections.deque(maxlen=buffer_size)
7-
init_group = lambda: collections.defaultdict(init_series)
8-
self.data = collections.defaultdict(init_group)
8+
init_group = lambda: collections.defaultdict(init_series)
9+
self.data = collections.defaultdict(init_group)
910

1011
def get_slice(self, series, indent, h_shift, cols, spacing):
1112
data_length = len(series) - h_shift if h_shift > 0 else len(series)

cubestat/input.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import curses
22

3+
34
class InputHandler:
45
def __init__(self, horizon):
56
self.horizon = horizon

cubestat/metrics/accel.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from cubestat.metrics.base_metric import base_metric
33
from cubestat.metrics.registry import cubestat_metric
44

5+
56
@cubestat_metric('darwin')
67
class ane_metric(base_metric):
78
def __init__(self) -> None:
@@ -10,7 +11,7 @@ def __init__(self) -> None:
1011
def get_ane_scaler(self) -> float:
1112
# This is pretty much a guess based on tests on a few models I had available.
1213
# Need anything M3 + Ultra models to test.
13-
# Based on TOPS numbers Apple published, all models seem to have same ANE
14+
# Based on TOPS numbers Apple published, all models seem to have same ANE
1415
# except Ultra having 2x.
1516
ane_power_scalers = {
1617
"M1": 13000.0,
@@ -19,7 +20,7 @@ def get_ane_scaler(self) -> float:
1920
}
2021
# identity the model to get ANE scaler
2122
brand_str = subprocess.check_output(['sysctl', '-n', 'machdep.cpu.brand_string'], text=True)
22-
ane_scaler = 15500 # default to M2
23+
ane_scaler = 15500 # default to M2
2324
for k, v in ane_power_scalers.items():
2425
if k in brand_str:
2526
ane_scaler = v
@@ -32,10 +33,10 @@ def read(self, context):
3233
res = {}
3334
res['ANE util %'] = 100.0 * context['processor']['ane_power'] / self.ane_scaler
3435
return res
35-
36+
3637
def pre(self, title):
3738
return True, ''
38-
39+
3940
def format(self, title, values, idxs):
4041
return 100.0, [f'{values[i]:3.0f}%' for i in idxs]
4142

cubestat/metrics/cpu.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from cubestat.metrics.registry import cubestat_metric
66
from cubestat.common import DisplayMode
77

8+
89
class CPUMode(DisplayMode):
910
all = 'all'
1011
by_cluster = 'by_cluster'
1112
by_core = 'by_core'
1213

1314
def auto_cpu_mode() -> CPUMode:
14-
return CPUMode.all if os.cpu_count() < 20 else CPUMode.by_cluster
15+
return CPUMode.all if os.cpu_count() < 20 else CPUMode.by_cluster
16+
1517

1618
class cpu_metric(base_metric):
1719
def pre(self, title):

0 commit comments

Comments
 (0)