From 27b573804d4e7703f361edb3b33001682778f378 Mon Sep 17 00:00:00 2001 From: Xuehai Pan Date: Mon, 20 Mar 2023 13:56:05 +0000 Subject: [PATCH] chore(api): remove redundant `__str__` method when `__repr__` is defined --- nvitop/api/device.py | 8 ++------ nvitop/api/libcuda.py | 4 +--- nvitop/api/libcudart.py | 4 +--- nvitop/api/process.py | 10 +++------- nvitop/api/utils.py | 4 +--- 5 files changed, 8 insertions(+), 22 deletions(-) diff --git a/nvitop/api/device.py b/nvitop/api/device.py index 0728931..20b55e6 100644 --- a/nvitop/api/device.py +++ b/nvitop/api/device.py @@ -661,7 +661,7 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me self._ident = (self.index, self.uuid()) self._hash = None - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the device.""" return '{}(index={}, name="{}", total_memory={})'.format( self.__class__.__name__, @@ -670,8 +670,6 @@ class Device: # pylint: disable=too-many-instance-attributes,too-many-public-me self.memory_total_human(), ) - __repr__ = __str__ - def __eq__(self, other: object) -> bool: """Test equality to other object.""" if not isinstance(other, Device): @@ -2281,7 +2279,7 @@ class CudaDevice(Device): self._ident = ((self._cuda_index, self.index), self.uuid()) - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the CUDA device.""" return '{}(cuda_index={}, nvml_index={}, name="{}", total_memory={})'.format( self.__class__.__name__, @@ -2291,8 +2289,6 @@ class CudaDevice(Device): self.memory_total_human(), ) - __repr__ = __str__ - def __reduce__(self) -> tuple[type[CudaDevice], tuple[int]]: """Return state information for pickling.""" return self.__class__, (self._cuda_index,) diff --git a/nvitop/api/libcuda.py b/nvitop/api/libcuda.py index 959c930..ace8f88 100644 --- a/nvitop/api/libcuda.py +++ b/nvitop/api/libcuda.py @@ -238,7 +238,7 @@ class CUDAError(Exception): obj.value = value return obj - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the error.""" # pylint: disable=no-member try: @@ -256,8 +256,6 @@ class CUDAError(Exception): except CUDAError: return f'CUDA Error with code {self.value}.' - __repr__ = __str__ - def __eq__(self, other: object) -> bool: """Test equality to other object.""" if not isinstance(other, CUDAError): diff --git a/nvitop/api/libcudart.py b/nvitop/api/libcudart.py index 969e935..6bb154f 100644 --- a/nvitop/api/libcudart.py +++ b/nvitop/api/libcudart.py @@ -289,7 +289,7 @@ class cudaError(Exception): obj.value = value return obj - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the error.""" # pylint: disable=no-member try: @@ -307,8 +307,6 @@ class cudaError(Exception): except cudaError: return f'CUDA Error with code {self.value}.' - __repr__ = __str__ - def __eq__(self, other: object) -> bool: """Test equality to other object.""" if not isinstance(other, cudaError): diff --git a/nvitop/api/process.py b/nvitop/api/process.py index b0e02bf..5837834 100644 --- a/nvitop/api/process.py +++ b/nvitop/api/process.py @@ -229,11 +229,9 @@ class HostProcess(host.Process, metaclass=ABCMeta): pass self._super_gone = value - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the process.""" - return super().__str__().replace(self.__class__.__module__ + '.', '', 1) - - __repr__ = __str__ + return super().__repr__().replace(self.__class__.__module__ + '.', '', 1) def __reduce__(self) -> tuple[type[HostProcess], tuple[int]]: """Return state information for pickling.""" @@ -520,7 +518,7 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi if not hasattr(self, f'_gpu_{util}_utilization'): setattr(self, f'_gpu_{util}_utilization', NA) - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the GPU process.""" return '{}(pid={}, gpu_memory={}, type={}, device={}, host={})'.format( self.__class__.__name__, @@ -531,8 +529,6 @@ class GpuProcess: # pylint: disable=too-many-instance-attributes,too-many-publi self.host, ) - __repr__ = __str__ - def __eq__(self, other: object) -> bool: """Test equality to other object.""" if not isinstance(other, (GpuProcess, host.Process)): diff --git a/nvitop/api/utils.py b/nvitop/api/utils.py index 403797d..f7b6d74 100644 --- a/nvitop/api/utils.py +++ b/nvitop/api/utils.py @@ -624,7 +624,7 @@ class Snapshot: for key, value in items.items(): setattr(self, key, value) - def __str__(self) -> str: + def __repr__(self) -> str: """Return a string representation of the snapshot.""" keys = set(self.__dict__.keys()).difference({'real', 'timestamp'}) keys = ['real', *sorted(keys)] @@ -641,8 +641,6 @@ class Snapshot: ',\n '.join(keyvals), ) - __repr__ = __str__ - def __hash__(self) -> int: """Return a hash value of the snapshot.""" return hash((self.real, self.timestamp))