Skip to content

Commit

Permalink
style: convert python scripts to use f-strings (MODFLOW-USGS#965)
Browse files Browse the repository at this point in the history
Downgrade GitHub Actions windows VM to use server-2019.
Black formatting and isort on python scripts.
  • Loading branch information
jdhughes-usgs authored and Hofer-Julian committed Jul 15, 2022
1 parent f411ff2 commit d39887c
Show file tree
Hide file tree
Showing 195 changed files with 3,370 additions and 3,487 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-tests-gfortran-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, windows-2019, macos-latest]
defaults:
run:
shell: bash
Expand Down
4 changes: 2 additions & 2 deletions autotest/binary_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def write_budget(
# write auxiliary column names
naux = ndat - 1
if naux > 0:
auxtxt = ["{:16}".format(colname) for colname in colnames[3:]]
auxtxt = [f"{colname:16}" for colname in colnames[3:]]
auxtxt = tuple(auxtxt)
dt = np.dtype([(colname, "S16") for colname in colnames[3:]])
h = np.array(auxtxt, dtype=dt)
Expand All @@ -143,7 +143,7 @@ def write_budget(

pass
else:
raise Exception("unknown method code {}".format(imeth))
raise Exception(f"unknown method code {imeth}")
return


Expand Down
49 changes: 23 additions & 26 deletions autotest/budget_file_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@


import os

import numpy as np


Expand All @@ -34,11 +35,11 @@ def eval_bud_diff(fpth, b0, b1, ia=None, dtol=1e-6):

# open a summary file and write header
f = open(fpth, "w")
line = "{:15s}".format("Time")
line += " {:15s}".format("Datatype")
line += " {:15s}".format("File 1")
line += " {:15s}".format("File 2")
line += " {:15s}".format("Difference")
line = f"{'Time':15s}"
line += f" {'Datatype':15s}"
line += f" {'File 1':15s}"
line += f" {'File 2':15s}"
line += f" {'Difference':15s}"
f.write(line + "\n")
f.write(len(line) * "-" + "\n")

Expand Down Expand Up @@ -79,45 +80,41 @@ def eval_bud_diff(fpth, b0, b1, ia=None, dtol=1e-6):
difftime = t
if abs(diff) > dtol:
fail = True
line = "{:15g}".format(t)
line += " {:15s}".format(key)
line += " {:15g}".format(v0)
line += " {:15g}".format(v1)
line += " {:15g}".format(diff)
line = f"{t:15g}"
line += f" {key:15s}"
line += f" {v0:15g}"
line += f" {v1:15g}"
line += f" {diff:15g}"
f.write(line + "\n")

# evaluate the sums
diff = v0sum - v1sum
if abs(diff) > dtol:
fail = True
line = "{:15g}".format(t)
line += " {:15s}".format("TOTAL")
line += " {:15g}".format(v0sum)
line += " {:15g}".format(v1sum)
line += " {:15g}".format(diff)
line = f"{t:15g}"
line += f" {'TOTAL':15s}"
line += f" {v0sum:15g}"
line += f" {v1sum:15g}"
line += f" {diff:15g}"
f.write(line + "\n")

msg = "\nSummary of changes in {}\n".format(os.path.basename(fpth))
msg = f"\nSummary of changes in {os.path.basename(fpth)}\n"
msg += "-" * 72 + "\n"
msg += "Maximum cbc difference: {}\n".format(diffmax)
msg += "Maximum cbc difference time: {}\n".format(difftime)
msg += "Maximum cbc datatype: {}\n".format(difftag)
msg += f"Maximum cbc difference: {diffmax}\n"
msg += f"Maximum cbc difference time: {difftime}\n"
msg += f"Maximum cbc datatype: {difftag}\n"
if fail:
msg += "Maximum cbc criteria exceeded: {}".format(dtol)
msg += f"Maximum cbc criteria exceeded: {dtol}"
assert not fail, msg

# close summary file and print the final message
f.close()
print(msg)

msg = "sum of first cbc file flows ({}) ".format(
v0sum
) + "exceeds dtol ({})".format(dtol)
msg = f"sum of first cbc file flows ({v0sum}) " + f"exceeds dtol ({dtol})"
assert abs(v0sum) < dtol, msg

msg = "sum of second cbc file flows ({}) ".format(
v1sum
) + "exceeds dtol ({})".format(dtol)
msg = f"sum of second cbc file flows ({v1sum}) " + f"exceeds dtol ({dtol})"
assert abs(v1sum) < dtol, msg

return
9 changes: 5 additions & 4 deletions autotest/build_exes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
# python -c "import build_exes; build_exes.test_build_modflow6()"

import os
import sys
import shutil
import sys

import pymake

from framework import running_on_CI
Expand Down Expand Up @@ -90,7 +91,7 @@ def build_mf6(srcdir=None, appdir=None):
# build the application
pm.build()

msg = "{} does not exist.".format(pm.target)
msg = f"{pm.target} does not exist."
assert pm.returncode == 0, msg


Expand All @@ -115,7 +116,7 @@ def build_mf6_so():
# build the application
pm.build()

msg = "{} does not exist.".format(pm.target)
msg = f"{pm.target} does not exist."
assert pm.returncode == 0, msg


Expand Down Expand Up @@ -182,7 +183,7 @@ def build_zbud6():
# build the application
pm.build()

msg = "{} does not exist.".format(pm.target)
msg = f"{pm.target} does not exist."
assert pm.returncode == 0, msg


Expand Down
39 changes: 15 additions & 24 deletions autotest/build_mfio_tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def test_rebuild_from_dfn():
# run python
argv = ["python", "mf6ivar.py"]
buff, ierr = run_command(argv, pth)
msg = "\nERROR {}: could not run {} with {}".format(
ierr, argv[0], argv[1]
)
msg = f"\nERROR {ierr}: could not run {argv[0]} with {argv[1]}"
assert ierr == 0, buff + msg

# get list for dfn files
Expand All @@ -85,13 +83,14 @@ def test_rebuild_from_dfn():
for f in dfnfiles:
if "common" in f:
continue
fpth = "{}-desc".format(f)
fpth = f"{f}-desc"
if fpth not in texfiles:
icnt += 1
missing += " {:3d} {}.tex\n".format(icnt, fpth)
msg = "\n{} TeX file(s) are missing. ".format(
icnt
) + "Missing files:\n{}".format(missing)
missing += f" {icnt:3d} {fpth}.tex\n"
msg = (
"\n{} TeX file(s) are missing. ".format(icnt)
+ f"Missing files:\n{missing}"
)
assert icnt == 0, msg

return
Expand All @@ -114,30 +113,22 @@ def test_build_mfio():
# build pdf
argv = ["pdflatex", f"{base_name}.tex"]
buff, ierr = run_command(argv, pth)
msg = "\nERROR {}: could not run {} on {}".format(
ierr, argv[0], argv[1]
)
msg = f"\nERROR {ierr}: could not run {argv[0]} on {argv[1]}"
assert ierr == 0, buff + msg

argv = ["bibtex", f"{base_name}.aux"]
buff, ierr = run_command(argv, pth)
msg = "\nERROR {}: could not run {} on {}".format(
ierr, argv[0], argv[1]
)
msg = f"\nERROR {ierr}: could not run {argv[0]} on {argv[1]}"
assert ierr == 0, buff + msg

argv = ["pdflatex", f"{base_name}.tex"]
buff, ierr = run_command(argv, pth)
msg = "\nERROR {}: could not run {} on {}".format(
ierr, argv[0], argv[1]
)
msg = f"\nERROR {ierr}: could not run {argv[0]} on {argv[1]}"
assert ierr == 0, buff + msg

argv = ["pdflatex", f"{base_name}.tex"]
buff, ierr = run_command(argv, pth)
msg = "\nERROR {}: could not run {} on {}".format(
ierr, argv[0], argv[1]
)
msg = f"\nERROR {ierr}: could not run {argv[0]} on {argv[1]}"
assert ierr == 0, buff + msg

return
Expand All @@ -161,10 +152,10 @@ def delete_files(files, pth, allow_failure=False):
for file in files:
fpth = os.path.join(pth, file)
try:
print("removing...{}".format(file))
print(f"removing...{file}")
os.remove(fpth)
except:
print("could not remove...{}".format(file))
print(f"could not remove...{file}")
if not allow_failure:
return False
return True
Expand Down Expand Up @@ -194,7 +185,7 @@ def run_command(argv, pth, timeout=10):
def main():
# write message
tnam = os.path.splitext(os.path.basename(__file__))[0]
msg = "Running {} test".format(tnam)
msg = f"Running {tnam} test"
print(msg)

print("running...test_rebuild_from_dfn()")
Expand All @@ -210,7 +201,7 @@ def main():


if __name__ == "__main__":
print("standalone run of {}".format(os.path.basename(__file__)))
print(f"standalone run of {os.path.basename(__file__)}")

# run main routine
main()
8 changes: 4 additions & 4 deletions autotest/common_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_home_dir():

home = os.path.join(*cwd_list[:ipos])

print("HOME: {}".format(home))
print(f"HOME: {home}")

return home

Expand All @@ -67,8 +67,8 @@ def is_directory_available(example_basedir):
if example_basedir is not None:
available = os.path.isdir(example_basedir)
if not available:
print('"{}" does not exist'.format(example_basedir))
print("no need to run {}".format(os.path.basename(__file__)))
print(f'"{example_basedir}" does not exist')
print(f"no need to run {os.path.basename(__file__)}")
return available


Expand All @@ -83,7 +83,7 @@ def get_example_basedir(home, find_dir, subdir=None):
break
if example_basedir is not None:
example_basedir = os.path.abspath(example_basedir)
print("Example base directory: {}".format(example_basedir))
print(f"Example base directory: {example_basedir}")
break
return example_basedir

Expand Down
11 changes: 6 additions & 5 deletions autotest/get_exes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import shutil

import pymake

from framework import running_on_CI
Expand Down Expand Up @@ -50,7 +51,7 @@ def create_dir(pth):
print(f"creating... {os.path.abspath(pth)}")
os.makedirs(pth, exist_ok=True)

msg = "could not create... {}".format(os.path.abspath(pth))
msg = f"could not create... {os.path.abspath(pth)}"
assert os.path.exists(pth), msg


Expand All @@ -71,14 +72,14 @@ def rebuild_mf6_release():
with open(fpth) as f:
lines = f.read().splitlines()

assert len(lines) > 0, "could not update {}".format(srcpth)
assert len(lines) > 0, f"could not update {srcpth}"

f = open(fpth, "w")
for line in lines:
tag = "IDEVELOPMODE = 0"
if tag in line:
line = line.replace(tag, "IDEVELOPMODE = 1")
f.write("{}\n".format(line))
f.write(f"{line}\n")
f.close()

# reset compiler based on environmental variable, if defined
Expand All @@ -91,7 +92,7 @@ def rebuild_mf6_release():
# build the release version of MODFLOW 6
pm.build()

msg = "{} does not exist.".format(pm.target)
msg = f"{pm.target} does not exist."
assert pm.returncode == 0, msg

# finalize the build
Expand All @@ -111,7 +112,7 @@ def test_getmfexes(verify=True):
srcpth = os.path.join(mfexe_pth, target)
if os.path.isfile(srcpth):
dstpth = os.path.join(downloaded_bindir, target)
print("copying {} -> {}".format(srcpth, dstpth))
print(f"copying {srcpth} -> {dstpth}")
shutil.copy(srcpth, dstpth)


Expand Down
Loading

0 comments on commit d39887c

Please sign in to comment.