Skip to content

Commit

Permalink
Fix VaspDir.files.
Browse files Browse the repository at this point in the history
  • Loading branch information
shyuep committed Oct 28, 2024
1 parent e9cd97e commit 22b6a7f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/pymatgen/io/vasp/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5807,7 +5807,10 @@ def reset(self):
changed.
"""
# Note that py3.12 has Path.walk(). But we need to use os.walk to ensure backwards compatibility for now.
self.files = [str(Path(d) / f).lstrip(str(self.path)) for d, _, fnames in os.walk(self.path) for f in fnames]
self.files = [
str(Path(d) / f)[len(str(self.path)) + 1 :] for d, _, fnames in os.walk(self.path) for f in fnames
]

self._parsed_files: dict[str, Any] = {}

def __contains__(self, item):
Expand Down Expand Up @@ -5851,3 +5854,6 @@ def get_files_by_name(self, name: str) -> dict[str, Any]:
{filename: object from VaspDir[filename]}
"""
return {f: self[f] for f in self.files if name in f}

def __repr__(self):
return f"VaspDir({self.path})"
2 changes: 2 additions & 0 deletions tests/io/vasp/test_outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2194,6 +2194,8 @@ def test_getitem(self):

d = VaspDir(f"{TEST_FILES_DIR}/io/vasp/fixtures/scan_relaxation")
assert len(d) == 2
assert "vasprun.xml.gz" in d.files
assert "OUTCAR" in d
assert d["vasprun.xml.gz"].incar["METAGGA"] == "R2scan"

with pytest.raises(ValueError, match="hello not found"):
Expand Down

0 comments on commit 22b6a7f

Please sign in to comment.