diff --git a/.pylintrc b/.pylintrc index 83a30f5..ff46ed3 100644 --- a/.pylintrc +++ b/.pylintrc @@ -46,8 +46,8 @@ ignore=CVS,.vscode,.history # Add files or directories matching the regular expressions patterns to the # ignore-list. The regex matches against paths and can be in Posix or Windows -# format. Because '\' represents the directory delimiter on Windows systems, it -# can't be used as an escape character. +# format. Because '\\' represents the directory delimiter on Windows systems, +# it can't be used as an escape character. ignore-paths= # Files or directories matching the regular expression patterns are skipped. @@ -270,7 +270,7 @@ exclude-protected=_asdict, valid-classmethod-first-arg=cls # List of valid names for the first argument in a metaclass class method. -valid-metaclass-classmethod-first-arg=cls +valid-metaclass-classmethod-first-arg=mcs [DESIGN] @@ -317,8 +317,8 @@ min-public-methods=2 [EXCEPTIONS] # Exceptions that will emit a warning when caught. -overgeneral-exceptions=BaseException, - Exception +overgeneral-exceptions=builtins.BaseException, + builtins.Exception [FORMAT] diff --git a/nvitop/api/device.py b/nvitop/api/device.py index 620d2ba..f3512be 100644 --- a/nvitop/api/device.py +++ b/nvitop/api/device.py @@ -540,8 +540,8 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me """ if (index, uuid, bus_id).count(None) != 2: raise TypeError( - 'Device(index=None, uuid=None, bus_id=None) takes 1 non-None arguments ' - 'but (index, uuid, bus_id) = {!r} were given'.format((index, uuid, bus_id)) + f'Device(index=None, uuid=None, bus_id=None) takes 1 non-None arguments ' + f'but (index, uuid, bus_id) = {(index, uuid, bus_id)!r} were given' ) if cls is not Device: @@ -784,15 +784,11 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me if self._cuda_index is None: visible_device_indices = self.parse_cuda_visible_devices() try: - cuda_index = visible_device_indices.index(self.index) + self._cuda_index = visible_device_indices.index(self.index) except ValueError as ex: raise RuntimeError( - 'CUDA Error: Device(index={}) is not visible to CUDA applications'.format( - self.index - ) + f'CUDA Error: Device(index={self.index}) is not visible to CUDA applications' ) from ex - else: - self._cuda_index = cuda_index return self._cuda_index diff --git a/nvitop/api/libnvml.py b/nvitop/api/libnvml.py index f1e8a4a..1cc0a55 100644 --- a/nvitop/api/libnvml.py +++ b/nvitop/api/libnvml.py @@ -302,10 +302,10 @@ def nvmlInitWithFlags(flags: int) -> None: # pylint: disable=function-redefined LOGGER.critical(message) raise - else: - with __lock: - __flags.append(flags) - __initialized = True + + with __lock: + __flags.append(flags) + __initialized = True def nvmlShutdown() -> None: # pylint: disable=function-redefined @@ -416,10 +416,10 @@ def nvmlQuery( if ignore_errors: return default raise - else: - if isinstance(retval, bytes): - retval = retval.decode('UTF-8') - return retval + + if isinstance(retval, bytes): + retval = retval.decode('UTF-8') + return retval def nvmlCheckReturn( diff --git a/nvitop/callbacks/keras.py b/nvitop/callbacks/keras.py index d3ea674..e51903d 100644 --- a/nvitop/callbacks/keras.py +++ b/nvitop/callbacks/keras.py @@ -124,8 +124,8 @@ class GpuStatsLogger(Callback): # pylint: disable=too-many-instance-attributes self._devices = get_devices_by_logical_ids(gpu_ids, unique=True) except (libnvml.NVMLError, RuntimeError) as ex: raise ValueError( - 'Cannot use GpuStatsLogger callback because devices unavailable. ' - 'Received: `gpus={}`'.format(gpu_ids) + f'Cannot use GpuStatsLogger callback because devices unavailable. ' + f'Received: `gpus={gpu_ids}`' ) from ex self._memory_utilization = memory_utilization diff --git a/nvitop/callbacks/pytorch_lightning.py b/nvitop/callbacks/pytorch_lightning.py index 86fc633..30f9f39 100644 --- a/nvitop/callbacks/pytorch_lightning.py +++ b/nvitop/callbacks/pytorch_lightning.py @@ -115,8 +115,8 @@ class GpuStatsLogger(Callback): # pylint: disable=too-many-instance-attributes if trainer.strategy.root_device.type != 'cuda': raise MisconfigurationException( - 'You are using GpuStatsLogger but are not running on GPU. ' - 'The root device type is {}.'.format(trainer.strategy.root_device.type) + f'You are using GpuStatsLogger but are not running on GPU. ' + f'The root device type is {trainer.strategy.root_device.type}.' ) device_ids = trainer.data_parallel_device_ids @@ -124,8 +124,8 @@ class GpuStatsLogger(Callback): # pylint: disable=too-many-instance-attributes self._devices = get_devices_by_logical_ids(device_ids, unique=True) except (libnvml.NVMLError, RuntimeError) as ex: raise ValueError( - 'Cannot use GpuStatsLogger callback because devices unavailable. ' - 'Received: `gpus={}`'.format(device_ids) + f'Cannot use GpuStatsLogger callback because devices unavailable. ' + f'Received: `gpus={device_ids}`' ) from ex def on_train_epoch_start(self, trainer, pl_module) -> None: diff --git a/nvitop/gui/library/keybinding.py b/nvitop/gui/library/keybinding.py index 64096c0..3aeba74 100644 --- a/nvitop/gui/library/keybinding.py +++ b/nvitop/gui/library/keybinding.py @@ -172,8 +172,8 @@ def key_to_string(key): if key in range(33, 127): return chr(key) if key in REVERSED_SPECIAL_KEYS: - return '<%s>' % REVERSED_SPECIAL_KEYS[key] - return '<%s>' % str(key) + return f'<{REVERSED_SPECIAL_KEYS[key]}>' + return f'<{key}>' def construct_keybinding(keys): @@ -201,7 +201,7 @@ def construct_keybinding(keys): continue if alt_key_on: try: - strings.append('<%s>' % REVERSED_SPECIAL_KEYS[(ALT_KEY, key)]) + strings.append(f'<{REVERSED_SPECIAL_KEYS[(ALT_KEY, key)]}>') except KeyError: strings.extend(map(key_to_string, (ALT_KEY, key))) else: @@ -276,7 +276,7 @@ class KeyMaps(dict): pointer = pointer[key] except KeyError as ex: raise KeyError( - "Tried to copy the keybinding `%s', but it was not found." % source + f'Tried to copy the keybinding `{source}`, but it was not found.' ) from ex try: self.bind(context, target, copy.deepcopy(pointer)) diff --git a/nvitop/gui/library/libcurses.py b/nvitop/gui/library/libcurses.py index c23223e..22365da 100644 --- a/nvitop/gui/library/libcurses.py +++ b/nvitop/gui/library/libcurses.py @@ -275,8 +275,7 @@ class CursesShortcuts: self.win.attrset(attr) except curses.error: return 0 - else: - return attr + return attr def update_size(self, termsize=None): if termsize is None: diff --git a/nvitop/gui/library/messagebox.py b/nvitop/gui/library/messagebox.py index 37d478c..c7e7869 100644 --- a/nvitop/gui/library/messagebox.py +++ b/nvitop/gui/library/messagebox.py @@ -277,6 +277,7 @@ def send_signal(signal, panel): processes = [process.ljust(maxlen) for process in processes] message = 'Send signal to the following processes?\n\n{}'.format(' '.join(processes)) + # pylint: disable=use-dict-literal panel.root.messagebox = MessageBox( message=message, options=[ @@ -325,3 +326,4 @@ def send_signal(signal, panel): win=panel.win, root=panel.root, ) + # pylint: enable=use-dict-literal diff --git a/nvitop/gui/screens/environ.py b/nvitop/gui/screens/environ.py index 533f82b..61f6411 100644 --- a/nvitop/gui/screens/environ.py +++ b/nvitop/gui/screens/environ.py @@ -145,15 +145,13 @@ class EnvironScreen(Displayable): # pylint: disable=too-many-instance-attribute self.color_reset() if isinstance(self.process, GpuProcess): - process_type = 'GPU: {}'.format( - self.process.type.replace('C', 'Compute').replace('G', 'Graphics') + process_type = 'GPU: ' + self.process.type.replace('C', 'Compute').replace( + 'G', 'Graphics' ) else: process_type = 'Host' header_prefix = WideString( - 'Environment of process {} ({}@{}): '.format( - self.process.pid, self.username, process_type - ) + f'Environment of process {self.process.pid} ({self.username}@{process_type}): ' ) offset = max(0, min(self.x_offset, len(self.command) + len(header_prefix) - self.width)) header = str((header_prefix + self.command[offset:]).ljust(self.width)[: self.width]) diff --git a/nvitop/gui/screens/main/__init__.py b/nvitop/gui/screens/main/__init__.py index 6234be6..250cbdc 100644 --- a/nvitop/gui/screens/main/__init__.py +++ b/nvitop/gui/screens/main/__init__.py @@ -212,7 +212,7 @@ class MainScreen(DisplayableContainer): # pylint: disable=too-many-instance-att sort_by(order=ProcessPanel.ORDERS[self.process_panel.order].next, reverse=False) def order_reverse(): - sort_by(order=self.process_panel.order, reverse=(not self.process_panel.reverse)) + sort_by(order=self.process_panel.order, reverse=not self.process_panel.reverse) keymaps = self.root.keymaps diff --git a/nvitop/gui/screens/main/device.py b/nvitop/gui/screens/main/device.py index 87c53a9..8b86a88 100644 --- a/nvitop/gui/screens/main/device.py +++ b/nvitop/gui/screens/main/device.py @@ -138,9 +138,7 @@ class DevicePanel(Displayable): # pylint: disable=too-many-instance-attributes if device.bar1_memory_percent >= 100: device.bar1_memory_percent_string = 'MAX' else: - device.bar1_memory_percent_string = '{}%'.format( - round(device.bar1_memory_percent) - ) + device.bar1_memory_percent_string = f'{round(device.bar1_memory_percent)}%' else: device.name = cut_string(device.name, maxlen=18, padstr='..', align='right') device.current_driver_model = device.current_driver_model.replace('WDM', 'TCC') diff --git a/nvitop/gui/screens/main/host.py b/nvitop/gui/screens/main/host.py index 272b0e4..36caf33 100644 --- a/nvitop/gui/screens/main/host.py +++ b/nvitop/gui/screens/main/host.py @@ -122,7 +122,7 @@ class HostPanel(Displayable): # pylint: disable=too-many-instance-attributes baseline=0.0, upperbound=100.0, dynamic_bound=False, - format=lambda x: ('GPU {} MEM: {}').format(device.display_index, percentage(x)), + format=lambda x: f'GPU {device.display_index} MEM: {percentage(x)}', )(device.memory_percent) device.gpu_utilization = BufferedHistoryGraph( interval=1.0, @@ -132,7 +132,7 @@ class HostPanel(Displayable): # pylint: disable=too-many-instance-attributes baseline=0.0, upperbound=100.0, dynamic_bound=False, - format=lambda x: ('GPU {} UTL: {}').format(device.display_index, percentage(x)), + format=lambda x: f'GPU {device.display_index} UTL: {percentage(x)}', )(device.gpu_utilization) for device in self.devices: @@ -147,7 +147,7 @@ class HostPanel(Displayable): # pylint: disable=too-many-instance-attributes baseline=0.0, upperbound=100.0, dynamic_bound=False, - format=lambda x: ('{}GPU MEM: {}').format(prefix, percentage(x)), + format=lambda x: f'{prefix}GPU MEM: {percentage(x)}', ) self.average_gpu_utilization = BufferedHistoryGraph( interval=1.0, @@ -157,7 +157,7 @@ class HostPanel(Displayable): # pylint: disable=too-many-instance-attributes baseline=0.0, upperbound=100.0, dynamic_bound=False, - format=lambda x: ('{}GPU UTL: {}').format(prefix, percentage(x)), + format=lambda x: f'{prefix}GPU UTL: {percentage(x)}', ) def take_snapshots(self): diff --git a/nvitop/gui/ui.py b/nvitop/gui/ui.py index 257e1fe..8c2b6b4 100644 --- a/nvitop/gui/ui.py +++ b/nvitop/gui/ui.py @@ -141,8 +141,8 @@ class UI(DisplayableContainer): # pylint: disable=too-many-instance-attributes return n_term_lines, n_term_cols = self.termsize - message = 'nvitop needs at least a width of 79 to render, the current width is {}.'.format( - self.width + message = ( + f'nvitop needs at least a width of 79 to render, the current width is {self.width}.' ) words = iter(message.split()) width = min(max(n_term_cols, 40), n_term_cols, 60) - 10 @@ -199,8 +199,7 @@ class UI(DisplayableContainer): # pylint: disable=too-many-instance-attributes event = MouseEvent(curses.getmouse()) except curses.error: return - else: - super().click(event) + super().click(event) def handle_key(self, key): """Handles key input"""