Skip to content

Commit

Permalink
Merge pull request #157 from gerlero/files
Browse files Browse the repository at this point in the history
Hide FoamFile header from __iter__
  • Loading branch information
gerlero authored Aug 22, 2024
2 parents 47e5076 + c161a9d commit 60d5823
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
12 changes: 9 additions & 3 deletions foamlib/_files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __setitem__(
elif not isinstance(keywords, tuple):
keywords = (keywords,)

if not self and keywords[0] != "FoamFile":
if not self and "FoamFile" not in self and keywords[0] != "FoamFile":
self.header = {
"version": 2.0,
"format": "ascii",
Expand Down Expand Up @@ -278,7 +278,11 @@ def __delitem__(self, keywords: Optional[Union[str, Tuple[str, ...]]]) -> None:
def _iter(self, keywords: Tuple[str, ...] = ()) -> Iterator[Optional[str]]:
_, parsed = self._read()

yield from (k[-1] if k else None for k in parsed if k[:-1] == keywords)
yield from (
k[-1] if k else None
for k in parsed
if k != ("FoamFile",) and k[:-1] == keywords
)

def __iter__(self) -> Iterator[Optional[str]]:
return self._iter()
Expand Down Expand Up @@ -310,7 +314,9 @@ def __fspath__(self) -> str:
def as_dict(self) -> FoamFileBase._Dict:
"""Return a nested dict representation of the file."""
_, parsed = self._read()
return parsed.as_dict()
d = parsed.as_dict()
del d["FoamFile"]
return d


class FoamFieldFile(FoamFile):
Expand Down
7 changes: 4 additions & 3 deletions tests/test_files/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ def test_write_read(tmp_path: Path) -> None:

d["key"] = "value"
assert d["key"] == "value"
assert len(d) == 2
assert len(d) == 1
assert "key" in d
assert list(d) == ["FoamFile", "key"]
assert list(d) == ["key"]
assert "FoamFile" in d
del d["key"]
assert len(d) == 1
assert not d
assert "key" not in d
with pytest.raises(KeyError):
del d["key"]
Expand Down

0 comments on commit 60d5823

Please sign in to comment.