foamlib 0.6.10__py3-none-any.whl → 0.6.12__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
foamlib/_cases/_sync.py CHANGED
@@ -1,7 +1,8 @@
1
+ from __future__ import annotations
2
+
1
3
  import shutil
2
4
  import sys
3
- from types import TracebackType
4
- from typing import TYPE_CHECKING, Any, Callable, Optional, Type, Union, overload
5
+ from typing import TYPE_CHECKING, Any, Callable, overload
5
6
 
6
7
  if sys.version_info >= (3, 9):
7
8
  from collections.abc import Collection, Sequence
@@ -13,7 +14,6 @@ if sys.version_info >= (3, 11):
13
14
  else:
14
15
  from typing_extensions import Self
15
16
 
16
- from .._files import FoamFieldFile
17
17
  from ._base import FoamCaseBase
18
18
  from ._run import FoamCaseRunBase
19
19
  from ._subprocess import run_sync
@@ -21,6 +21,9 @@ from ._util import ValuedGenerator
21
21
 
22
22
  if TYPE_CHECKING:
23
23
  import os
24
+ from types import TracebackType
25
+
26
+ from .._files import FoamFieldFile
24
27
 
25
28
 
26
29
  class FoamCase(FoamCaseRunBase):
@@ -36,7 +39,7 @@ class FoamCase(FoamCaseRunBase):
36
39
 
37
40
  class TimeDirectory(FoamCaseRunBase.TimeDirectory):
38
41
  @property
39
- def _case(self) -> "FoamCase":
42
+ def _case(self) -> FoamCase:
40
43
  return FoamCase(self.path.parent)
41
44
 
42
45
  def cell_centers(self) -> FoamFieldFile:
@@ -50,7 +53,7 @@ class FoamCase(FoamCaseRunBase):
50
53
 
51
54
  @staticmethod
52
55
  def _run(
53
- cmd: Union[Sequence[Union[str, "os.PathLike[str]"]], str],
56
+ cmd: Sequence[str | os.PathLike[str]] | str,
54
57
  *,
55
58
  cpus: int,
56
59
  **kwargs: Any,
@@ -58,34 +61,29 @@ class FoamCase(FoamCaseRunBase):
58
61
  run_sync(cmd, **kwargs)
59
62
 
60
63
  @staticmethod
61
- def _rmtree(
62
- path: Union["os.PathLike[str]", str], *, ignore_errors: bool = False
63
- ) -> None:
64
+ def _rmtree(path: os.PathLike[str] | str, *, ignore_errors: bool = False) -> None:
64
65
  shutil.rmtree(path, ignore_errors=ignore_errors)
65
66
 
66
67
  @staticmethod
67
68
  def _copytree(
68
- src: Union["os.PathLike[str]", str],
69
- dest: Union["os.PathLike[str]", str],
69
+ src: os.PathLike[str] | str,
70
+ dest: os.PathLike[str] | str,
70
71
  *,
71
72
  symlinks: bool = False,
72
- ignore: Optional[
73
- Callable[[Union["os.PathLike[str]", str], Collection[str]], Collection[str]]
74
- ] = None,
73
+ ignore: Callable[[os.PathLike[str] | str, Collection[str]], Collection[str]]
74
+ | None = None,
75
75
  ) -> None:
76
76
  shutil.copytree(src, dest, symlinks=symlinks, ignore=ignore)
77
77
 
78
78
  @overload
79
- def __getitem__(
80
- self, index: Union[int, float, str]
81
- ) -> "FoamCase.TimeDirectory": ...
79
+ def __getitem__(self, index: int | float | str) -> FoamCase.TimeDirectory: ...
82
80
 
83
81
  @overload
84
- def __getitem__(self, index: slice) -> Sequence["FoamCase.TimeDirectory"]: ...
82
+ def __getitem__(self, index: slice) -> Sequence[FoamCase.TimeDirectory]: ...
85
83
 
86
84
  def __getitem__(
87
- self, index: Union[int, slice, float, str]
88
- ) -> Union["FoamCase.TimeDirectory", Sequence["FoamCase.TimeDirectory"]]:
85
+ self, index: int | slice | float | str
86
+ ) -> FoamCase.TimeDirectory | Sequence[FoamCase.TimeDirectory]:
89
87
  ret = super().__getitem__(index)
90
88
  if isinstance(ret, FoamCaseBase.TimeDirectory):
91
89
  return FoamCase.TimeDirectory(ret)
