Skip to content

Commit

Permalink
chore: reformat Python code with line length = 88 (#198)
Browse files Browse the repository at this point in the history
Reformat the code with a maximum line length of 88 characters to better represent the code.

See modflowpy/flopy#2362 for a related PR with rational and discussion.

Also change some of Ruff's configuration:

- Remove target-version, since it is automatically evaluated from pyproject.toml
- Remove include so that Ruff will work globally in this repo
  • Loading branch information
mwtoews authored Nov 21, 2024
1 parent 91f1576 commit 2f58d33
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 205 deletions.
4 changes: 1 addition & 3 deletions autotest/test_mf2005.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,5 @@ def test_mf2005(namefile, workspace, target):
if not (example_ws / namefile).is_file():
pytest.skip(f"{namefile} does not exist")

success, _ = flopy.run_model(
target, namefile, model_ws=example_ws, silent=False
)
success, _ = flopy.run_model(target, namefile, model_ws=example_ws, silent=False)
assert success, f"could not run {namefile} with {target}"
8 changes: 2 additions & 6 deletions autotest/test_mp6.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@ def test_compile(pm, target):
@pytest.mark.dependency(name="test", depends=["build"])
@pytest.mark.xdist_group(TARGET_NAME)
@pytest.mark.regression
@pytest.mark.parametrize(
"namefile", [f"EXAMPLE-{n}.mpsim" for n in range(1, 10)]
)
@pytest.mark.parametrize("namefile", [f"EXAMPLE-{n}.mpsim" for n in range(1, 10)])
def test_mp6(namefile, workspace, target):
example_ws = workspace / "example-run"
if not (example_ws / namefile).is_file():
pytest.skip(f"Namefile {namefile} does not exist")

update_files(namefile, example_ws)
success, _ = flopy.run_model(
target, namefile, model_ws=example_ws, silent=False
)
success, _ = flopy.run_model(target, namefile, model_ws=example_ws, silent=False)
assert success, f"could not run {namefile}"
16 changes: 4 additions & 12 deletions autotest/test_mp7.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ def pm(module_tmpdir, target) -> pymake.Pymake:

def replace_data(dpth):
fpths = [
name
for name in os.listdir(dpth)
if os.path.isfile(os.path.join(dpth, name))
name for name in os.listdir(dpth) if os.path.isfile(os.path.join(dpth, name))
]
repl = False
if "ex01_mf2005.dis" in fpths:
Expand Down Expand Up @@ -137,9 +135,7 @@ def run_modpath7(namefile, mp7_exe, mf2005_exe, mfusg_exe, mf6_exe):

if success:
fpth = os.path.basename(namefile)
success, _ = flopy.run_model(
mp7_exe, fpth, model_ws=model_ws, silent=False
)
success, _ = flopy.run_model(mp7_exe, fpth, model_ws=model_ws, silent=False)

return success

Expand All @@ -163,14 +159,10 @@ def test_compile(pm, target):
@pytest.mark.xdist_group(TARGET_NAME)
@pytest.mark.regression
def test_download_exes(module_tmpdir):
pymake.getmfexes(
str(module_tmpdir), exes=("mf2005", "mfusg", "mf6"), verbose=True
)
pymake.getmfexes(str(module_tmpdir), exes=("mf2005", "mfusg", "mf6"), verbose=True)


@pytest.mark.dependency(
name="test", depends=["download", "download_exes", "build"]
)
@pytest.mark.dependency(name="test", depends=["download", "download_exes", "build"])
@pytest.mark.xdist_group(TARGET_NAME)
@pytest.mark.regression
@pytest.mark.parametrize(
Expand Down
4 changes: 1 addition & 3 deletions autotest/test_mt3dusgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def run_mt3dusgs(workspace, mt3dms_exe, mfnwt_exe, mf6_exe):
model_ws = workspace

files = [
f
for f in os.listdir(model_ws)
if os.path.isfile(os.path.join(model_ws, f))
f for f in os.listdir(model_ws) if os.path.isfile(os.path.join(model_ws, f))
]

mf_nam = None
Expand Down
14 changes: 4 additions & 10 deletions autotest/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ def test_mfexes_download_and_unzip_and_zip(function_tmpdir):
success = pymake.zip_all(
str(zip_pth),
file_pths=[
os.path.join(function_tmpdir, e)
for e in os.listdir(function_tmpdir)
os.path.join(function_tmpdir, e) for e in os.listdir(function_tmpdir)
],
)
assert success, "could not create zipfile using file names"
Expand All @@ -178,9 +177,7 @@ def test_mfexes_download_and_unzip_and_zip(function_tmpdir):
# zip up exe's using directories and a pattern
zip_pth = function_tmpdir / "ziptest03.zip"
print(f"creating '{zip_pth}'")
success = pymake.zip_all(
str(zip_pth), dir_pths=function_tmpdir, patterns="mf"
)
success = pymake.zip_all(str(zip_pth), dir_pths=function_tmpdir, patterns="mf")
assert success, "could not create zipfile using directories and a pattern"

# zip up exe's using files and directories
Expand All @@ -189,8 +186,7 @@ def test_mfexes_download_and_unzip_and_zip(function_tmpdir):
success = pymake.zip_all(
str(zip_pth),
file_pths=[
os.path.join(function_tmpdir, e)
for e in os.listdir(function_tmpdir)
os.path.join(function_tmpdir, e) for e in os.listdir(function_tmpdir)
],
dir_pths=function_tmpdir,
)
Expand Down Expand Up @@ -283,9 +279,7 @@ def test_usgsprograms_export_json(module_tmpdir):
def test_usgsprograms_load_json_error(module_tmpdir):
fpth = os.path.join(module_tmpdir, "code.test.error.json")
my_dict = {"mf2005": {"bad": 12, "key": True}}
pymake.usgs_program_data.export_json(
fpth=fpth, prog_data=my_dict, update=False
)
pymake.usgs_program_data.export_json(fpth=fpth, prog_data=my_dict, update=False)

with pytest.raises(KeyError):
pymake.usgs_program_data.load_json(fpth=fpth)
Expand Down
4 changes: 1 addition & 3 deletions pymake/plot/dependency_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ def _add_pydot_edges(graph, node_dict, edge_set, n, ilev, level):
edge_set.add(tpl)
edge = pydot.Edge(node_dict[n], node_dict[m])
graph.add_edge(edge)
_add_pydot_edges(
graph, node_dict, edge_set, m, ilev + 1, level
)
_add_pydot_edges(graph, node_dict, edge_set, m, ilev + 1, level)
return


Expand Down
30 changes: 7 additions & 23 deletions pymake/pymake.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ def __init__(self, name="pymake", verbose=None):
setattr(self, key, value["default"])

# parse command line arguments if python is running script
if (
sys.argv[0].lower().endswith(".py")
or "make-program" in sys.argv[0].lower()
):
if sys.argv[0].lower().endswith(".py") or "make-program" in sys.argv[0].lower():
self._arg_parser()

# reset select variables using passed variables
Expand Down Expand Up @@ -298,9 +295,7 @@ def compress_targets(self):
+ f"directory to zip file '{pl.Path(zip_pth).resolve()}'"
)
for idx, target in enumerate(targets):
print(
f" {idx + 1:>3d}. adding " + f"'{target}' to zipfile"
)
print(f" {idx + 1:>3d}. adding " + f"'{target}' to zipfile")

# compress the compiled executables
if not zip_all(
Expand Down Expand Up @@ -654,10 +649,7 @@ def _set_extrafiles(self):
fpth = os.path.join(srcdir, extrafiles)
extrafiles = os.path.normpath(fpth)
else:
msg = (
"invalid extrafiles format - "
+ "must be a list or string"
)
msg = "invalid extrafiles format - " + "must be a list or string"
raise ValueError(msg)

# reset extrafiles
Expand All @@ -677,9 +669,7 @@ def _set_excludefiles(self):
"""
if self.excludefiles is None:
if self._get_base_target() in ("libmf6",):
self.excludefiles = [
os.path.join(self.download_dir, "src", "mf6.f90")
]
self.excludefiles = [os.path.join(self.download_dir, "src", "mf6.f90")]
return

def build(self, target=None, srcdir=None, modify_exe_name=False):
Expand Down Expand Up @@ -730,9 +720,7 @@ def build(self, target=None, srcdir=None, modify_exe_name=False):
if self.fc != "none":
if self.fflags is None:
optlevel = (
_get_optlevel(
self.target, self.fc, self.cc, self.debug, [], []
)
_get_optlevel(self.target, self.fc, self.cc, self.debug, [], [])
+ " "
)

Expand All @@ -749,9 +737,7 @@ def build(self, target=None, srcdir=None, modify_exe_name=False):
if self.cc != "none":
if self.cflags is None:
optlevel = (
_get_optlevel(
self.target, self.fc, self.cc, self.debug, [], []
)
_get_optlevel(self.target, self.fc, self.cc, self.debug, [], [])
+ " "
)

Expand All @@ -776,9 +762,7 @@ def build(self, target=None, srcdir=None, modify_exe_name=False):
)[1]
)

self.target = self.update_target(
self.target, modify_target=modify_exe_name
)
self.target = self.update_target(self.target, modify_target=modify_exe_name)

build_target = self.set_build_target_bool()

Expand Down
45 changes: 10 additions & 35 deletions pymake/pymake_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,10 +635,7 @@ def _clean_temp_files(

# remove temporary directories
if verbose:
msg = (
"\nCleaning up temporary source, object, "
+ "and module directories..."
)
msg = "\nCleaning up temporary source, object, " + "and module directories..."
print(msg)
if not inplace:
if os.path.isdir(srcdir_temp):
Expand Down Expand Up @@ -844,9 +841,7 @@ def _pymake_compile(
)

# set optimization levels
optlevel = _get_optlevel(
target, fc, cc, debug, fflags, cflags, verbose=verbose
)
optlevel = _get_optlevel(target, fc, cc, debug, fflags, cflags, verbose=verbose)

# get fortran and c compiler switches
tfflags = _get_fortran_flags(
Expand Down Expand Up @@ -1161,9 +1156,7 @@ def _create_win_batch(
+ f"{latest_version}\\env\\vars.bat"
)
else:
cpvars = (
"C:\\Program Files (x86)\\Intel\\oneAPI\\" + "setvars.bat"
)
cpvars = "C:\\Program Files (x86)\\Intel\\oneAPI\\" + "setvars.bat"
if not os.path.isfile(cpvars):
raise Exception(f"Could not find cpvars: {cpvars}")
intel_setvars = f'"{cpvars}"'
Expand All @@ -1174,18 +1167,15 @@ def _create_win_batch(
for ift in iflist:
stand_alone_intel = os.environ.get(ift)
if stand_alone_intel is not None:
cpvars = os.path.join(
stand_alone_intel, "bin", "compilervars.bat"
)
cpvars = os.path.join(stand_alone_intel, "bin", "compilervars.bat")
if not os.path.isfile(cpvars):
raise Exception(f"Could not find cpvars: {cpvars}")
intel_setvars = '"' + os.path.normpath(cpvars) + '" ' + arch
break
# check if either OneAPI or stand alone intel is installed
if intel_setvars is None:
err_msg = (
"OneAPI or stand alone version of Intel compilers "
+ "is not installed"
"OneAPI or stand alone version of Intel compilers " + "is not installed"
)
raise ValueError(err_msg)

Expand All @@ -1205,11 +1195,7 @@ def _create_win_batch(
searchdir.append(dirname)

# write commands to build object files
line = (
"echo Creating object files to create '"
+ os.path.basename(target)
+ "'\n"
)
line = "echo Creating object files to create '" + os.path.basename(target) + "'\n"
f.write(line)
for srcfile in srcfiles:
if srcfile.endswith(".c") or srcfile.endswith(".cpp"):
Expand Down Expand Up @@ -1243,11 +1229,7 @@ def _create_win_batch(
f.write(cmd + "\n")

# write commands to link
line = (
"echo Linking object files to create '"
+ os.path.basename(target)
+ "'\n"
)
line = "echo Linking object files to create '" + os.path.basename(target) + "'\n"
f.write(line)

# assemble the link command
Expand Down Expand Up @@ -1460,9 +1442,7 @@ def _create_makefile(
for ext in cext:
f.write(f"$(OBJDIR)/%{objext} : %{ext}\n")
f.write("\t@mkdir -p $(@D)\n")
line = (
"\t$(CC) $(OPTLEVEL) $(CFLAGS) -c $< -o $@ " + "$(INCSWITCH)\n"
)
line = "\t$(CC) $(OPTLEVEL) $(CFLAGS) -c $< -o $@ " + "$(INCSWITCH)\n"
f.write(f"{line}\n")

# close the makefile
Expand All @@ -1483,10 +1463,7 @@ def _create_makefile(
line += "\tdetected_OS = Windows\n"
line += "\tOS_macro = -D_WIN32\n"
line += "else\n"
line += (
"\tdetected_OS = $(shell sh -c 'uname 2>/dev/null "
+ "|| echo Unknown')\n"
)
line += "\tdetected_OS = $(shell sh -c 'uname 2>/dev/null " + "|| echo Unknown')\n"
line += "\tifeq ($(detected_OS), Darwin)\n"
line += "\t\tOS_macro = -D__APPLE__\n"
line += "\telse\n"
Expand Down Expand Up @@ -1566,9 +1543,7 @@ def _create_makefile(
f.write(line)

# optimization level
optlevel = _get_optlevel(
target, fc, cc, debug, fflags, cflags, verbose=verbose
)
optlevel = _get_optlevel(target, fc, cc, debug, fflags, cflags, verbose=verbose)
line = "# set the optimization level (OPTLEVEL) if not defined\n"
line += f"OPTLEVEL ?= {optlevel.replace('/', '-')}\n\n"
f.write(line)
Expand Down
9 changes: 2 additions & 7 deletions pymake/pymake_build_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,7 @@ def build_apps(

# determine if the target should be built
build_target = pmobj.set_build_target_bool(
target=pmobj.update_target(
target, modify_target=update_target_name
)
target=pmobj.update_target(target, modify_target=update_target_name)
)

# setup download for target
Expand All @@ -252,10 +250,7 @@ def build_apps(
end_downcomp = datetime.now()
elapsed = end_downcomp - start_downcomp
if pmobj.verbose:
print(
"elapsed download and compile time (hh:mm:ss.ms): "
+ f"{elapsed}\n"
)
print("elapsed download and compile time (hh:mm:ss.ms): " + f"{elapsed}\n")

end_time = datetime.now()
elapsed = end_time - start_time
Expand Down
5 changes: 1 addition & 4 deletions pymake/utils/_Popen_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ def _process_Popen_communicate(proc, verbose=True):

# catch non-zero return code
if proc.returncode != 0:
msg = (
f"{' '.join(proc.args)} failed\n"
+ f"\tstatus code {proc.returncode}\n"
)
msg = f"{' '.join(proc.args)} failed\n" + f"\tstatus code {proc.returncode}\n"
print(msg)

return stderr, stdout
Expand Down
5 changes: 1 addition & 4 deletions pymake/utils/_compiler_language_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,7 @@ def _preprocess_file(srcfiles, meson=False):
break

else:
msg = (
"_preprocess_file: could not "
+ f"open {os.path.basename(srcfile)}"
)
msg = "_preprocess_file: could not " + f"open {os.path.basename(srcfile)}"
raise FileNotFoundError(msg)

return preprocess
Expand Down
Loading

0 comments on commit 2f58d33

Please sign in to comment.