Skip to content

Commit

Permalink
Python 3.8 walrus if :=
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jan 9, 2025
1 parent 642f588 commit 1f2930d
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 80 deletions.
4 changes: 1 addition & 3 deletions scripts/plot_all_subdir.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ def main(top: Path, resume: bool) -> None:
)
P = p.parse_args()

top_dir = Path(P.top_dir).expanduser()

if not top_dir.is_dir():
if not (top_dir := Path(P.top_dir).expanduser()).is_dir():
raise NotADirectoryError(top_dir)

main(top_dir, P.resume)
13 changes: 4 additions & 9 deletions scripts/plot_look_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
p.add_argument("new_dir", help="New: top level directory to look one level below")
P = p.parse_args()

ref_dir = Path(P.ref_dir).expanduser().resolve()
new_dir = Path(P.new_dir).expanduser().resolve()

if not ref_dir.is_dir():
if not (ref_dir := Path(P.ref_dir).expanduser().resolve()).is_dir():
raise NotADirectoryError(ref_dir)
if not new_dir.is_dir():
if not (new_dir := Path(P.new_dir).expanduser().resolve()).is_dir():
raise NotADirectoryError(new_dir)
if ref_dir.samefile(new_dir):
raise ValueError("ref_dir and new_dir must be different")
Expand All @@ -48,11 +45,9 @@

for v in var:
plot_name = sorted((new_sim / "plots").glob(f"{v}-*.png"))[-1].name
ref_file = ref_dir / new_name / "plots" / plot_name
new_file = new_sim / "plots" / plot_name

if not ref_file.is_file():
if not (ref_file := ref_dir / new_name / "plots" / plot_name).is_file():
raise FileNotFoundError(ref_file)
new_file = new_sim / "plots" / plot_name

axs[0].imshow(imread(ref_file))
axs[0].set_title(ref_file, fontsize=fs)
Expand Down
3 changes: 1 addition & 2 deletions scripts/plot_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
if args.verbose:
logging.basicConfig(level=logging.DEBUG)

indir = Path(args.indir).expanduser()
if not indir.is_dir():
if not (indir := Path(args.indir).expanduser()).is_dir():
raise NotADirectoryError(indir)

plot.patch(indir, var=set(args.var))
9 changes: 3 additions & 6 deletions src/gemini3d/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@


def cmake_exe() -> str:
cmake = shutil.which("cmake")
if not cmake:
if not (cmake := shutil.which("cmake")):
# try to help if Homebrew or Ports is not on PATH
if sys.platform == "darwin":
paths = ["/opt/homebrew/bin", "/usr/local/bin", "/opt/local/bin"]
for path in paths:
cmake = shutil.which("cmake", path=path)
if cmake:
if cmake := shutil.which("cmake", path=path):
break

