foamlib 0.2.6__tar.gz → 0.2.7__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.2.6
3
+ Version: 0.2.7
4
4
  Summary: A Python interface for interacting with OpenFOAM
5
5
  Author-email: "Gabriel S. Gerlero" <ggerlero@cimec.unl.edu.ar>
6
6
  Project-URL: Homepage, https://github.com/gerlero/foamlib
@@ -1,6 +1,6 @@
1
1
  """A Python interface for interacting with OpenFOAM."""
2
2
 
3
- __version__ = "0.2.6"
3
+ __version__ = "0.2.7"
4
4
 
5
5
  from ._cases import AsyncFoamCase, FoamCase, FoamCaseBase
6
6
  from ._dictionaries import FoamDictionaryBase, FoamFieldFile, FoamFile
@@ -4,9 +4,15 @@ from dataclasses import dataclass
4
4
  from typing import Dict, NamedTuple, Optional, Union
5
5
 
6
6
  if sys.version_info >= (3, 9):
7
- from collections.abc import Sequence
7
+ from collections.abc import Mapping, Sequence
8
8
  else:
9
- from typing import Sequence
9
+ from typing import Mapping, Sequence
10
+
11
+ try:
12
+ import numpy as np
13
+ from numpy.typing import NDArray
14
+ except ModuleNotFoundError:
15
+ pass
10
16
 
11
17
 
12
18
  class FoamDictionaryBase:
@@ -39,9 +45,13 @@ class FoamDictionaryBase:
39
45
  A value that can be stored in an OpenFOAM dictionary.
