Skip to content

Commit

Permalink
style: use f-strings to format str (#1212)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed Aug 30, 2021
1 parent cf9c24f commit 5d4324d
Show file tree
Hide file tree
Showing 231 changed files with 2,940 additions and 4,080 deletions.
8 changes: 4 additions & 4 deletions .docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
with open(rst_name, "w") as f:
for line in lines:
if line.startswith("**Documentation for version"):
line = "**Documentation for version {}".format(__version__)
line = f"**Documentation for version {__version__}"
if rc_text != "":
line += " --- {}".format(rc_text)
line += f" --- {rc_text}"
line += "**\n"
f.write(line)

Expand All @@ -64,7 +64,7 @@
)
authors = __author__.split(sep=",")
for author in authors:
line += " * {}\n".format(author.strip())
line += f" * {author.strip()}\n"
line += " * and others\n\n"
f.write(line)
elif line.startswith(tag_end):
Expand All @@ -91,7 +91,7 @@

# -- Project information -----------------------------------------------------
project = "flopy Documentation"
copyright = "2021, {}".format(__author__)
copyright = f"2021, {__author__}"
author = __author__

# The version.
Expand Down
6 changes: 3 additions & 3 deletions .docs/create_rstfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
def create_section(f, title, filenames, upper_case=False):
if upper_case:
title = title.upper()
title = "{} Tutorials".format(title)
line = "{}\n".format(title) + len(title) * "-" + "\n\n"
title = f"{title} Tutorials"
line = f"{title}\n" + len(title) * "-" + "\n\n"
line += "Contents:\n\n.. toctree::\n :maxdepth: 2\n\n"
for filename in filenames:
line += " _notebooks/{}\n".format(filename)
line += f" _notebooks/{filename}\n"
line += "\n\n"
f.write(line)

Expand Down
2 changes: 1 addition & 1 deletion .docs/create_tutorials.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def create_notebooks():
# copy the python files
for src in py_files:
dst = os.path.join(wpth, os.path.basename(src))
print("{} -> {}".format(src, dst))
print(f"{src} -> {dst}")
shutil.copyfile(src, dst)

# create and run notebooks
Expand Down
6 changes: 3 additions & 3 deletions .docs/pysrc/tutorial1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
import flopy.utils.binaryfile as bf

plt.subplot(1, 1, 1, aspect="equal")
hds = bf.HeadFile(modelname + ".hds")
hds = bf.HeadFile(f"{modelname}.hds")
head = hds.get_data(totim=1.0)
levels = np.arange(1, 10, 1)
extent = (delr / 2.0, Lx - delc / 2.0, Ly - delc / 2.0, delc / 2.0)
Expand All @@ -66,12 +66,12 @@
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(1, 1, 1, aspect="equal")

hds = bf.HeadFile(modelname + ".hds")
hds = bf.HeadFile(f"{modelname}.hds")
times = hds.get_times()
head = hds.get_data(totim=times[-1])
levels = np.linspace(0, 10, 11)

cbb = bf.CellBudgetFile(modelname + ".cbc")
cbb = bf.CellBudgetFile(f"{modelname}.cbc")
kstpkper_list = cbb.get_kstpkper()
frf = cbb.get_data(text="FLOW RIGHT FACE", totim=times[-1])[0]
fff = cbb.get_data(text="FLOW FRONT FACE", totim=times[-1])[0]
Expand Down
10 changes: 5 additions & 5 deletions .docs/pysrc/tutorial2.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@
import flopy.utils.binaryfile as bf

# Create the headfile and budget file objects
headobj = bf.HeadFile(modelname + ".hds")
headobj = bf.HeadFile(f"{modelname}.hds")
times = headobj.get_times()
cbb = bf.CellBudgetFile(modelname + ".cbc")
cbb = bf.CellBudgetFile(f"{modelname}.cbc")

# Setup contour parameters
levels = np.linspace(0, 10, 11)
Expand Down Expand Up @@ -156,7 +156,7 @@
# Create the plot
f = plt.figure()
plt.subplot(1, 1, 1, aspect="equal")
plt.title("stress period " + str(iplot + 1))
plt.title(f"stress period {iplot + 1}")

modelmap = flopy.plot.PlotMapView(model=mf, layer=0)
qm = modelmap.plot_ibound()
Expand All @@ -181,13 +181,13 @@
zorder=9,
)
plt.text(wpt[0] + 25, wpt[1] - 25, "well", size=12, zorder=12)
plt.savefig("tutorial2-{}.png".format(iplot))
plt.savefig(f"tutorial2-{iplot}.png")

# Plot the head versus time
idx = (0, int(nrow / 2) - 1, int(ncol / 2) - 1)
ts = headobj.get_ts(idx)
plt.subplot(1, 1, 1)
ttl = "Head at cell ({0},{1},{2})".format(idx[0] + 1, idx[1] + 1, idx[2] + 1)
ttl = f"Head at cell ({idx[0] + 1},{idx[1] + 1},{idx[2] + 1})"
plt.title(ttl)
plt.xlabel("time")
plt.ylabel("head")
Expand Down
2 changes: 1 addition & 1 deletion autotest/autotest_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def run_notebook(dpth, fn):
)
print(" ".join(arg))
ival = os.system(" ".join(arg))
assert ival == 0, "could not run {}".format(fn)
assert ival == 0, f"could not run {fn}"


def test_notebooks():
Expand Down
12 changes: 6 additions & 6 deletions autotest/autotest_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def copy_scripts(src_dir, dst_dir, include_subdir=False):
dst = os.path.join(dst_dir, os.path.basename(src))