if not cmake:
Expand Down Expand Up @@ -47,8 +45,7 @@ def extract(archive: str | Path, out_path: str | Path):
directory to extract files and directories to
"""

archive = Path(archive).expanduser().resolve()
if not archive.is_file():
if not (archive := Path(archive).expanduser().resolve()).is_file():
raise FileNotFoundError(archive)

out_path = Path(out_path).expanduser().resolve()
Expand Down
3 changes: 1 addition & 2 deletions src/gemini3d/compare/out.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ def compare_output(

for i, t in enumerate(params["time"]):
st = f"UTsec {t}"
A = read.frame(new_dir, t)
if not A:
if not (A := read.frame(new_dir, t)):
raise FileNotFoundError(f"{new_dir} does not appear to contain data at {t}")
B = read.frame(ref_dir, t)

Expand Down
19 changes: 6 additions & 13 deletions src/gemini3d/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def executable(name: str, root: Path | None = None) -> Path:
if not name:
raise ValueError("executable name must be non-empty")

ep = Path(name).expanduser()
if ep.is_file():
if (ep := Path(name).expanduser()).is_file():
return ep

exe_paths = EXE_PATHS
Expand Down Expand Up @@ -133,8 +132,7 @@ def frame(simdir: Path, time: datetime) -> Path:
+ f"{time.microsecond:06d}"
)

fn = simdir / (stem + suffix)
if fn.is_file():
if (fn := simdir / (stem + suffix)).is_file():
return fn

# %% WORKAROUND for real32 file ticks. This will be removed when datetime-fortran is implemented
Expand Down Expand Up @@ -171,19 +169,14 @@ def grid(path: Path) -> Path:
def find_stem(path: Path, stem: str, suffix: str = ".h5") -> Path | None:
"""find file containing stem"""

path = Path(path).expanduser()

if path.is_file():
if (path := Path(path).expanduser()).is_file():
if stem in path.stem:
return path
else:
found = find_stem(path.parent, stem, path.suffix)
if found:
return found
elif found := find_stem(path.parent, stem, path.suffix):
return found
elif path.is_dir():
for p in (path, path / "inputs"):
f = p / (stem + suffix)
if f.is_file():
if (f := p / (stem + suffix)).is_file():
return f

return None
Expand Down
22 changes: 9 additions & 13 deletions src/gemini3d/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ def runner(pr: dict[str, typing.Any]) -> None:

# %% setup Efield if needed
if "E0dir" in p:
E0dir = out_dir / p["E0dir"]
if not E0dir.is_dir():
if not (out_dir / p["E0dir"]).is_dir():
model.setup(p["nml"], out_dir)

# %% setup precip if needed
if "precdir" in p:
precdir = out_dir / p["precdir"]
if not precdir.is_dir():
if not (out_dir / p["precdir"]).is_dir():
model.setup(p["nml"], out_dir)

# build checks
Expand Down Expand Up @@ -164,9 +162,10 @@ def memory_estimate(path: Path) -> int:

def check_compiler():
fc = os.environ.get("FC")
fc = shutil.which(fc) if fc else shutil.which("gfortran")
if not fc:
raise EnvironmentError("Cannot find Fortran compiler e.g. Gfortran")
if fc := shutil.which(fc if fc else "gfortran"):
return fc

raise EnvironmentError("Cannot find Fortran compiler e.g. Gfortran")


def check_mpiexec(mpiexec_name: str, gemexe: Path) -> str:
Expand All @@ -178,12 +177,10 @@ def check_mpiexec(mpiexec_name: str, gemexe: Path) -> str:
if not mpiexec_name:
mpiexec_name = "mpiexec"

mpi_root = os.environ.get("MPI_ROOT", None)
if mpi_root:
if mpi_root := os.environ.get("MPI_ROOT", None):
mpi_root += "/bin"

mpiexec = shutil.which(mpiexec_name, path=mpi_root)
if not mpiexec:
if not (mpiexec := shutil.which(mpiexec_name, path=mpi_root)):
raise FileNotFoundError(f"Cannot find mpiexec {mpiexec_name}")

ret = subprocess.run([mpiexec, "-help"], capture_output=True, text=True, timeout=5)
Expand All @@ -210,8 +207,7 @@ def check_mpiexec(mpiexec_name: str, gemexe: Path) -> str:


def check_outdir(out_dir: str | Path) -> Path:
out_dir = Path(out_dir).expanduser().resolve()
if out_dir.is_file():
if (out_dir := Path(out_dir).expanduser().resolve()).is_file():
raise NotADirectoryError(
f"please specify output DIRECTORY, you specified {out_dir}"
)
Expand Down
3 changes: 1 addition & 2 deletions src/gemini3d/linux_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def os_release() -> list[str]:
if sys.platform != "linux":
return []

fn = Path("/etc/os-release")
if not fn.is_file():
if not (fn := Path("/etc/os-release")).is_file():
if Path("/etc/redhat-release").is_file() or Path("/etc/centos-release").is_file():
return ["rhel"]
elif Path("/etc/debian_version").is_file():
Expand Down
4 changes: 1 addition & 3 deletions src/gemini3d/namelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ def _write_value(f, key: str, value: T.Any):
else:
raise TypeError(f"unsure how to handle {type(value)}")

file = file.expanduser().resolve()

if file.is_dir():
if (file := file.expanduser().resolve()).is_dir():
raise OSError(f"give a filename, not a directory {file}")

mode = "w" if overwrite else "a"
Expand Down
3 changes: 1 addition & 2 deletions src/gemini3d/patch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def filenames2times(indir: Path) -> list[datetime]:
get times from filenames
"""

indir = Path(indir).expanduser()
if not indir.is_dir():
if not (indir := Path(indir).expanduser()).is_dir():
raise NotADirectoryError(indir)

