Skip to content

Commit d9cc46d

Browse files
committed
Delete obsolete codes and use blessings for color
1 parent c3b83f2 commit d9cc46d

File tree

2 files changed

+17
-53
lines changed

2 files changed

+17
-53
lines changed

gpustat.py

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,14 @@
2323
import psutil
2424
# wildcard import because the name is too long
2525
from pynvml import *
26+
from blessings import Terminal
2627

2728
__version__ = '0.4.0.dev'
2829

2930

30-
class ANSIColors:
31-
RESET = '\033[0m'
32-
WHITE = '\033[1m'
33-
RED = '\033[0;31m'
34-
GREEN = '\033[0;32m'
35-
YELLOW = '\033[0;33m'
36-
BLUE = '\033[0;34m'
37-
MAGENTA = '\033[0;35m'
38-
CYAN = '\033[0;36m'
39-
GRAY = '\033[1;30m'
40-
BOLD_RED = '\033[1;31m'
41-
BOLD_GREEN = '\033[1;32m'
42-
BOLD_YELLOW = '\033[1;33m'
43-
4431
NOT_SUPPPORTED = 'Not Supported'
4532

46-
@staticmethod
47-
def wrap(color, msg):
48-
return (color + msg + ANSIColors.RESET)
49-
33+
term = Terminal()
5034

5135
def execute_process(command_shell):
5236
stdout = check_output(command_shell, shell=True).strip()
@@ -142,24 +126,24 @@ def print_to(self, fp,
142126
# color settings
143127
colors = {}
144128
def _conditional(cond_fn, true_value, false_value,
145-
error_value=ANSIColors.GRAY):
129+
error_value=term.gray):
146130
try:
147131
if cond_fn(): return true_value
148132
else: return false_value
149133
except:
150134
return error_value
151135

152-
colors['C0'] = ANSIColors.RESET
153-
colors['C1'] = ANSIColors.CYAN
154-
colors['CName'] = ANSIColors.BLUE
136+
colors['C0'] = term.normal
137+
colors['C1'] = term.cyan
138+
colors['CName'] = term.blue
155139
colors['CTemp'] = _conditional(lambda: int(self.entry['temperature.gpu']) < 50,
156-
ANSIColors.RED, ANSIColors.BOLD_RED)
157-
colors['CMemU'] = ANSIColors.BOLD_YELLOW
158-
colors['CMemT'] = ANSIColors.YELLOW
159-
colors['CMemP'] = ANSIColors.YELLOW
160-
colors['CUser'] = ANSIColors.GRAY
140+
term.red, term.bold_red)
141+
colors['CMemU'] = term.bold_yellow
142+
colors['CMemT'] = term.yellow
143+
colors['CMemP'] = term.yellow
144+
colors['CUser'] = term.gray
161145
colors['CUtil'] = _conditional(lambda: int(self.entry['utilization.gpu']) < 30,
162-
ANSIColors.GREEN, ANSIColors.BOLD_GREEN)
146+
term.green, term.bold_green)
163147

164148
if not with_colors:
165149
for k in list(colors.keys()):
@@ -312,11 +296,11 @@ def print_formatted(self, fp=sys.stdout, no_color=False,
312296
):
313297
# header
314298
time_format = locale.nl_langinfo(locale.D_T_FMT)
315-
header_msg = '%(WHITE)s{hostname}%(RESET)s {timestr}'.format(**{
299+
header_msg = '{t.white}{hostname}{t.normal} {timestr}'.format(**{
316300
'hostname' : self.hostname,
317-
'timestr' : self.query_time.strftime(time_format)
318-
319-
}) % (defaultdict(str) if no_color else ANSIColors.__dict__)
301+
'timestr' : self.query_time.strftime(time_format),
302+
't': term,
303+
})
320304

321305
print(header_msg)
322306

@@ -354,27 +338,6 @@ def date_handler(obj):
354338
fp.flush()
355339

356340

357-
def self_test():
358-
gpu_stats = GPUStatCollection.new_query()
359-
print('# of GPUS:', len(gpu_stats))
360-
for g in gpu_stats:
361-
print(g)
362-
363-
process_entries = GPUStatCollection.running_processes()
364-
print('---Entries---')
365-
print(process_entries)
366-
367-
print('-------------')
368-
369-
370-
def new_query():
371-
'''
372-
Obtain a new GPUStatCollection instance by querying nvidia-smi
373-
to get the list of GPUs and running process information.
374-
'''
375-
return GPUStatCollection.new_query()
376-
377-
378341
def print_gpustat(json=False, debug=False, **args):
379342
'''
380343
Display the GPU query results into standard output.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nvidia-ml-py
22
psutil
3+
blessings

0 commit comments

Comments
 (0)