Skip to content

Commit

Permalink
Merge pull request #160 from gerlero/delete
Browse files Browse the repository at this point in the history
Add __delitem__ methods
  • Loading branch information
gerlero authored Aug 23, 2024
2 parents 6a99c19 + 331781f commit ed985c9
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions foamlib/_cases/_base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
import sys
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -87,6 +88,12 @@ def __iter__(self) -> Iterator[FoamFieldFile]:
def __len__(self) -> int:
return len(list(iter(self)))

def __delitem__(self, key: str) -> None:
if (self.path / f"{key}.gz").is_file() and not (self.path / key).is_file():
(self.path / f"{key}.gz").unlink()
else:
(self.path / key).unlink()

def __fspath__(self) -> str:
return str(self.path)

Expand Down Expand Up @@ -165,6 +172,9 @@ def _clean_paths(self) -> Set[Path]:

return paths

def __delitem__(self, key: Union[int, float, str]) -> None:
shutil.rmtree(self[key].path)

def _clone_ignore(
self,
) -> Callable[[Union[Path, str], Collection[str]], Collection[str]]:
Expand Down

0 comments on commit ed985c9

Please sign in to comment.