# copy script
print("copying {} from {} to {}".format(filename, filedir, testdir))
print(f"copying {filename} from {filedir} to {testdir}")
shutil.copyfile(src, dst)

return [os.path.basename(filepath) for filepath in files]
Expand All @@ -77,29 +77,29 @@ def run_scripts(fn, testdir):

# change to working directory
opth = os.getcwd()
print('changing to working directory "{}"'.format(testdir))
print(f'changing to working directory "{testdir}"')
os.chdir(testdir)

# run the script
ival = run()

# change back to starting directory
print('changing back to starting directory "{}"'.format(opth))
print(f'changing back to starting directory "{opth}"')
os.chdir(opth)

# make sure script ran successfully
assert ival == 0, "could not run {}".format(fn)
assert ival == 0, f"could not run {fn}"


def run_tutorial_scripts(fn, testdir):
args = ("python", fn)
print("running...'{}'".format(" ".join(args)))
print(f"running...'{' '.join(args)}'")
proc = Popen(args, stdout=PIPE, stderr=PIPE, cwd=testdir)
stdout, stderr = proc.communicate()
if stdout:
print(stdout.decode("utf-8"))
if stderr:
print("Errors:\n{}".format(stderr.decode("utf-8")))
print(f"Errors:\n{stderr.decode('utf-8')}")

return

Expand Down
10 changes: 5 additions & 5 deletions autotest/get_exes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@
if dotlocal:
bindir = os.path.join(os.path.expanduser("~"), ".local", "bin")
bindir = os.path.abspath(bindir)
print("bindir: {}".format(bindir))
print(f"bindir: {bindir}")
if not os.path.isdir(bindir):
os.makedirs(bindir)

# write where the executables will be downloaded
print('modflow executables will be downloaded to:\n\n "{}"'.format(bindir))
print(f'modflow executables will be downloaded to:\n\n "{bindir}"')


def get_branch():
Expand Down Expand Up @@ -97,13 +97,13 @@ def move_exe():
continue
src = os.path.join(exe_pth, file)
dst = os.path.join(bindir, file)
print("moving {} -> {}".format(src, dst))
print(f"moving {src} -> {dst}")
shutil.move(src, dst)
return


def list_exes():
cmd = "ls -l {}".format(bindir)
cmd = f"ls -l {bindir}"
os.system(cmd)
return

Expand All @@ -125,7 +125,7 @@ def test_download_nightly_build():

# get the current branch
branch = get_branch()
print("current branch: {}".format(branch))
print(f"current branch: {branch}")

# No need to replace MODFLOW 6 executables
if branch == "master":
Expand Down
6 changes: 3 additions & 3 deletions autotest/t004_test_utilarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def stress_util2d(ml, nlay, nrow, ncol):
# save hk up one dir from model_ws
fnames = []
for i, h in enumerate(hk):
fname = os.path.join(out_dir, "test_{0}.ref".format(i))
fname = os.path.join(out_dir, f"test_{i}.ref")
fnames.append(fname)
np.savetxt(fname, h, fmt="%15.6e", delimiter="")
vk[i] = i + 1.0
Expand Down Expand Up @@ -406,7 +406,7 @@ def stress_util2d_for_joe_the_file_king(ml, nlay, nrow, ncol):
# save hk up one dir from model_ws
fnames = []
for i, h in enumerate(hk):
fname = os.path.join("test_{0}.ref".format(i))
fname = os.path.join(f"test_{i}.ref")
fnames.append(fname)
np.savetxt(fname, h, fmt="%15.6e", delimiter="")
vk[i] = i + 1.0
Expand Down Expand Up @@ -746,7 +746,7 @@ def test_mflist():
.to_records(index=True)
.astype(data.dtype)
)
errmsg = "data not equal:\n {}\n {}".format(dfdata, data)
errmsg = f"data not equal:\n {dfdata}\n {data}"
assert np.array_equal(dfdata, data), errmsg

m4ds = ml.wel.stress_period_data.masked_4D_arrays
Expand Down
8 changes: 4 additions & 4 deletions autotest/t005_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ def test_modflow_unstructured():

# write well file
wel.write_file()
assert os.path.isfile(os.path.join(cpth, "{}.wel".format(mf.name))) is True
assert os.path.isfile(os.path.join(cpth, f"{mf.name}.wel")) is True
wel2 = flopy.modflow.ModflowWel.load(
os.path.join(cpth, "{}.wel".format(mf.name)), mf
os.path.join(cpth, f"{mf.name}.wel"), mf
)
assert wel2.stress_period_data[0] == wel.stress_period_data[0]

# write ghb file
ghb.write_file(check=False)
assert os.path.isfile(os.path.join(cpth, "{}.ghb".format(mf.name))) is True
assert os.path.isfile(os.path.join(cpth, f"{mf.name}.ghb")) is True
ghb2 = flopy.modflow.ModflowGhb.load(
os.path.join(cpth, "{}.ghb".format(mf.name)), mf
os.path.join(cpth, f"{mf.name}.ghb"), mf
)
assert ghb2.stress_period_data[0] == ghb.stress_period_data[0]

Expand Down
2 changes: 1 addition & 1 deletion autotest/t006_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_cbc_ts():
)
zobj = flopy.utils.CellBudgetFile(fpth, precision="single")
ts = zobj.get_ts(text="ZETASRF 1", idx=(0, 0, 24))
errtxt = "shape of zeta timeseries is {} not (4, 2)".format(ts.shape)
errtxt = f"shape of zeta timeseries is {ts.shape} not (4, 2)"
assert ts.shape == (4, 2), errtxt


Expand Down
Loading

0 comments on commit 5d4324d

Please sign in to comment.