From 5cff79f14fe6b13c3cdd163d94e2a258ff560469 Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Sun, 17 Jul 2022 22:01:46 +0800 Subject: [PATCH] style(core): resolve pylint warnings Signed-off-by: Xuehai Pan --- nvitop/cli.py | 4 ++-- nvitop/core/device.py | 19 ++++++++++++------- nvitop/core/libnvml.py | 28 ++++++++++++++-------------- nvitop/core/process.py | 21 ++++----------------- nvitop/gui/library/utils.py | 2 +- 5 files changed, 33 insertions(+), 41 deletions(-) diff --git a/nvitop/cli.py b/nvitop/cli.py index 5bb660a..ecf5a47 100644 --- a/nvitop/cli.py +++ b/nvitop/cli.py @@ -140,9 +140,9 @@ def main(): # pylint: disable=too-many-branches,too-many-statements,too-many-lo try: device_count = Device.count() - except libnvml.NVMLError_LibraryNotFound: # pylint: disable=no-member + except libnvml.NVMLError_LibraryNotFound: return 1 - except libnvml.NVMLError as e: # pylint: disable=invalid-name + except libnvml.NVMLError as e: # pylint: disable=invalid-name print('{} {}'.format(colored('NVML ERROR:', color='red', attrs=('bold',)), e), file=sys.stderr) return 1 diff --git a/nvitop/core/device.py b/nvitop/core/device.py index d534d64..4961631 100644 --- a/nvitop/core/device.py +++ b/nvitop/core/device.py @@ -406,7 +406,7 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me if index_or_uuid.isdigit(): index_or_uuid = int(index_or_uuid) elif Device.UUID_PATTERN.match(index_or_uuid) is None: - raise libnvml.NVMLError_NotFound # pylint: disable=no-member + raise libnvml.NVMLError_NotFound if use_integer_identifiers is None: use_integer_identifiers = isinstance(index_or_uuid, int) @@ -534,7 +534,7 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me self._nvml_index = index try: self._handle = libnvml.nvmlQuery('nvmlDeviceGetHandleByIndex', index, ignore_errors=False) - except libnvml.NVMLError_GpuIsLost: # pylint: disable=no-member + except libnvml.NVMLError_GpuIsLost: self._handle = None else: try: @@ -542,7 +542,7 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me self._handle = libnvml.nvmlQuery('nvmlDeviceGetHandleByUUID', uuid, ignore_errors=False) else: self._handle = libnvml.nvmlQuery('nvmlDeviceGetHandleByPciBusId', bus_id, ignore_errors=False) - except libnvml.NVMLError_GpuIsLost: # pylint: disable=no-member + except libnvml.NVMLError_GpuIsLost: self._handle = None self._nvml_index = NA else: @@ -626,7 +626,7 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me def attribute(*args, **kwargs): try: return libnvml.nvmlQuery(func, self._handle, *args, **kwargs, ignore_errors=False) - except libnvml.NVMLError_NotSupported: # pylint: disable=no-member + except libnvml.NVMLError_NotSupported: return NA attribute.__name__ = name @@ -1759,7 +1759,12 @@ class MigDevice(Device): # pylint: disable=too-many-instance-attributes def __init__(self, index: Optional[Union[Tuple[int, int], str]] = None, *, # pylint: disable=super-init-not-called uuid: Optional[str] = None) -> None: - """Initializes the instance created by ``__new__()``.""" + """Initializes the instance created by ``__new__()``. + + Raises: + libnvml.NVMLError_NotFound: + If the device is not found for the given NVML identifier. + """ if isinstance(index, str) and self.UUID_PATTERN.match(index) is not None: # passed by UUID index, uuid = None, index @@ -1789,7 +1794,7 @@ class MigDevice(Device): # pylint: disable=too-many-instance-attributes try: self._handle = libnvml.nvmlQuery('nvmlDeviceGetMigDeviceHandleByIndex', self.parent.handle, self.mig_index, ignore_errors=False) - except libnvml.NVMLError_GpuIsLost: # pylint: disable=no-member + except libnvml.NVMLError_GpuIsLost: pass else: self._handle = libnvml.nvmlQuery('nvmlDeviceGetHandleByUUID', uuid, ignore_errors=False) @@ -1802,7 +1807,7 @@ class MigDevice(Device): # pylint: disable=too-many-instance-attributes self._nvml_index = mig_device.index break else: - raise libnvml.NVMLError_NotFound() # pylint: disable=no-member + raise libnvml.NVMLError_NotFound self._max_clock_infos = ClockInfos(graphics=NA, sm=NA, memory=NA, video=NA) self._timestamp = 0 diff --git a/nvitop/core/libnvml.py b/nvitop/core/libnvml.py index 2ee591a..594bace 100644 --- a/nvitop/core/libnvml.py +++ b/nvitop/core/libnvml.py @@ -18,6 +18,7 @@ from typing import (Tuple as _Tuple, Callable as _Callable, Type as _Type, # Python Bindings for the NVIDIA Management Library (NVML) # https://pypi.org/project/nvidia-ml-py import pynvml as _pynvml +from pynvml import * # pylint: disable=wildcard-import,unused-wildcard-import from nvitop.core.utils import NA, colored as __colored @@ -49,7 +50,7 @@ for _name, _attr in _vars_pynvml.items(): if _name in ('nvmlInit', 'nvmlInitWithFlags', 'nvmlShutdown'): continue if _name.startswith('NVML_ERROR_') or _name.startswith('NVMLError_'): - _vars[_name] = _attr + __all__.append(_name) if _name.startswith('NVML_ERROR_'): _errcode_to_name[_attr] = _name _const_names.append(_name) @@ -63,15 +64,11 @@ for _name, _attr in _vars_pynvml.items(): ) or ( _name.startswith('nvml') and isinstance(_attr, _FunctionType) ): - _vars[_name] = _attr + __all__.append(_name) if _name.startswith('NVML_'): _const_names.append(_name) -# 3. Register the inherited members into `__all__` and globals. -__all__.extend(_vars.keys()) -globals().update(_vars) - -# 4. Add docstring to exception classes +# 3. Add docstring to exception classes _errcode = _reason = _subclass = None for _errcode, _reason in _errcode_to_string.items(): _subclass = _pynvml.nvmlExceptionClass(_errcode) @@ -79,7 +76,7 @@ for _errcode, _reason in _errcode_to_string.items(): _errcode_to_name[_errcode], _errcode) -# 5. Add undocumented constants into module docstring +# 4. Add undocumented constants into module docstring _data_docs = [] _sphinx_doc = None for _name in _const_names: @@ -119,14 +116,17 @@ Functions and Exceptions """.format('\n\n'.join(_data_docs)) -del (_name, _attr, _vars_pynvml, _vars, +del (_name, _attr, _vars_pynvml, _errcode, _reason, _subclass, _errcode_to_name, _errcode_to_string, _const_names, _data_docs, _sphinx_doc) -# 6. Add explicit references to appease linters +# 5. Add explicit references to appease linters c_nvmlDevice_t = _pynvml.c_nvmlDevice_t -NVMLError_LibraryNotFound = _pynvml.NVMLError_LibraryNotFound # pylint: disable=no-member +NVMLError_LibraryNotFound = _pynvml.NVMLError_LibraryNotFound # pylint: disable=no-member NVMLError_FunctionNotFound = _pynvml.NVMLError_FunctionNotFound # pylint: disable=no-member +NVMLError_NotSupported = _pynvml.NVMLError_NotSupported # pylint: disable=no-member +NVMLError_NotFound = _pynvml.NVMLError_NotFound # pylint: disable=no-member +NVMLError_GpuIsLost = _pynvml.NVMLError_GpuIsLost # pylint: disable=no-member # New members in `libnvml` ############################################################################################# @@ -149,7 +149,7 @@ def _lazy_init() -> None: nvmlInit() -def nvmlInit() -> None: +def nvmlInit() -> None: # pylint: disable=function-redefined """Initializes the NVML context with default flag (0). Raises: @@ -168,7 +168,7 @@ def nvmlInit() -> None: nvmlInitWithFlags(0) -def nvmlInitWithFlags(flags: int) -> None: +def nvmlInitWithFlags(flags: int) -> None: # pylint: disable=function-redefined """Initializes the NVML context with the given flags. Raises: @@ -232,7 +232,7 @@ def nvmlInitWithFlags(flags: int) -> None: __initialized = True -def nvmlShutdown() -> None: +def nvmlShutdown() -> None: # pylint: disable=function-redefined """Shutdowns the NVML context. Raises: diff --git a/nvitop/core/process.py b/nvitop/core/process.py index 8e35cba..248daeb 100644 --- a/nvitop/core/process.py +++ b/nvitop/core/process.py @@ -632,10 +632,10 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi """The type of the GPU context. The type is one of the following: - - 'C': compute context - - 'G': graphics context - - 'C+G': both compute context and graphics context - - 'N/A': not applicable + - :data:`'C'`: compute context + - :data:`'G'`: graphics context + - :data:`'C+G'`: both compute context and graphics context + - :data:`'N/A'`: not applicable """ return self._type @@ -668,7 +668,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -686,7 +685,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -704,7 +702,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -721,7 +718,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -738,7 +734,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -763,7 +758,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -783,7 +777,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -801,7 +794,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -820,7 +812,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -840,7 +831,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -857,7 +847,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -877,7 +866,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ @@ -897,7 +885,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi If the user do not have read privilege to the process' status file. Note: - To return the fallback value rather than raise an exception, please use the context manager :meth:`GpuProcess.failsafe`. See also :meth:`take_snapshots` and :meth:`failsafe`. """ diff --git a/nvitop/gui/library/utils.py b/nvitop/gui/library/utils.py index 273bb62..0743b06 100644 --- a/nvitop/gui/library/utils.py +++ b/nvitop/gui/library/utils.py @@ -55,7 +55,7 @@ try: except ModuleNotFoundError: USERNAME = os.getlogin() -if host.WINDOWS: # pylint: disable=no-member +if host.WINDOWS: import ctypes SUPERUSER = bool(ctypes.windll.shell32.IsUserAnAdmin()) else: