foamlib 0.2.7__tar.gz → 0.2.9__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.7
3
+ Version: 0.2.9
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.7"
3
+ __version__ = "0.2.9"
4
4
 
5
5
  from ._cases import AsyncFoamCase, FoamCase, FoamCaseBase
6
6
  from ._dictionaries import FoamDictionaryBase, FoamFieldFile, FoamFile
@@ -40,7 +40,16 @@ class FoamDictionaryBase:
40
40
  if not isinstance(self.dimensions, FoamDictionaryBase.DimensionSet):
41
41
  self.dimensions = FoamDictionaryBase.DimensionSet(*self.dimensions)
42
42
 
43
- Value = Union[str, int, float, bool, Dimensioned, DimensionSet, Sequence["Value"]]
43
+ Value = Union[
44
+ str,
45
+ int,
46
+ float,
47
+ bool,
48
+ Dimensioned,
49
+ DimensionSet,
50
+ Sequence["Value"],
51
+ Mapping[str, "Value"],
52
+ ]
44
53
  """
45
54
  A value that can be stored in an OpenFOAM dictionary.
46
55
  """
@@ -53,5 +62,3 @@ class FoamDictionaryBase:
53
62
  raise NotImplementedError
54
63
 
55
64
  _SetValue = Union[Value, "NDArray[np.generic]"]
56
-
57
- _SetMapping = Mapping[str, Union["_SetValue", "_SetMapping"]]
@@ -1,4 +1,5 @@
1
1
  import sys
2
+ from copy import deepcopy
2
3
  from pathlib import Path
3
4
  from types import TracebackType