@@ -96,9 +94,9 @@ class FoamCase(FoamCaseRunBase):
96
94
 
97
95
  def __exit__(
98
96
  self,
99
- exc_type: Optional[Type[BaseException]],
100
- exc_val: Optional[BaseException],
101
- exc_tb: Optional[TracebackType],
97
+ exc_type: type[BaseException] | None,
98
+ exc_val: BaseException | None,
99
+ exc_tb: TracebackType | None,
102
100
  ) -> None:
103
101
  self._rmtree(self.path)
104
102
 
@@ -117,10 +115,10 @@ class FoamCase(FoamCaseRunBase):
117
115
 
118
116
  def run(
119
117
  self,
120
- cmd: Optional[Union[Sequence[Union[str, "os.PathLike[str]"]], str]] = None,
118
+ cmd: Sequence[str | os.PathLike[str]] | str | None = None,
121
119
  *,
122
- parallel: Optional[bool] = None,
123
- cpus: Optional[int] = None,
120
+ parallel: bool | None = None,
121
+ cpus: int | None = None,
124
122
  check: bool = True,
125
123
  log: bool = True,
126
124
  ) -> None:
@@ -158,7 +156,7 @@ class FoamCase(FoamCaseRunBase):
158
156
  for _ in self._restore_0_dir_calls():
159
157
  pass
160
158
 
161
- def copy(self, dst: Optional[Union["os.PathLike[str]", str]] = None) -> Self:
159
+ def copy(self, dst: os.PathLike[str] | str | None = None) -> Self:
162
160
  """
163
161
  Make a copy of this case.
164
162
 
@@ -173,7 +171,7 @@ class FoamCase(FoamCaseRunBase):
173
171
 
174
172
  return calls.value
175
173
 
176
- def clone(self, dst: Optional[Union["os.PathLike[str]", str]] = None) -> Self:
174
+ def clone(self, dst: os.PathLike[str] | str | None = None) -> Self:
177
175
  """
178
176
  Clone this case (make a clean copy).
179
177
 
foamlib/_cases/_util.py CHANGED
@@ -1,16 +1,19 @@
1
+ from __future__ import annotations
2
+
1
3
  import functools
2
4
  import sys
3
- from types import TracebackType
4
5
  from typing import (
6
+ TYPE_CHECKING,
5
7
  Any,
6
8
  AsyncContextManager,
7
9
  Callable,
8
10
  Generic,
9
- Optional,
10
- Type,
11
11
  TypeVar,
12
12
  )
13
13
 
14
+ if TYPE_CHECKING:
15
+ from types import TracebackType
16
+
14
17
  if sys.version_info >= (3, 9):
15
18
  from collections.abc import Generator
16
19
  else:
@@ -23,7 +26,7 @@ R = TypeVar("R")
23
26
 
24
27
 
25
28
  class ValuedGenerator(Generic[Y, S, R]):
26
- def __init__(self, generator: Generator[Y, S, R]):
29
+ def __init__(self, generator: Generator[Y, S, R]) -> None:
27
30
  self._generator = generator
28
31
 
29
32
  def __iter__(self) -> Generator[Y, S, R]:
@@ -32,7 +35,7 @@ class ValuedGenerator(Generic[Y, S, R]):
32
35
 
33
36
 
34
37
  class _AwaitableAsyncContextManager(Generic[R]):
35
- def __init__(self, cm: "AsyncContextManager[R]"):
38
+ def __init__(self, cm: AsyncContextManager[R]) -> None:
36
39
  self._cm = cm
37
40
 
38
41
  def __await__(self) -> Generator[Any, Any, R]:
@@ -43,15 +46,15 @@ class _AwaitableAsyncContextManager(Generic[R]):
43
46
 
44
47
  async def __aexit__(
45
48
  self,
46
- exc_type: Optional[Type[BaseException]],
47
- exc_val: Optional[BaseException],
48
- exc_tb: Optional[TracebackType],
49
- ) -> Optional[bool]:
49
+ exc_type: type[BaseException] | None,
50
+ exc_val: BaseException | None,
51
+ exc_tb: TracebackType | None,
52
+ ) -> bool | None:
50
53
  return await self._cm.__aexit__(exc_type, exc_val, exc_tb)
51
54
 
52
55
 
53
56
  def awaitableasynccontextmanager(
54
- cm: Callable[..., "AsyncContextManager[R]"],
57
+ cm: Callable[..., AsyncContextManager[R]],
55
58
  ) -> Callable[..., _AwaitableAsyncContextManager[R]]:
