From 9e8d71293793f6ab878c374a8695c64cfb8e35e0 Mon Sep 17 00:00:00 2001 From: gouzil <66515297+gouzil@users.noreply.github.com> Date: Sun, 30 Jun 2024 19:16:03 +0800 Subject: [PATCH] [CodeStyle][UP031] fix some `/python/paddle/` - part 18 (#65580) --- python/paddle/base/core.py | 10 +++++----- python/paddle/distributed/parallel.py | 4 ++-- python/paddle/framework/framework.py | 4 ++-- python/paddle/pir/core.py | 2 +- python/paddle/profiler/profiler.py | 4 ++-- python/paddle/regularizer.py | 4 ++-- python/paddle/text/datasets/conll05.py | 4 +--- python/paddle/utils/deprecated.py | 4 ++-- 8 files changed, 17 insertions(+), 19 deletions(-) diff --git a/python/paddle/base/core.py b/python/paddle/base/core.py index 15baafd70e8884..9734805d5476d4 100644 --- a/python/paddle/base/core.py +++ b/python/paddle/base/core.py @@ -50,7 +50,7 @@ f"""NOTE: You may need to run \"set PATH={executable_path};%PATH%\" if you encounters \"DLL load failed\" errors. If you have python installed in other directory, replace \"{executable_path}\" with your own - directory. The original error is: \n {str(e)}""" + directory. The original error is: \n {e}""" ) else: raise ImportError( @@ -78,7 +78,7 @@ def avx_supported(): except Exception as e: sys.stderr.write( 'Can not get the AVX flag from /proc/cpuinfo.\n' - 'The original error is: %s\n' % str(e) + f'The original error is: {e}\n' ) return has_avx elif sysstr == 'darwin': @@ -89,7 +89,7 @@ def avx_supported(): except Exception as e: sys.stderr.write( 'Can not get the AVX flag from machdep.cpu.features.\n' - 'The original error is: %s\n' % str(e) + f'The original error is: {e}\n' ) if not has_avx: import subprocess @@ -173,11 +173,11 @@ def asm_func(code_str, restype=ctypes.c_uint32, argtypes=()): except Exception as e: sys.stderr.write( 'Failed getting the AVX flag on Windows.\n' - 'The original error is: %s\n' % str(e) + f'The original error is: {e}\n' ) return (retval & (1 << avx_bit)) > 0 else: - sys.stderr.write('Do not get AVX flag on %s\n' % sysstr) + sys.stderr.write(f'Do not get AVX flag on {sysstr}\n') return False diff --git a/python/paddle/distributed/parallel.py b/python/paddle/distributed/parallel.py index c3d8c7f5dd1f38..0d905b4f5d9856 100644 --- a/python/paddle/distributed/parallel.py +++ b/python/paddle/distributed/parallel.py @@ -160,7 +160,7 @@ def sync_params_buffers( for _, param in model._obtain_parameters_buffers().items(): if not isinstance(param, core.eager.Tensor): raise TypeError( - "The data type of '%s' must be core.eager.Tensor" % param.name + f"The data type of '{param.name}' must be core.eager.Tensor" ) if is_model_parallel: @@ -916,7 +916,7 @@ def _check_var_exists(var_name): if var is None: raise ValueError( "paddle.distributed initialize error, " - "environment variable %s is needed, but not set." % var_name + f"environment variable {var_name} is needed, but not set." ) diff --git a/python/paddle/framework/framework.py b/python/paddle/framework/framework.py index a0b33f4fd44b3f..ae05d53e6266f0 100644 --- a/python/paddle/framework/framework.py +++ b/python/paddle/framework/framework.py @@ -49,7 +49,7 @@ def set_default_dtype(d): else: raise TypeError( "set_default_dtype only supports [float16, float32, float64] " - ", but received %s" % d.__name__ + f", but received {d.__name__}" ) else: if isinstance(d, paddle.dtype): @@ -67,7 +67,7 @@ def set_default_dtype(d): else: raise TypeError( "set_default_dtype only supports [float16, float32, float64, bfloat16] " - ", but received %s" % str(d) + f", but received {d}" ) LayerHelperBase.set_default_dtype(d) diff --git a/python/paddle/pir/core.py b/python/paddle/pir/core.py index dc6763375871c4..52d01edc74e6c3 100644 --- a/python/paddle/pir/core.py +++ b/python/paddle/pir/core.py @@ -114,7 +114,7 @@ def convert_np_dtype_to_dtype_(np_dtype): if dtype in np_type_to_paddle_type.keys(): return np_type_to_paddle_type[dtype] else: - raise ValueError("Not supported numpy dtype %s" % dtype) + raise ValueError(f"Not supported numpy dtype {dtype}") # program is a global instance. diff --git a/python/paddle/profiler/profiler.py b/python/paddle/profiler/profiler.py index 61c44b9ea19b5d..b0accc76b60b0f 100644 --- a/python/paddle/profiler/profiler.py +++ b/python/paddle/profiler/profiler.py @@ -255,7 +255,7 @@ def export_chrome_tracing( def handle_fn(prof): nonlocal worker_name if not worker_name: - worker_name = f"host_{socket.gethostname()}pid_{str(os.getpid())}" + worker_name = f"host_{socket.gethostname()}pid_{os.getpid()}" now = datetime.datetime.now() filename = '{}_time_{}.paddle_trace.json'.format( worker_name, now.strftime('%Y_%m_%d_%H_%M_%S_%f') @@ -309,7 +309,7 @@ def export_protobuf( def handle_fn(prof): nonlocal worker_name if not worker_name: - worker_name = f"host_{socket.gethostname()}pid_{str(os.getpid())}" + worker_name = f"host_{socket.gethostname()}pid_{os.getpid()}" now = datetime.datetime.now() filename = '{}_time_{}.paddle_trace.pb'.format( worker_name, now.strftime('%Y_%m_%d_%H_%M_%S_%f') diff --git a/python/paddle/regularizer.py b/python/paddle/regularizer.py index 148729484b243c..f09a148799a20e 100644 --- a/python/paddle/regularizer.py +++ b/python/paddle/regularizer.py @@ -163,7 +163,7 @@ def __call__( return decay def __str__(self) -> str: - return "L1Decay, coeff=%f" % self._coeff + return f"L1Decay, coeff={self._coeff:f}" class L2Decay(WeightDecayRegularizer): @@ -273,4 +273,4 @@ def __call__( return decay def __str__(self) -> str: - return "L2Decay, coeff=%f" % self._coeff + return f"L2Decay, coeff={self._coeff:f}" diff --git a/python/paddle/text/datasets/conll05.py b/python/paddle/text/datasets/conll05.py index e863620ed305e8..6fd79542577e33 100644 --- a/python/paddle/text/datasets/conll05.py +++ b/python/paddle/text/datasets/conll05.py @@ -256,9 +256,7 @@ def _load_anno(self): lbl_seq.append('B-' + cur_tag) is_in_bracket = True else: - raise RuntimeError( - 'Unexpected label: %s' % l - ) + raise RuntimeError(f'Unexpected label: {l}') self.sentences.append(sentences) self.predicates.append(verb_list[i]) diff --git a/python/paddle/utils/deprecated.py b/python/paddle/utils/deprecated.py index d5825a66bf13aa..8c66c6428bea28 100755 --- a/python/paddle/utils/deprecated.py +++ b/python/paddle/utils/deprecated.py @@ -122,10 +122,10 @@ def wrapper(*args: _InputT.args, **kwargs: _InputT.kwargs) -> _RetT: f'API "{func.__module__}.{func.__name__}" has been deprecated.' ) - warningmsg = "\033[93m\nWarning:\n%s \033[0m" % (msg) + warningmsg = f"\033[93m\nWarning:\n{msg} \033[0m" # ensure ANSI escape sequences print correctly in cmd and powershell if sys.platform.lower() == 'win32': - warningmsg = "\nWarning:\n%s " % (msg) + warningmsg = f"\nWarning:\n{msg} " v_current = parse_version(paddle.__version__) v_current += [0] * (4 - len(v_current))