Skip to content

Commit

Permalink
Run pyupgrade on src tree
Browse files Browse the repository at this point in the history
  • Loading branch information
JHolba committed Dec 11, 2024
1 parent c9d43bc commit 5918003
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/ert/run_models/base_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/ert/run_models/everest_run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ert/storage/local_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
4 changes: 2 additions & 2 deletions src/ert/storage/local_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions src/everest/bin/config_branch_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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__":
Expand Down
12 changes: 5 additions & 7 deletions src/everest/bin/everest_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/everest/bin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/everest/bin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion src/everest/config/everest_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/everest/config_file_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <GEO_ID> as format
defs[key] = f"<{val}>" # ert uses <GEO_ID> as format
else:
logging.warn("Empty configuration file provided!")

Expand Down
12 changes: 5 additions & 7 deletions src/everest/detached/jobs/everserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = (
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion src/everest/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/everest/jobs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion src/everest/jobs/recovery_factor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/everest/jobs/templating/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 6 additions & 8 deletions src/everest/simulator/everest_to_ert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
]

Expand All @@ -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)
]

Expand Down Expand Up @@ -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)
Expand All @@ -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))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/everest/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5918003

Please sign in to comment.