From 591800355f1fa4b5b47043dabaa5309f6576bb02 Mon Sep 17 00:00:00 2001 From: Jon Holba Date: Wed, 11 Dec 2024 09:08:26 +0100 Subject: [PATCH] Run pyupgrade on src tree --- src/ert/run_models/base_run_model.py | 2 +- src/ert/run_models/everest_run_model.py | 4 ++-- src/ert/storage/local_experiment.py | 2 +- src/ert/storage/local_storage.py | 4 ++-- src/everest/bin/config_branch_script.py | 10 +++++----- src/everest/bin/everest_script.py | 12 +++++------- src/everest/bin/main.py | 2 +- src/everest/bin/utils.py | 2 +- src/everest/config/everest_config.py | 2 +- src/everest/config_file_loader.py | 2 +- src/everest/detached/jobs/everserver.py | 12 +++++------- src/everest/export.py | 2 +- src/everest/jobs/__init__.py | 2 +- src/everest/jobs/recovery_factor.py | 2 +- src/everest/jobs/templating/render.py | 4 ++-- src/everest/simulator/everest_to_ert.py | 14 ++++++-------- src/everest/util/__init__.py | 4 ++-- 17 files changed, 38 insertions(+), 44 deletions(-) diff --git a/src/ert/run_models/base_run_model.py b/src/ert/run_models/base_run_model.py index ccc58c60003..a426c5be7a1 100644 --- a/src/ert/run_models/base_run_model.py +++ b/src/ert/run_models/base_run_model.py @@ -121,7 +121,7 @@ def emit(self, record: logging.LogRecord) -> None: @contextmanager def captured_logs( messages: MutableSequence[str], level: int = logging.ERROR -) -> Generator[None, None, None]: +) -> Generator[None]: handler = _LogAggregration(messages) root_logger = logging.getLogger() handler.setLevel(level) diff --git a/src/ert/run_models/everest_run_model.py b/src/ert/run_models/everest_run_model.py index 60e24a2dc5a..8d7f9fdef12 100644 --- a/src/ert/run_models/everest_run_model.py +++ b/src/ert/run_models/everest_run_model.py @@ -305,7 +305,7 @@ def _handle_errors( fm_name: str, error_path: str, ) -> None: - fm_id = "b_{}_r_{}_s_{}_{}".format(batch, realization, simulation, fm_name) + fm_id = f"b_{batch}_r_{realization}_s_{simulation}_{fm_name}" fm_logger = logging.getLogger("forward_models") with open(error_path, encoding="utf-8") as errors: error_str = errors.read() @@ -339,7 +339,7 @@ def onerror( ], ) -> None: logging.getLogger(EVEREST).debug( - "Failed to remove {}, {}".format(path, sys_info) + f"Failed to remove {path}, {sys_info}" ) shutil.rmtree(path_to_delete, onerror=onerror) # pylint: disable=deprecated-argument diff --git a/src/ert/storage/local_experiment.py b/src/ert/storage/local_experiment.py index 16ebef794b4..39612949b10 100644 --- a/src/ert/storage/local_experiment.py +++ b/src/ert/storage/local_experiment.py @@ -193,7 +193,7 @@ def create_ensemble( ) @property - def ensembles(self) -> Generator[LocalEnsemble, None, None]: + def ensembles(self) -> Generator[LocalEnsemble]: yield from ( ens for ens in self._storage.ensembles if ens.experiment_id == self.id ) diff --git a/src/ert/storage/local_storage.py b/src/ert/storage/local_storage.py index 679d452e7c7..7f75d449293 100644 --- a/src/ert/storage/local_storage.py +++ b/src/ert/storage/local_storage.py @@ -190,11 +190,11 @@ def get_ensemble(self, uuid: UUID | str) -> LocalEnsemble: return self._ensembles[uuid] @property - def experiments(self) -> Generator[LocalExperiment, None, None]: + def experiments(self) -> Generator[LocalExperiment]: yield from self._experiments.values() @property - def ensembles(self) -> Generator[LocalEnsemble, None, None]: + def ensembles(self) -> Generator[LocalEnsemble]: yield from self._ensembles.values() def _load_index(self) -> _Index: diff --git a/src/everest/bin/config_branch_script.py b/src/everest/bin/config_branch_script.py index 6c31d3d25d2..f48fe0f1474 100644 --- a/src/everest/bin/config_branch_script.py +++ b/src/everest/bin/config_branch_script.py @@ -58,7 +58,7 @@ def _updated_initial_guess(conf_controls, opt_controls): for control in conf_controls: control.pop(CK.INITIAL_GUESS, None) - control_name = "{}_".format(control[CK.NAME]) + control_name = f"{control[CK.NAME]}_" batch_controls = { key.split(control_name)[-1]: val for key, val in opt_controls.items() @@ -69,7 +69,7 @@ def _updated_initial_guess(conf_controls, opt_controls): var_index = variable.get(CK.INDEX, None) if var_index is not None: - opt_control_name = "{}-{}".format(variable[CK.NAME], var_index) + opt_control_name = f"{variable[CK.NAME]}-{var_index}" else: opt_control_name = variable[CK.NAME] @@ -94,11 +94,11 @@ def config_branch_entry(args=None): db_path = join(optimization_dir, seba_db.FILENAME) if not exists(db_path): - parser.error("Optimization source {} not found".format(db_path)) + parser.error(f"Optimization source {db_path} not found") opt_controls = opt_controls_by_batch(optimization_dir, options.batch) if opt_controls is None: - parser.error("Batch {} not present in optimization data".format(options.batch)) + parser.error(f"Batch {options.batch} not present in optimization data") yml_config[CK.CONTROLS] = _updated_initial_guess( conf_controls=yml_config[CK.CONTROLS], opt_controls=opt_controls @@ -109,7 +109,7 @@ def config_branch_entry(args=None): yaml.preserve_quotes = True with open(options.output_config, "w", encoding="utf-8") as f: yaml.dump(yml_config, f) - print("New config file {} created.".format(options.output_config)) + print(f"New config file {options.output_config} created.") if __name__ == "__main__": diff --git a/src/everest/bin/everest_script.py b/src/everest/bin/everest_script.py index f1c11320c3c..4cc91ae7f59 100755 --- a/src/everest/bin/everest_script.py +++ b/src/everest/bin/everest_script.py @@ -101,10 +101,10 @@ async def run_everest(options): ) elif server_state["status"] == ServerStatus.never_run or options.new_run: config_dict = options.config.to_dict() - logger.info("Running everest with config info\n {}".format(config_dict)) + logger.info(f"Running everest with config info\n {config_dict}") for fm_job in options.config.forward_model or []: job_name = fm_job.split()[0] - logger.info("Everest forward model contains job {}".format(job_name)) + logger.info(f"Everest forward model contains job {job_name}") makedirs_if_needed(options.config.output_dir, roll_if_exists=True) @@ -123,9 +123,7 @@ async def run_everest(options): save_config_path = os.path.join(output_dir, config_file) options.config.dump(save_config_path) except (OSError, LookupError) as e: - logging.getLogger(EVEREST).error( - "Failed to save optimization config: {}".format(e) - ) + logging.getLogger(EVEREST).error(f"Failed to save optimization config: {e}") await start_server(options.config, options.debug) print("Waiting for server ...") wait_for_server(options.config.output_dir, timeout=600) @@ -139,10 +137,10 @@ async def run_everest(options): server_state = everserver_status(everserver_status_path) server_state_info = server_state["message"] if server_state["status"] == ServerStatus.failed: - logger.error("Everest run failed with: {}".format(server_state_info)) + logger.error(f"Everest run failed with: {server_state_info}") raise SystemExit(server_state_info) if server_state_info is not None: - logger.info("Everest run finished with: {}".format(server_state_info)) + logger.info(f"Everest run finished with: {server_state_info}") print(server_state_info) else: report_on_previous_run( diff --git a/src/everest/bin/main.py b/src/everest/bin/main.py index 7e9154bba92..0b2e355c897 100644 --- a/src/everest/bin/main.py +++ b/src/everest/bin/main.py @@ -72,7 +72,7 @@ def _build_args_parser(): arg_parser.add_argument( "--version", action="version", - version="%(prog)s {version}".format(version=everest_version), + version=f"%(prog)s {everest_version}", ) return arg_parser diff --git a/src/everest/bin/utils.py b/src/everest/bin/utils.py index 7c891f729cb..440f7398e43 100644 --- a/src/everest/bin/utils.py +++ b/src/everest/bin/utils.py @@ -53,7 +53,7 @@ def export_with_progress(config: EverestConfig, export_ecl=True): def export_to_csv(data_frame: DataFrame, export_path: str) -> None: os.makedirs(os.path.dirname(export_path), exist_ok=True) data_frame.to_csv(export_path, sep=";", index=False) - logging.getLogger(EVEREST).info("Data exported to {}".format(export_path)) + logging.getLogger(EVEREST).info(f"Data exported to {export_path}") def handle_keyboard_interrupt(signal, frame, options): diff --git a/src/everest/config/everest_config.py b/src/everest/config/everest_config.py index 9131ac57337..ce22b353649 100644 --- a/src/everest/config/everest_config.py +++ b/src/everest/config/everest_config.py @@ -757,7 +757,7 @@ def load_file(config_path: str) -> "EverestConfig": config_path = os.path.realpath(config_path) if not os.path.isfile(config_path): - raise FileNotFoundError("File not found: {}".format(config_path)) + raise FileNotFoundError(f"File not found: {config_path}") config_dict = yaml_file_to_substituted_config_dict(config_path) return EverestConfig.model_validate(config_dict) diff --git a/src/everest/config_file_loader.py b/src/everest/config_file_loader.py index 15f1d29ed6d..0c365c1d396 100644 --- a/src/everest/config_file_loader.py +++ b/src/everest/config_file_loader.py @@ -63,7 +63,7 @@ def _get_definitions(configuration, configpath): "Internal key {k} specified by user as {u}. " "Overriding as {v}".format(k=key, u=defs[key], v=val) ) - defs[key] = "<{}>".format(val) # ert uses as format + defs[key] = f"<{val}>" # ert uses as format else: logging.warn("Empty configuration file provided!") diff --git a/src/everest/detached/jobs/everserver.py b/src/everest/detached/jobs/everserver.py index 40bf0485e77..936ccedf236 100755 --- a/src/everest/detached/jobs/everserver.py +++ b/src/everest/detached/jobs/everserver.py @@ -174,9 +174,9 @@ def _find_open_port(host, lower, upper) -> int: return port except OSError: logging.getLogger("everserver").info( - "Port {} for host {} is taken".format(port, host) + f"Port {port} for host {host} is taken" ) - msg = "No open port for host {} in the range {}-{}".format(host, lower, upper) + msg = f"No open port for host {host} in the range {lower}-{upper}" logging.getLogger("everserver").exception(msg) raise Exception(msg) @@ -266,9 +266,7 @@ def main(): update_everserver_status(status_path, ServerStatus.starting) logging.getLogger(EVEREST).info(version_info()) - logging.getLogger(EVEREST).info( - "Output directory: {}".format(config.output_dir) - ) + logging.getLogger(EVEREST).info(f"Output directory: {config.output_dir}") logging.getLogger(EVEREST).debug(str(options)) authentication = _generate_authentication() @@ -436,7 +434,7 @@ def _generate_certificate(cert_folder: str): x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Bergen"), x509.NameAttribute(NameOID.LOCALITY_NAME, "Sandsli"), x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Equinor"), - x509.NameAttribute(NameOID.COMMON_NAME, "{}".format(cert_name)), + x509.NameAttribute(NameOID.COMMON_NAME, f"{cert_name}"), ] ) cert = ( @@ -448,7 +446,7 @@ def _generate_certificate(cert_folder: str): .not_valid_before(datetime.utcnow()) .not_valid_after(datetime.utcnow() + timedelta(days=365)) # 1 year .add_extension( - x509.SubjectAlternativeName([x509.DNSName("{}".format(cert_name))]), + x509.SubjectAlternativeName([x509.DNSName(f"{cert_name}")]), critical=False, ) .sign(key, hashes.SHA256(), default_backend()) diff --git a/src/everest/export.py b/src/everest/export.py index de0a2de79ae..3044f599179 100644 --- a/src/everest/export.py +++ b/src/everest/export.py @@ -130,7 +130,7 @@ def export_metadata(config: ExportConfig | None, optimization_output_dir: str): for function, gradients in opt.gradient_info.items(): for control, gradient_value in gradients.items(): md_row.update( - {"gradient-{}-{}".format(function, control): gradient_value} + {f"gradient-{function}-{control}": gradient_value} ) else: print( diff --git a/src/everest/jobs/__init__.py b/src/everest/jobs/__init__.py index 63fb608b42d..b74bf648ee6 100644 --- a/src/everest/jobs/__init__.py +++ b/src/everest/jobs/__init__.py @@ -46,7 +46,7 @@ def fetch_script(script_name): if script_name in _scripts: # noqa F821 return _scripts[script_name] # noqa F821 else: - raise KeyError("Unknown script: {}".format(script_name)) + raise KeyError(f"Unknown script: {script_name}") _inject_scripts() diff --git a/src/everest/jobs/recovery_factor.py b/src/everest/jobs/recovery_factor.py index 7d3e10e325b..1e164a5e8a0 100644 --- a/src/everest/jobs/recovery_factor.py +++ b/src/everest/jobs/recovery_factor.py @@ -17,7 +17,7 @@ def _compute_recovery_factor(load_case): def _save_object_value(object_value, target_file): with everest.jobs.io.safe_open(target_file, "w") as f: - f.write("{}\n".format(object_value)) + f.write(f"{object_value}\n") def recovery_factor(load_case, output_file): diff --git a/src/everest/jobs/templating/render.py b/src/everest/jobs/templating/render.py index db2aba6be19..946b5b99978 100644 --- a/src/everest/jobs/templating/render.py +++ b/src/everest/jobs/templating/render.py @@ -28,10 +28,10 @@ def _load_input(input_files): def _assert_input(input_files, template_file, output_file): for input_file in input_files: if not os.path.isfile(input_file): - raise ValueError("Input file: {}, does not exist..".format(input_file)) + raise ValueError(f"Input file: {input_file}, does not exist..") if not os.path.isfile(template_file): - raise ValueError("Template file: {}, does not exist..".format(template_file)) + raise ValueError(f"Template file: {template_file}, does not exist..") if not isinstance(output_file, str): raise TypeError("Expected output path to be a string") diff --git a/src/everest/simulator/everest_to_ert.py b/src/everest/simulator/everest_to_ert.py index 17f280a2a24..dca1c81b328 100644 --- a/src/everest/simulator/everest_to_ert.py +++ b/src/everest/simulator/everest_to_ert.py @@ -90,7 +90,7 @@ def _extract_summary_keys(ever_config: EverestConfig, ert_config): groups = [] group_keys = [ - "{sum_key}:{gname}".format(sum_key=sum_key, gname=gname) + f"{sum_key}:{gname}" for (sum_key, gname) in itertools.product(group_sum_keys, groups) ] @@ -109,7 +109,7 @@ def _extract_summary_keys(ever_config: EverestConfig, ert_config): wells = list(set(data_wells + everest_wells)) well_keys = [ - "{sum_key}:{wname}".format(sum_key=sum_key, wname=wname) + f"{sum_key}:{wname}" for (sum_key, wname) in itertools.product(well_sum_keys, wells) ] @@ -191,9 +191,7 @@ def _fetch_everest_jobs(ever_config: EverestConfig): mechanisms in place.""" assert ever_config.output_dir is not None job_storage = os.path.join(ever_config.output_dir, ".jobs") - logging.getLogger(EVEREST).debug( - "Creating job description files in {}".format(job_storage) - ) + logging.getLogger(EVEREST).debug(f"Creating job description files in {job_storage}") if not os.path.isdir(job_storage): os.makedirs(job_storage) @@ -205,7 +203,7 @@ def _fetch_everest_jobs(ever_config: EverestConfig): script = everest.jobs.fetch_script(default_job) job_spec_file = os.path.join(job_storage, "_" + default_job) with open(job_spec_file, "w", encoding="utf-8") as f: - f.write("EXECUTABLE {}".format(script)) + f.write(f"EXECUTABLE {script}") ever_jobs.append(Job(name=default_job, source=job_spec_file)) @@ -287,7 +285,7 @@ def _internal_data_files(ever_config: EverestConfig): assert ever_config.output_dir is not None data_storage = os.path.join(ever_config.output_dir, ".internal_data") data_storage = os.path.realpath(data_storage) - logging.getLogger(EVEREST).debug("Storing internal data in {}".format(data_storage)) + logging.getLogger(EVEREST).debug(f"Storing internal data in {data_storage}") if not os.path.isdir(data_storage): os.makedirs(data_storage) @@ -341,7 +339,7 @@ def _is_dir_all_geo(source, ever_config: EverestConfig): is_dir.append(os.path.isdir(geo_source)) if set(is_dir) == {True, False}: - msg = "Source: {} represent both files and directories".format(source) + msg = f"Source: {source} represent both files and directories" raise ValueError(msg) return is_dir[0] diff --git a/src/everest/util/__init__.py b/src/everest/util/__init__.py index dfe660ba154..a84dd7b7d19 100644 --- a/src/everest/util/__init__.py +++ b/src/everest/util/__init__.py @@ -68,13 +68,13 @@ def _roll_dir(old_name): old_name = os.path.realpath(old_name) new_name = old_name + datetime.datetime.utcnow().strftime("__%Y-%m-%d_%H.%M.%S.%f") os.rename(old_name, new_name) - logging.getLogger(EVEREST).info("renamed {} to {}".format(old_name, new_name)) + logging.getLogger(EVEREST).info(f"renamed {old_name} to {new_name}") def load_deck(fname): """Take a .DATA file and return an opm.io.Deck.""" if not os.path.exists(fname): - raise OSError('No such data file "{}".'.format(fname)) + raise OSError(f'No such data file "{fname}".') if not has_opm(): raise RuntimeError("Cannot load ECL files, opm could not be imported")