56
59
  @functools.wraps(cm)
57
60
  def f(*args: Any, **kwargs: Any) -> _AwaitableAsyncContextManager[R]:
foamlib/_files/_base.py CHANGED
@@ -1,3 +1,5 @@
1
+ from __future__ import annotations
2
+
1
3
  import sys
2
4
  from dataclasses import dataclass
3
5
  from typing import TYPE_CHECKING, Dict, NamedTuple, Optional, Tuple, Union
@@ -6,35 +8,46 @@ if TYPE_CHECKING:
6
8
  import numpy as np
7
9
 
8
10
  if sys.version_info >= (3, 9):
9
- from collections.abc import Mapping, Sequence
11
+ from collections.abc import Mapping, MutableMapping, Sequence
10
12
  else:
11
- from typing import Mapping, Sequence
13
+ from typing import Mapping, MutableMapping, Sequence
12
14
 
13
15
 
14
16
  class FoamFileBase:
15
17
  class DimensionSet(NamedTuple):
16
- mass: Union[int, float] = 0
17
- length: Union[int, float] = 0
18
- time: Union[int, float] = 0
19
- temperature: Union[int, float] = 0
20
- moles: Union[int, float] = 0
21
- current: Union[int, float] = 0
22
- luminous_intensity: Union[int, float] = 0
18
+ mass: float = 0
19
+ length: float = 0
20
+ time: float = 0
21
+ temperature: float = 0
22
+ moles: float = 0
23
+ current: float = 0
24
+ luminous_intensity: float = 0
23
25
 
24
26
  def __repr__(self) -> str:
25
27
  return f"{type(self).__qualname__}({', '.join(f'{n}={v}' for n, v in zip(self._fields, self) if v != 0)})"
26
28
 
29
+ _Tensor = Union[
30
+ float,
31
+ Sequence[float],
32
+ "np.ndarray[Tuple[()], np.dtype[np.generic]]",
33
+ "np.ndarray[Tuple[int], np.dtype[np.generic]]",
34
+ ]
35
+
27
36
  @dataclass
28
37
  class Dimensioned:
29
- value: Union[int, float, Sequence[Union[int, float]]] = 0
30
- dimensions: Union["FoamFileBase.DimensionSet", Sequence[Union[int, float]]] = ()
31
- name: Optional[str] = None
38
+ value: FoamFileBase._Tensor = 0
39
+ dimensions: FoamFileBase.DimensionSet | Sequence[float] = ()
40
+ name: str | None = None
32
41
 
33
42
  def __post_init__(self) -> None:
34
43
  if not isinstance(self.dimensions, FoamFileBase.DimensionSet):
35
44
  self.dimensions = FoamFileBase.DimensionSet(*self.dimensions)
36
45
 
37
- Data = Union[
46
+ _Field = Union[
47
+ _Tensor, Sequence[_Tensor], "np.ndarray[Tuple[int, int], np.dtype[np.generic]]"
48
+ ]
49
+
50
+ _DataEntry = Union[
38
51
  str,
39
52
  int,
40
53
  float,
@@ -42,25 +55,22 @@ class FoamFileBase:
42
55
  Dimensioned,
43
56
  DimensionSet,
44
57
  Sequence["Data"],
58
+ _Tensor,
59
+ _Field,
60
+ ]
61
+
62
+ Data = Union[
63
+ _DataEntry,
45
64
  Mapping[str, "Data"],
46
65
  ]
47
66
  """
48
67
  A value that can be stored in an OpenFOAM file.
49
68
  """
50
69
 
70
+ _MutableData = Union[
71
+ _DataEntry,
72
+ MutableMapping[str, "_MutableData"],
73
+ ]
74
+
51
75
  _Dict = Dict[str, Union["Data", "_Dict"]]
52
76
  _File = Dict[Optional[str], Union["Data", "_Dict"]]
53
-
54
- _SetData = Union[
55
- str,
56
- int,
57
- float,
58
- bool,
59
- Dimensioned,
60
- DimensionSet,
61
- Sequence["_SetData"],
62
- Mapping[str, "_SetData"],
63
- "np.ndarray[Tuple[()], np.dtype[np.generic]]",
64
- "np.ndarray[Tuple[int], np.dtype[np.generic]]",
65
- "np.ndarray[Tuple[int, int], np.dtype[np.generic]]",
66
- ]