times = []
Expand Down
3 changes: 1 addition & 2 deletions src/gemini3d/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ def frame(
var = set(var)

# %% file or directory
path = Path(path).expanduser()
if path.is_dir():
if (path := Path(path).expanduser()).is_dir():
if time is None:
raise ValueError(
f"must specify time when giving directory {path} instead of file"
Expand Down
9 changes: 3 additions & 6 deletions src/gemini3d/tests/intg/test_grid_matlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,16 @@ def test_tilted_dipole():
pytest.skip("Matlab engine not available")

# find MatGemini
root = os.environ.get("MATGEMINI")
if not root:
if not (root := os.environ.get("MATGEMINI")):
# guess
root = Path(__file__).parents[5] / "mat_gemini"

root = Path(root).expanduser()
if not root.is_dir():
if not (root := Path(root).expanduser()).is_dir():
raise FileNotFoundError(
"Please set MATGEMINI environment variable with top-level mat_gemini directory."
f"\nMatGemini not found at {root}"
)
mg_setup = root / "setup.m"
if not mg_setup.is_file():
if not (mg_setup := root / "setup.m").is_file():
raise FileNotFoundError(str(mg_setup))

eng = mateng.start_matlab("-nojvm")
Expand Down
3 changes: 1 addition & 2 deletions src/gemini3d/tests/intg/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def test_model_setup(name, tmp_path, monkeypatch, helpers):

# patch eq_dir to use reference data
if "eq_dir" in params:
eq_dir = test_dir.parent / params["eq_dir"].name
if eq_dir.is_dir():
if (eq_dir := test_dir.parent / params["eq_dir"].name).is_dir():
print(f"Using {eq_dir} for equilibrium data")
params["eq_dir"] = eq_dir

Expand Down
9 changes: 3 additions & 6 deletions src/gemini3d/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,11 @@ def str2func(name: str, path: Path | None = None) -> T.Callable:
mod = importlib.import_module(func_name)
else:
# https://docs.python.org/3.10/library/importlib.html#importing-a-source-file-directly
mod_file = path / (name + ".py")
if not mod_file.is_file():
if not (mod_file := path / (name + ".py")).is_file():
raise FileNotFoundError(mod_file)
spec = importlib.util.spec_from_file_location(name, mod_file)
if spec is None:
if (spec := importlib.util.spec_from_file_location(name, mod_file)) is None:
raise ModuleNotFoundError(f"{name} not found in {mod_file}")
mod = importlib.util.module_from_spec(spec)
if mod is None:
if (mod := importlib.util.module_from_spec(spec)) is None:
raise ImportError(f"could not import {name} from {mod_file}")
spec.loader.exec_module(mod) # type: ignore

Expand Down
7 changes: 2 additions & 5 deletions src/gemini3d/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def git_download(path: Path, repo: str, tag: str | None = None):
Use Git to download code repo.
"""

git = shutil.which("git")

if not git:
if not (git := shutil.which("git")):
raise FileNotFoundError("Git not found.")

if not tag:
Expand Down Expand Up @@ -48,9 +46,8 @@ def git_download(path: Path, repo: str, tag: str | None = None):


def download_and_extract(test_name: str, data_dir: Path) -> Path:
ref_file = data_dir / "ref_data.json"

if not ref_file.is_file():
if not (ref_file := data_dir / "ref_data.json").is_file():
jmeta = json.loads(
(importlib.resources.files("gemini3d") / "libraries.json").read_text()
)
Expand Down
6 changes: 2 additions & 4 deletions src/gemini3d/write.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def grid(cfg: dict[str, T.Any], xg: dict[str, T.Any]) -> None:
grid values
"""

input_dir = cfg["indat_size"].parent
if input_dir.is_file():
if (input_dir := cfg["indat_size"].parent).is_file():
raise OSError(f"{input_dir} is a file instead of directory")

input_dir.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -129,8 +128,7 @@ def meta(fn: Path, git_meta: dict[str, str], cfg: dict[str, T.Any]) -> None:
if "eq_dir" in cfg:
# JSON does not allow unescaped backslash
jm["equilibrium"] = {"eq_dir": cfg["eq_dir"].as_posix()}
hf = cfg["eq_dir"] / "sha256sum.txt"
if hf.is_file():
if (hf := cfg["eq_dir"] / "sha256sum.txt").is_file():
jm["equilibrium"]["sha256"] = hf.read_text().strip()

js = json.dumps(jm, sort_keys=True, indent=2)
Expand Down

0 comments on commit 1f2930d

Please sign in to comment.