4
5
  from typing import (
@@ -72,9 +73,12 @@ class _FoamFileBase:
72
73
  assert self.__contents is not None
73
74
 
74
75
  if self.__parsed is None:
75
- self.__parsed = parse(self.__contents)
76
+ parsed = parse(self.__contents)
77
+ self.__parsed = parsed
78
+ else:
79
+ parsed = deepcopy(self.__parsed)
76
80
 
77
- return self.__contents, self.__parsed
81
+ return self.__contents, parsed
78
82
 
79
83
  def _write(self, contents: str) -> None:
80
84
  self.__contents = contents
@@ -131,11 +135,7 @@ class FoamFile(
131
135
  assume_dimensions=assume_dimensions,
132
136
  )
133
137
 
134
- def __setitem__(
135
- self,
136
- keyword: str,
137
- value: Union["FoamFile._SetValue", "FoamFile._SetMapping"],
138
- ) -> None:
138
+ def __setitem__(self, keyword: str, value: "FoamFile._SetValue") -> None:
139
139
  self._setitem(keyword, value)
140
140
 
141
141
  def __delitem__(self, keyword: str) -> None:
@@ -191,7 +191,7 @@ class FoamFile(
191
191
  def _setitem(
192
192
  self,
193
193
  keywords: Union[str, Tuple[str, ...]],
194
- value: Union["FoamFile._SetValue", "FoamFile._SetMapping"],
194
+ value: "FoamFile._SetValue",
195
195
  *,
196
196
  assume_field: bool = False,
197
197
  assume_dimensions: bool = False,
@@ -224,7 +224,7 @@ class FoamFile(
224
224
  def __setitem__(
225
225
  self,
226
226
  keywords: Union[str, Tuple[str, ...]],
227
- value: Union["FoamFile._SetValue", "FoamFile._SetMapping"],
227
+ value: "FoamFile._SetValue",
228
228
  ) -> None:
229
229
  self._setitem(keywords, value)
230
230
 
@@ -284,7 +284,11 @@ class FoamFieldFile(FoamFile):
284
284
 
285
285
  class BoundariesDictionary(FoamFile.Dictionary):
286
286
  def __getitem__(self, keyword: str) -> "FoamFieldFile.BoundaryDictionary":
287
- return cast(FoamFieldFile.BoundaryDictionary, super().__getitem__(keyword))
287
+ value = super().__getitem__(keyword)
288
+ if not isinstance(value, FoamFieldFile.BoundaryDictionary):
289
+ assert not isinstance(value, FoamFile.Dictionary)
290
+ raise TypeError(f"boundary {keyword} is not a dictionary")
291
+ return value
288
292
 
289
293
  class BoundaryDictionary(FoamFile.Dictionary):
290
294
  """An OpenFOAM dictionary representing a boundary condition as a mutable mapping."""
@@ -292,7 +296,7 @@ class FoamFieldFile(FoamFile):
292
296
  def __setitem__(
293
297
  self,
294
298
  key: str,
295
- value: Union[FoamFile._SetValue, FoamFile._SetMapping],
299
+ value: FoamFile._SetValue,
296
300
  ) -> None:
297
301
  if key == "value":
298
302
  self._setitem(key, value, assume_field=True)
@@ -1,11 +1,16 @@
1
1
  import sys
2
- from typing import Optional, Tuple
2
+ from typing import Optional, Tuple, Union
3
3
 
4
4
  if sys.version_info >= (3, 9):
5
5
  from collections.abc import Mapping, MutableMapping, Sequence
6
6
  else:
7
7
  from typing import Mapping, MutableMapping, Sequence
8
8
 
9
+ if sys.version_info >= (3, 10):
10
+ from types import EllipsisType
11
+ else:
12
+ from typing import Any as EllipsisType
13
+
9
14
  from pyparsing import (
10
15
  Dict,
11
16
  Forward,
@@ -56,6 +61,28 @@ def _list_of(elem: ParserElement) -> ParserElement:
56
61
  )
57
62
 
58
63
 
64
+ def _dictionary_of(
65
+ keyword: ParserElement,
66
+ value: ParserElement,
67
+ *,
68
+ len: Union[int, EllipsisType] = ...,
69
+ located: bool = False,
70
+ ) -> ParserElement:
71
+ subdict = Forward()
72
+
73
+ entry = keyword + (
74
+ (Literal("{").suppress() + subdict + Literal("}").suppress())
75
+ | (value + Literal(";").suppress())
76
+ )
77
+
78
+ if located:
79
+ entry = Located(entry)
80
+
81
+ subdict <<= Dict(Group(entry)[...], asdict=not located)
82
+
83
+ return Dict(Group(entry)[len], asdict=not located)
84
+
85
+
59
86
  _TENSOR = _list_of(common.number) | common.number
60
87
  _IDENTIFIER = Word(identbodychars + "$", printables.replace(";", ""))
61
88
  _DIMENSIONED = (Opt(_IDENTIFIER) + _DIMENSIONS + _TENSOR).set_parse_action(
@@ -66,25 +93,19 @@ _FIELD = (Keyword("uniform").suppress() + _TENSOR) | (
66
93
  )
67
94
  _TOKEN = QuotedString('"', unquote_results=False) | _IDENTIFIER
68
95
  _ITEM = Forward()
69
- _LIST = _list_of(_ITEM)
96
+ _ENTRY = _dictionary_of(_IDENTIFIER, _ITEM, len=1)
97
+ _LIST = _list_of(_ENTRY | _ITEM)
70
98
  _ITEM <<= _FIELD | _LIST | _DIMENSIONED | _DIMENSIONS | common.number | _SWITCH | _TOKEN
99
+
71
100
  _TOKENS = (
72
101
  QuotedString('"', unquote_results=False) | Word(printables.replace(";", ""))
73
102
  )[2, ...].set_parse_action(lambda tks: " ".join(tks))
74
103
 
75
104
  _VALUE = _ITEM ^ _TOKENS
76
105
 
77
- _ENTRY = Forward()
78
- _DICTIONARY = Dict(Group(_ENTRY)[...])
79
- _ENTRY <<= Located(
80
- _TOKEN
81
- + (
82
- (Literal("{").suppress() + _DICTIONARY + Literal("}").suppress())
83
- | (Opt(_VALUE, default="") + Literal(";").suppress())
84
- )
85
- )
86
106
  _FILE = (
87
- _DICTIONARY.ignore(c_style_comment)
107
+ _dictionary_of(_TOKEN, Opt(_VALUE, default=""), located=True)
108
+ .ignore(c_style_comment)
88
109
  .ignore(cpp_style_comment)
89
110
  .ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]
90
111
  )
@@ -1,6 +1,5 @@
1
1
  import sys
2
2
  from contextlib import suppress
3
- from typing import Union
4
3
 
5
4
  if sys.version_info >= (3, 9):
6
5
  from collections.abc import Mapping
@@ -11,9 +10,7 @@ from .._util import is_sequence
11
10
  from ._base import FoamDictionaryBase
12
11
 
13
12
 
14
- def _serialize_switch(
15
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
16
- ) -> str:
13
+ def _serialize_switch(value: FoamDictionaryBase._SetValue) -> str:
17
14
  if value is True:
18
15
  return "yes"
19
16
  elif value is False:
@@ -23,7 +20,7 @@ def _serialize_switch(
23
20
 
24
21
 
25
22
  def _serialize_list(
26
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
23
+ value: FoamDictionaryBase._SetValue,
27
24
  ) -> str:
28
25
  if is_sequence(value):
29
26
  return f"({' '.join(_serialize_value(v) for v in value)})"
@@ -32,7 +29,7 @@ def _serialize_list(
32
29
 
33
30
 
34
31
  def _serialize_field(
35
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
32
+ value: FoamDictionaryBase._SetValue,
36
33
  ) -> str:
37
34
  if is_sequence(value):
38
35
  try:
@@ -61,7 +58,7 @@ def _serialize_field(
61
58
 
62
59
 
63
60
  def _serialize_dimensions(
64
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
61
+ value: FoamDictionaryBase._SetValue,
65
62
  ) -> str:
66
63
  if is_sequence(value) and len(value) == 7:
67
64
  return f"[{' '.join(str(v) for v in value)}]"
@@ -70,7 +67,7 @@ def _serialize_dimensions(
70
67
 
71
68
 
72
69
  def _serialize_dimensioned(
73
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
70
+ value: FoamDictionaryBase._SetValue,
74
71
  ) -> str:
75
72
  if isinstance(value, FoamDictionaryBase.Dimensioned):
76
73
  if value.name is not None:
@@ -82,7 +79,7 @@ def _serialize_dimensioned(
82
79
 
83
80
 
84
81
  def _serialize_value(
85
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
82
+ value: FoamDictionaryBase._SetValue,
86
83
  *,
87
84
  assume_field: bool = False,
88
85
  assume_dimensions: bool = False,
@@ -104,11 +101,14 @@ def _serialize_value(
104
101
  with suppress(TypeError):
105
102
  return _serialize_switch(value)
106
103
 
104
+ with suppress(TypeError):
105
+ return _serialize_dictionary(value)
106
+
107
107
  return str(value)
108
108
 
109
109
 
110
110
  def _serialize_dictionary(
111
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
111
+ value: FoamDictionaryBase._SetValue,
112
112
  ) -> str:
113
113
  if isinstance(value, Mapping):
114
114
  return "\n".join(serialize_entry(k, v) for k, v in value.items())
@@ -118,7 +118,7 @@ def _serialize_dictionary(
118
118
 
119
119
  def serialize_entry(
120
120
  keyword: str,
121
- value: Union[FoamDictionaryBase._SetValue, FoamDictionaryBase._SetMapping],
121
+ value: FoamDictionaryBase._SetValue,
122
122
  *,
123
123
  assume_field: bool = False,
124
124
  assume_dimensions: bool = False,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.2.7
3
+ Version: 0.2.9
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
@@ -14,5 +14,4 @@ foamlib/_dictionaries/__init__.py
14
14
  foamlib/_dictionaries/_base.py
15
15
  foamlib/_dictionaries/_files.py
16
16
  foamlib/_dictionaries/_parsing.py
17
- foamlib/_dictionaries/_serialization.py
18
- tests/test_serialization.py
17
+ foamlib/_dictionaries/_serialization.py
@@ -1,52 +0,0 @@
1
- from foamlib import FoamFile
2
- from foamlib._dictionaries._serialization import _serialize_value
3
-
4
-
5
- def test_serialize_value() -> None:
6
- assert _serialize_value(1) == "1"
7
- assert _serialize_value(1.0) == "1.0"
8
- assert _serialize_value(1.0e-3) == "0.001"
9
- assert _serialize_value(True) == "yes"
10
- assert _serialize_value(False) == "no"
11
- assert _serialize_value("word") == "word"
12
- assert _serialize_value("word word") == "word word"
13
- assert _serialize_value('"a string"') == '"a string"'
14
- assert _serialize_value(1, assume_field=True) == "uniform 1"
15
- assert _serialize_value(1.0, assume_field=True) == "uniform 1.0"
16
- assert _serialize_value(1.0e-3, assume_field=True) == "uniform 0.001"
17
- assert _serialize_value([1.0, 2.0, 3.0]) == "(1.0 2.0 3.0)"
18
- assert _serialize_value([1, 2, 3], assume_field=True) == "uniform (1 2 3)"
19
- assert (
20
- _serialize_value([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], assume_field=True)
21
- == "nonuniform List<scalar> 10(1 2 3 4 5 6 7 8 9 10)"
22
- )
23
- assert (
24
- _serialize_value([[1, 2, 3], [4, 5, 6]], assume_field=True)
25
- == "nonuniform List<vector> 2((1 2 3) (4 5 6))"
26
- )
27
- assert (
28
- _serialize_value(FoamFile.DimensionSet(mass=1, length=1, time=-2))
29
- == "[1 1 -2 0 0 0 0]"
30
- )
31
- assert (
32
- _serialize_value(
33
- FoamFile.Dimensioned(
34
- name="g",
35
- dimensions=FoamFile.DimensionSet(mass=1, length=1, time=-2),
36
- value=9.81,
37
- )
38
- )
39
- == "g [1 1 -2 0 0 0 0] 9.81"
40
- )
41
- assert (
42
- _serialize_value(
43
- FoamFile.Dimensioned(
44
- dimensions=FoamFile.DimensionSet(mass=1, length=1, time=-2), value=9.81
45
- )
46
- )
47
- == "[1 1 -2 0 0 0 0] 9.81"
48
- )
49
- assert (
50
- _serialize_value("hex (0 1 2 3 4 5 6 7) (1 1 1) simpleGrading (1 1 1)")
51
- == "hex (0 1 2 3 4 5 6 7) (1 1 1) simpleGrading (1 1 1)"
52
- )
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes