From c8482cc0e56d6458f7ab535243d733d11361d38a Mon Sep 17 00:00:00 2001 From: NovusEdge <68768969+NovusEdge@users.noreply.github.com> Date: Thu, 13 May 2021 03:13:50 +0530 Subject: [PATCH] [python] added f-string to python-package/lightgbm/callback.py (#4142) * added f-string * fixed indent issues * fixed some linting issues * trying to fix E225 for 233:65 * Update python-package/lightgbm/callback.py * first_metric_only log message * Apply suggestions from code review Co-authored-by: Nikita Titov Co-authored-by: James Lamb Co-authored-by: Nikita Titov --- python-package/lightgbm/callback.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/python-package/lightgbm/callback.py b/python-package/lightgbm/callback.py index 2bf1e6360856..fc151d1e82c0 100644 --- a/python-package/lightgbm/callback.py +++ b/python-package/lightgbm/callback.py @@ -39,12 +39,12 @@ def __init__(self, best_iteration: int, best_score: float) -> None: def _format_eval_result(value: list, show_stdv: bool = True) -> str: """Format metric string.""" if len(value) == 4: - return '%s\'s %s: %g' % (value[0], value[1], value[2]) + return f"{value[0]}'s {value[1]}: {value[2]:g}" elif len(value) == 5: if show_stdv: - return '%s\'s %s: %g + %g' % (value[0], value[1], value[2], value[4]) + return f"{value[0]}'s {value[1]}: {value[2]:g} + {value[4]:g}" else: - return '%s\'s %s: %g' % (value[0], value[1], value[2]) + return f"{value[0]}'s {value[1]}: {value[2]:g}" else: raise ValueError("Wrong metric value") @@ -67,7 +67,7 @@ def print_evaluation(period: int = 1, show_stdv: bool = True) -> Callable: def _callback(env: CallbackEnv) -> None: if period > 0 and env.evaluation_result_list and (env.iteration + 1) % period == 0: result = '\t'.join([_format_eval_result(x, show_stdv) for x in env.evaluation_result_list]) - _log_info('[%d]\t%s' % (env.iteration + 1, result)) + _log_info(f'[{env.iteration + 1}]\t{result}') _callback.order = 10 # type: ignore return _callback @@ -129,8 +129,7 @@ def _callback(env: CallbackEnv) -> None: for key, value in kwargs.items(): if isinstance(value, list): if len(value) != env.end_iteration - env.begin_iteration: - raise ValueError("Length of list {} has to equal to 'num_boost_round'." - .format(repr(key))) + raise ValueError(f"Length of list {repr(key)} has to equal to 'num_boost_round'.") new_param = value[env.iteration - env.begin_iteration] else: new_param = value(env.iteration - env.begin_iteration) @@ -187,7 +186,7 @@ def _init(env: CallbackEnv) -> None: 'at least one dataset and eval metric is required for evaluation') if verbose: - _log_info("Training until validation scores don't improve for {} rounds".format(stopping_rounds)) + _log_info(f"Training until validation scores don't improve for {stopping_rounds} rounds") # split is needed for " " case (e.g. "train l1") first_metric[0] = env.evaluation_result_list[0][1].split(" ")[-1] @@ -204,10 +203,11 @@ def _init(env: CallbackEnv) -> None: def _final_iteration_check(env: CallbackEnv, eval_name_splitted: List[str], i: int) -> None: if env.iteration == env.end_iteration - 1: if verbose: - _log_info('Did not meet early stopping. Best iteration is:\n[%d]\t%s' % ( - best_iter[i] + 1, '\t'.join([_format_eval_result(x) for x in best_score_list[i]]))) + best_score_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) + _log_info('Did not meet early stopping. ' + f'Best iteration is:\n[{best_iter[i] + 1}]\t{best_score_str}') if first_metric_only: - _log_info("Evaluated only: {}".format(eval_name_splitted[-1])) + _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) def _callback(env: CallbackEnv) -> None: @@ -231,10 +231,10 @@ def _callback(env: CallbackEnv) -> None: continue # train data for lgb.cv or sklearn wrapper (underlying lgb.train) elif env.iteration - best_iter[i] >= stopping_rounds: if verbose: - _log_info('Early stopping, best iteration is:\n[%d]\t%s' % ( - best_iter[i] + 1, '\t'.join([_format_eval_result(x) for x in best_score_list[i]]))) + eval_result_str = '\t'.join([_format_eval_result(x) for x in best_score_list[i]]) + _log_info(f"Early stopping, best iteration is:\n[{best_iter[i] + 1}]\t{eval_result_str}") if first_metric_only: - _log_info("Evaluated only: {}".format(eval_name_splitted[-1])) + _log_info(f"Evaluated only: {eval_name_splitted[-1]}") raise EarlyStopException(best_iter[i], best_score_list[i]) _final_iteration_check(env, eval_name_splitted, i) _callback.order = 30 # type: ignore