40
46
  """
41
47
 
42
- _Dict = Dict[str, Union["FoamDictionaryBase.Value", "_Dict"]]
48
+ _Dict = Dict[str, Union["Value", "_Dict"]]
43
49
 
44
50
  @abstractmethod
45
51
  def as_dict(self) -> _Dict:
46
52
  """Return a nested dict representation of the dictionary."""
47
53
  raise NotImplementedError
54
+
55
+ _SetValue = Union[Value, "NDArray[np.generic]"]
56
+
57
+ _SetMapping = Mapping[str, Union["_SetValue", "_SetMapping"]]
@@ -1,9 +1,11 @@
1
1
  import sys
2
2
  from pathlib import Path
3
+ from types import TracebackType
3
4
  from typing import (
4
5
  Any,
5
6
  Optional,
6
7
  Tuple,
8
+ Type,
7
9
  Union,
8
10
  cast,
9
11
  )
@@ -48,7 +50,12 @@ class _FoamFileBase:
48
50
  self.__defer_io += 1
49
51
  return self
50
52
 
51
- def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
53
+ def __exit__(
54
+ self,
55
+ exc_type: Optional[Type[BaseException]],
56
+ exc_val: Optional[BaseException],
57
+ exc_tb: Optional[TracebackType],
58
+ ) -> None:
52
59
  self.__defer_io -= 1
53
60
  if self.__defer_io == 0 and self.__dirty:
54
61
  assert self.__contents is not None
@@ -124,7 +131,11 @@ class FoamFile(
124
131
  assume_dimensions=assume_dimensions,
125
132
  )
126
133
 
127
- def __setitem__(self, keyword: str, value: Any) -> None:
134
+ def __setitem__(
135
+ self,
136
+ keyword: str,
137
+ value: Union["FoamFile._SetValue", "FoamFile._SetMapping"],
138
+ ) -> None:
128
139
  self._setitem(keyword, value)
129
140
 
130
141
  def __delitem__(self, keyword: str) -> None:
@@ -180,7 +191,7 @@ class FoamFile(
180
191
  def _setitem(
181
192
  self,
182
193
  keywords: Union[str, Tuple[str, ...]],
183
- value: Any,
194
+ value: Union["FoamFile._SetValue", "FoamFile._SetMapping"],
184
195
  *,
185
196
  assume_field: bool = False,
186
197
  assume_dimensions: bool = False,
@@ -210,7 +221,11 @@ class FoamFile(
210
221
  f"{contents[:start]}\n{serialize_entry(keywords[-1], value, assume_field=assume_field, assume_dimensions=assume_dimensions)}\n{contents[end:]}"
211
222
  )
212
223
 
213
- def __setitem__(self, keywords: Union[str, Tuple[str, ...]], value: Any) -> None:
224
+ def __setitem__(
225
+ self,
226
+ keywords: Union[str, Tuple[str, ...]],
227
+ value: Union["FoamFile._SetValue", "FoamFile._SetMapping"],
228
+ ) -> None:
214
229
  self._setitem(keywords, value)
215
230
 
216
231
  def __delitem__(self, keywords: Union[str, Tuple[str, ...]]) -> None:
@@ -274,7 +289,11 @@ class FoamFieldFile(FoamFile):
274
289
  class BoundaryDictionary(FoamFile.Dictionary):
275
290
  """An OpenFOAM dictionary representing a boundary condition as a mutable mapping."""
276
291
 
277
- def __setitem__(self, key: str, value: Any) -> None:
292
+ def __setitem__(
293
+ self,
294
+ key: str,
295
+ value: Union[FoamFile._SetValue, FoamFile._SetMapping],
296
+ ) -> None:
278
297
  if key == "value":
279
298
  self._setitem(key, value, assume_field=True)
280
299
  else:
@@ -1,6 +1,6 @@
1
1
  import sys
2
2
  from contextlib import suppress
3
- from typing import Any
3
+ from typing import Union
4
4
 
5
5
  if sys.version_info >= (3, 9):
6
6
  from collections.abc import Mapping
@@ -11,7 +11,9 @@ from .._util import is_sequence
11
11
  from ._base import FoamDictionaryBase
12
12
 
13
13
 
14
- def _serialize_switch(value: Any) -> str:
14
+ def _serialize_switch(
15
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
16
+ ) -> str:
15
17
  if value is True:
16
18
  return "yes"
17
19
  elif value is False:
@@ -20,14 +22,18 @@ def _serialize_switch(value: Any) -> str:
20
22
  raise TypeError(f"Not a bool: {type(value)}")
21
23
 
22
24
 
23
- def _serialize_list(value: Any) -> str:
25
+ def _serialize_list(
26
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
27
+ ) -> str:
24
28
  if is_sequence(value):
25
29
  return f"({' '.join(_serialize_value(v) for v in value)})"
26
30
  else:
27
31
  raise TypeError(f"Not a valid sequence: {type(value)}")
28
32
 
29
33
 
30
- def _serialize_field(value: Any) -> str:
34
+ def _serialize_field(
35
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
36
+ ) -> str:
31
37
  if is_sequence(value):
32
38
  try:
33
39
  s = _serialize_list(value)
@@ -54,14 +60,18 @@ def _serialize_field(value: Any) -> str:
54
60
  return f"uniform {value}"
55
61
 
56
62
 
57
- def _serialize_dimensions(value: Any) -> str:
63
+ def _serialize_dimensions(
64
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
65
+ ) -> str:
58
66
  if is_sequence(value) and len(value) == 7:
59
67
  return f"[{' '.join(str(v) for v in value)}]"
60
68
  else:
61
69
  raise TypeError(f"Not a valid dimension set: {type(value)}")
62
70
 
63
71
 
64
- def _serialize_dimensioned(value: Any) -> str:
72
+ def _serialize_dimensioned(
73
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
74
+ ) -> str:
65
75
  if isinstance(value, FoamDictionaryBase.Dimensioned):
66
76
  if value.name is not None:
67
77
  return f"{value.name} {_serialize_dimensions(value.dimensions)} {_serialize_value(value.value)}"
@@ -72,7 +82,10 @@ def _serialize_dimensioned(value: Any) -> str:
72
82
 
73
83
 
74
84
  def _serialize_value(
75
- value: Any, *, assume_field: bool = False, assume_dimensions: bool = False
85
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
86
+ *,
87
+ assume_field: bool = False,
88
+ assume_dimensions: bool = False,
76
89
  ) -> str:
77
90
  if isinstance(value, FoamDictionaryBase.DimensionSet) or assume_dimensions:
78
91
  with suppress(TypeError):
@@ -94,7 +107,9 @@ def _serialize_value(
94
107
  return str(value)
95
108
 
96
109
 
97
- def _serialize_dictionary(value: Any) -> str:
110
+ def _serialize_dictionary(
111
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
112
+ ) -> str:
98
113
  if isinstance(value, Mapping):
99
114
  return "\n".join(serialize_entry(k, v) for k, v in value.items())
100
115
  else:
@@ -103,7 +118,7 @@ def _serialize_dictionary(value: Any) -> str:
103
118
 
104
119
  def serialize_entry(
105
120
  keyword: str,
106
- value: Any,
121
+ value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
107
122
  *,
108
123
  assume_field: bool = False,
109
124
  assume_dimensions: bool = False,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.2.6
3
+ Version: 0.2.7
4
4
  Summary: A Python interface for interacting with OpenFOAM
5
5
  Author-email: "Gabriel S. Gerlero" <ggerlero@cimec.unl.edu.ar>
6
6
  Project-URL: Homepage, https://github.com/gerlero/foamlib
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes