Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced os.path.exists() and os.path.join() #858

Merged
merged 7 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/quacc/calculators/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __init__(
# Get user-defined preset parameters for the calculator
if preset:
calc_preset = load_vasp_yaml_calc(
os.path.join(SETTINGS.VASP_PRESET_DIR, preset)
Path(SETTINGS.VASP_PRESET_DIR, preset)
)["inputs"]
else:
calc_preset = {}
Expand All @@ -152,7 +152,7 @@ def __init__(
and self.user_calc_params["setups"] not in ase_setups.setups_defaults
):
self.user_calc_params["setups"] = load_vasp_yaml_calc(
os.path.join(SETTINGS.VASP_PRESET_DIR, self.user_calc_params["setups"])
Path(SETTINGS.VASP_PRESET_DIR, self.user_calc_params["setups"])
)["inputs"]["setups"]

# If the preset has auto_kpts but the user explicitly requests kpts,
Expand Down
8 changes: 4 additions & 4 deletions src/quacc/schemas/vasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,8 @@ def bader_runner(path: str | None = None, scratch_dir: str | None = None) -> dic
# Make sure files are present
relevant_files = ["AECCAR0", "AECCAR2", "CHGCAR", "POTCAR"]
for f in relevant_files:
if not os.path.exists(os.path.join(path, f)) and not os.path.exists(
os.path.join(path, f"{f}.gz")
if not Path.exists(Path(path, f)) and not Path.exists(
Andrew-S-Rosen marked this conversation as resolved.
Show resolved Hide resolved
Path(path, f"{f}.gz")
):
msg = f"Could not find {f} in {path}."
raise FileNotFoundError(msg)
Expand Down Expand Up @@ -422,8 +422,8 @@ def chargemol_runner(
# Make sure files are present
relevant_files = ["AECCAR0", "AECCAR2", "CHGCAR", "POTCAR"]
for f in relevant_files:
if not os.path.exists(os.path.join(path, f)) and not os.path.exists(
os.path.join(path, f"{f}.gz")
if not Path.exists(Path(path, f)) and not Path.exists(
Path(path, f"{f}.gz")
):
msg = f"Could not find {f} in {path}."
raise FileNotFoundError(msg)
Expand Down
12 changes: 6 additions & 6 deletions src/quacc/utils/calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_calc(
# Note: We have to be careful to make sure we don't lose the converged
# magnetic moments, if present. That's why we simply update the
# positions and cell in-place.
atoms_new = read(zpath(os.path.join(tmpdir, geom_file)))
atoms_new = read(zpath(Path(tmpdir, geom_file)))
if isinstance(atoms_new, list):
atoms_new = atoms_new[-1]

Expand Down Expand Up @@ -150,7 +150,7 @@ def run_ase_opt(
msg = "Quacc does not support setting the `trajectory` kwarg."
raise ValueError(msg)

traj_filename = os.path.join(tmpdir, "opt.traj")
traj_filename = Path(tmpdir, "opt.traj")
optimizer_kwargs["trajectory"] = Trajectory(traj_filename, "w", atoms=atoms)

# Define optimizer class
Expand Down Expand Up @@ -209,9 +209,9 @@ def run_ase_vib(
atoms, tmpdir, job_results_dir = _calc_setup(atoms, copy_files=copy_files)

# Run calculation
vib = Vibrations(atoms, name=os.path.join(tmpdir, "vib"), **vib_kwargs)
vib = Vibrations(atoms, name=Path(tmpdir, "vib"), **vib_kwargs)
vib.run()
vib.summary(log=os.path.join(tmpdir, "vib_summary.log"))
vib.summary(log=Path(tmpdir, "vib_summary.log").as_posix())
Andrew-S-Rosen marked this conversation as resolved.
Show resolved Hide resolved

# Perform cleanup operations
_calc_cleanup(tmpdir, job_results_dir)
Expand Down Expand Up @@ -265,7 +265,7 @@ def _calc_setup(
tmpdir = Path.resolve(Path(mkdtemp(prefix="quacc-tmp-", dir=SETTINGS.SCRATCH_DIR)))

# Create a symlink (if not on Windows) to the tmpdir in the results_dir
symlink = os.path.join(job_results_dir, f"{tmpdir.name}-symlink")
symlink = Path(job_results_dir, f"{tmpdir.name}-symlink")
if os.name != "nt":
if os.path.islink(symlink):
os.unlink(symlink)
Expand Down Expand Up @@ -311,7 +311,7 @@ def _calc_cleanup(tmpdir: str, job_results_dir: str) -> None:
copy_r(tmpdir, job_results_dir)

# Remove symlink to tmpdir
symlink = os.path.join(job_results_dir, f"{os.path.basename(tmpdir)}-symlink")
symlink = Path(job_results_dir, f"{os.path.basename(tmpdir)}-symlink")
if os.path.islink(symlink):
os.remove(symlink)

Expand Down
10 changes: 5 additions & 5 deletions src/quacc/utils/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ def copy_decompress(source_files: list[str], destination: str) -> None:
"""
for f in source_files:
z_path = zpath(f)
if os.path.exists(z_path):
if Path.exists(Path(z_path)):
Andrew-S-Rosen marked this conversation as resolved.
Show resolved Hide resolved
z_file = os.path.basename(z_path)
copy(z_path, os.path.join(destination, z_file))
decompress_file(os.path.join(destination, z_file))
copy(z_path, Path(destination, z_file))
decompress_file(Path(destination, z_file).as_posix())
Andrew-S-Rosen marked this conversation as resolved.
Show resolved Hide resolved
else:
warnings.warn(f"Cannot find file: {z_path}", UserWarning)


def make_unique_dir(base_path: str | None = None) -> str:
def make_unique_dir(base_path: str | None = None) -> str | Path:
"""
Make a directory with a unique name. Uses the same format as Jobflow.

Expand All @@ -86,7 +86,7 @@ def make_unique_dir(base_path: str | None = None) -> str:
time_now = datetime.utcnow().strftime("%Y-%m-%d-%H-%M-%S-%f")
job_dir = f"quacc-{time_now}-{randint(10000, 99999)}"
if base_path:
job_dir = os.path.join(base_path, job_dir)
job_dir = Path(base_path, job_dir)
os.makedirs(job_dir)

return job_dir
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ def test_make_unique_dir(tmpdir):

jobdir = make_unique_dir(base_path="tmp_dir")
assert os.path.exists("tmp_dir")
assert "tmp_dir" in jobdir
assert "tmp_dir" in str(jobdir)
assert os.path.exists(jobdir)