foamlib 0.3.0__py3-none-any.whl → 0.3.1__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/__init__.py +1 -1
- foamlib/_files/_base.py +14 -3
- foamlib/_files/_fields.py +12 -5
- foamlib/_files/_files.py +3 -3
- foamlib/_files/_serialization.py +59 -132
- {foamlib-0.3.0.dist-info → foamlib-0.3.1.dist-info}/METADATA +1 -1
- foamlib-0.3.1.dist-info/RECORD +16 -0
- foamlib-0.3.0.dist-info/RECORD +0 -16
- {foamlib-0.3.0.dist-info → foamlib-0.3.1.dist-info}/LICENSE.txt +0 -0
- {foamlib-0.3.0.dist-info → foamlib-0.3.1.dist-info}/WHEEL +0 -0
- {foamlib-0.3.0.dist-info → foamlib-0.3.1.dist-info}/top_level.txt +0 -0
foamlib/__init__.py
CHANGED
foamlib/_files/_base.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import sys
|
2
2
|
from abc import abstractmethod
|
3
3
|
from dataclasses import dataclass
|
4
|
-
from typing import Dict, NamedTuple, Optional, Union
|
4
|
+
from typing import Dict, NamedTuple, Optional, Tuple, Union
|
5
5
|
|
6
6
|
if sys.version_info >= (3, 9):
|
7
7
|
from collections.abc import Mapping, Sequence
|
@@ -10,7 +10,6 @@ else:
|
|
10
10
|
|
11
11
|
try:
|
12
12
|
import numpy as np
|
13
|
-
from numpy.typing import NDArray
|
14
13
|
except ModuleNotFoundError:
|
15
14
|
pass
|
16
15
|
|
@@ -59,4 +58,16 @@ class FoamDict:
|
|
59
58
|
"""Return a nested dict representation of the dictionary."""
|
60
59
|
raise NotImplementedError
|
61
60
|
|
62
|
-
_SetData = Union[
|
61
|
+
_SetData = Union[
|
62
|
+
str,
|
63
|
+
int,
|
64
|
+
float,
|
65
|
+
bool,
|
66
|
+
Dimensioned,
|
67
|
+
DimensionSet,
|
68
|
+
Sequence["_SetData"],
|
69
|
+
Mapping[str, "_SetData"],
|
70
|
+
"np.ndarray[Tuple[()], np.dtype[np.generic]]",
|
71
|
+
"np.ndarray[Tuple[int], np.dtype[np.generic]]",
|
72
|
+
"np.ndarray[Tuple[int, int], np.dtype[np.generic]]",
|
73
|
+
]
|
foamlib/_files/_fields.py
CHANGED
@@ -10,7 +10,6 @@ from ._files import FoamFile
|
|
10
10
|
|
11
11
|
try:
|
12
12
|
import numpy as np
|
13
|
-
from numpy.typing import NDArray
|
14
13
|
except ModuleNotFoundError:
|
15
14
|
pass
|
16
15
|
|
@@ -58,7 +57,9 @@ class FoamFieldFile(FoamFile):
|
|
58
57
|
int,
|
59
58
|
float,
|
60
59
|
Sequence[Union[int, float, Sequence[Union[int, float]]]],
|
61
|
-
"
|
60
|
+
"np.ndarray[Tuple[()], np.dtype[np.generic]]",
|
61
|
+
"np.ndarray[Tuple[int], np.dtype[np.generic]]",
|
62
|
+
"np.ndarray[Tuple[int, int], np.dtype[np.generic]]",
|
62
63
|
]:
|
63
64
|
"""Alias of `self["value"]`."""
|
64
65
|
ret = self["value"]
|
@@ -73,7 +74,9 @@ class FoamFieldFile(FoamFile):
|
|
73
74
|
int,
|
74
75
|
float,
|
75
76
|
Sequence[Union[int, float, Sequence[Union[int, float]]]],
|
76
|
-
"
|
77
|
+
"np.ndarray[Tuple[()], np.dtype[np.generic]]",
|
78
|
+
"np.ndarray[Tuple[int], np.dtype[np.generic]]",
|
79
|
+
"np.ndarray[Tuple[int, int], np.dtype[np.generic]]",
|
77
80
|
],
|
78
81
|
) -> None:
|
79
82
|
self["value"] = value
|
@@ -128,7 +131,9 @@ class FoamFieldFile(FoamFile):
|
|
128
131
|
int,
|
129
132
|
float,
|
130
133
|
Sequence[Union[int, float, Sequence[Union[int, float]]]],
|
131
|
-
"
|
134
|
+
"np.ndarray[Tuple[()], np.dtype[np.generic]]",
|
135
|
+
"np.ndarray[Tuple[int], np.dtype[np.generic]]",
|
136
|
+
"np.ndarray[Tuple[int, int], np.dtype[np.generic]]",
|
132
137
|
]:
|
133
138
|
"""Alias of `self["internalField"]`."""
|
134
139
|
ret = self["internalField"]
|
@@ -143,7 +148,9 @@ class FoamFieldFile(FoamFile):
|
|
143
148
|
int,
|
144
149
|
float,
|
145
150
|
Sequence[Union[int, float, Sequence[Union[int, float]]]],
|
146
|
-
"
|
151
|
+
"np.ndarray[Tuple[()], np.dtype[np.generic]]",
|
152
|
+
"np.ndarray[Tuple[int], np.dtype[np.generic]]",
|
153
|
+
"np.ndarray[Tuple[int, int], np.dtype[np.generic]]",
|
147
154
|
],
|
148
155
|
) -> None:
|
149
156
|
self["internalField"] = value
|
foamlib/_files/_files.py
CHANGED
@@ -12,7 +12,7 @@ else:
|
|
12
12
|
|
13
13
|
from ._base import FoamDict
|
14
14
|
from ._io import FoamFileIO
|
15
|
-
from ._serialization import
|
15
|
+
from ._serialization import serialize
|
16
16
|
|
17
17
|
|
18
18
|
class FoamFile(
|
@@ -134,7 +134,7 @@ class FoamFile(
|
|
134
134
|
start, end = parsed.entry_location(keywords, missing_ok=True)
|
135
135
|
|
136
136
|
self._write(
|
137
|
-
f"{contents[:start]}\n{
|
137
|
+
f"{contents[:start]}\n{serialize({keywords[-1]: {}})}\n{contents[end:]}"
|
138
138
|
)
|
139
139
|
|
140
140
|
for k, v in data.items():
|
@@ -143,7 +143,7 @@ class FoamFile(
|
|
143
143
|
start, end = parsed.entry_location(keywords, missing_ok=True)
|
144
144
|
|
145
145
|
self._write(
|
146
|
-
f"{contents[:start]}\n{
|
146
|
+
f"{contents[:start]}\n{serialize({keywords[-1]: data}, assume_field=assume_field, assume_dimensions=assume_dimensions)}\n{contents[end:]}"
|
147
147
|
)
|
148
148
|
|
149
149
|
def __setitem__(
|
foamlib/_files/_serialization.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
import sys
|
2
|
-
from contextlib import suppress
|
3
2
|
|
4
3
|
if sys.version_info >= (3, 9):
|
5
4
|
from collections.abc import Mapping
|
@@ -10,147 +9,75 @@ from .._util import is_sequence
|
|
10
9
|
from ._base import FoamDict
|
11
10
|
|
12
11
|
|
13
|
-
def
|
14
|
-
if data is True:
|
15
|
-
return "yes"
|
16
|
-
elif data is False:
|
17
|
-
return "no"
|
18
|
-
else:
|
19
|
-
raise TypeError(f"Not a bool: {type(data)}")
|
20
|
-
|
21
|
-
|
22
|
-
def _serialize_list(
|
23
|
-
data: FoamDict._SetData,
|
24
|
-
) -> str:
|
25
|
-
if is_sequence(data):
|
26
|
-
return f"({' '.join(_serialize_data_entry(v) for v in data)})"
|
27
|
-
else:
|
28
|
-
raise TypeError(f"Not a valid sequence: {type(data)}")
|
29
|
-
|
30
|
-
|
31
|
-
def _serialize_field(
|
12
|
+
def serialize(
|
32
13
|
data: FoamDict._SetData,
|
14
|
+
*,
|
15
|
+
assume_field: bool = False,
|
16
|
+
assume_dimensions: bool = False,
|
17
|
+
assume_data_entries: bool = False,
|
33
18
|
) -> str:
|
34
|
-
if
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
19
|
+
if isinstance(data, Mapping):
|
20
|
+
entries = []
|
21
|
+
for k, v in data.items():
|
22
|
+
s = serialize(
|
23
|
+
v,
|
24
|
+
assume_field=assume_field,
|
25
|
+
assume_dimensions=assume_dimensions,
|
26
|
+
assume_data_entries=True,
|
27
|
+
)
|
28
|
+
if isinstance(v, Mapping):
|
29
|
+
entries.append(f"{k}\n{{\n{s}\n}}")
|
30
|
+
elif s:
|
31
|
+
entries.append(f"{k} {s};")
|
42
32
|
else:
|
43
|
-
|
44
|
-
|
45
|
-
elif len(data[0]) == 3:
|
46
|
-
kind = "vector"
|
47
|
-
elif len(data[0]) == 6:
|
48
|
-
kind = "symmTensor"
|
49
|
-
elif len(data[0]) == 9:
|
50
|
-
kind = "tensor"
|
51
|
-
else:
|
52
|
-
raise TypeError(
|
53
|
-
f"Unsupported sequence length for field: {len(data[0])}"
|
54
|
-
)
|
55
|
-
return f"nonuniform List<{kind}> {len(data)}{s}"
|
56
|
-
else:
|
57
|
-
return f"uniform {data}"
|
58
|
-
|
33
|
+
entries.append(f"{k};")
|
34
|
+
return "\n".join(entries)
|
59
35
|
|
60
|
-
|
61
|
-
|
62
|
-
)
|
63
|
-
if is_sequence(data) and len(data) == 7:
|
36
|
+
elif isinstance(data, FoamDict.DimensionSet) or (
|
37
|
+
assume_dimensions and is_sequence(data) and len(data) == 7
|
38
|
+
):
|
64
39
|
return f"[{' '.join(str(v) for v in data)}]"
|
65
|
-
else:
|
66
|
-
raise TypeError(f"Not a valid dimension set: {type(data)}")
|
67
40
|
|
41
|
+
elif assume_field and isinstance(data, (int, float)):
|
42
|
+
return f"uniform {data}"
|
68
43
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
44
|
+
elif assume_field and is_sequence(data):
|
45
|
+
if isinstance(data[0], (int, float)) and len(data) in (3, 6, 9):
|
46
|
+
return f"uniform {serialize(data)}"
|
47
|
+
elif isinstance(data[0], (int, float)):
|
48
|
+
return f"nonuniform List<scalar> {len(data)}{serialize(data)}"
|
49
|
+
elif len(data[0]) == 3:
|
50
|
+
return f"nonuniform List<vector> {len(data)}{serialize(data)}"
|
51
|
+
elif len(data[0]) == 6:
|
52
|
+
return f"nonuniform List<symmTensor> {len(data)}{serialize(data)}"
|
53
|
+
elif len(data[0]) == 9:
|
54
|
+
return f"nonuniform List<tensor> {len(data)}{serialize(data)}"
|
55
|
+
else:
|
56
|
+
return serialize(
|
57
|
+
data,
|
58
|
+
assume_dimensions=assume_dimensions,
|
59
|
+
assume_data_entries=assume_data_entries,
|
60
|
+
)
|
61
|
+
|
62
|
+
elif assume_data_entries and isinstance(data, tuple):
|
63
|
+
return " ".join(
|
64
|
+
serialize(v, assume_field=assume_field, assume_dimensions=assume_dimensions)
|
65
|
+
for v in data
|
66
|
+
)
|
67
|
+
|
68
|
+
elif isinstance(data, FoamDict.Dimensioned):
|
73
69
|
if data.name is not None:
|
74
|
-
return f"{data.name} {
|
70
|
+
return f"{data.name} {serialize(data.dimensions, assume_dimensions=True)} {serialize(data.value)}"
|
75
71
|
else:
|
76
|
-
return f"{
|
77
|
-
else:
|
78
|
-
raise TypeError(f"Not a valid dimensioned value: {type(data)}")
|
72
|
+
return f"{serialize(data.dimensions, assume_dimensions=True)} {serialize(data.value)}"
|
79
73
|
|
74
|
+
elif is_sequence(data):
|
75
|
+
return f"({' '.join(serialize(v) for v in data)})"
|
80
76
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
assume_dimensions: bool = False,
|
86
|
-
) -> str:
|
87
|
-
if isinstance(data, FoamDict.DimensionSet) or assume_dimensions:
|
88
|
-
with suppress(TypeError):
|
89
|
-
return _serialize_dimensions(data)
|
90
|
-
|
91
|
-
if assume_field:
|
92
|
-
with suppress(TypeError):
|
93
|
-
return _serialize_field(data)
|
94
|
-
|
95
|
-
with suppress(TypeError):
|
96
|
-
return _serialize_dimensioned(data)
|
97
|
-
|
98
|
-
with suppress(TypeError):
|
99
|
-
return _serialize_list(data)
|
100
|
-
|
101
|
-
with suppress(TypeError):
|
102
|
-
return _serialize_switch(data)
|
103
|
-
|
104
|
-
with suppress(TypeError):
|
105
|
-
return _serialize_dictionary(data)
|
106
|
-
|
107
|
-
return str(data)
|
108
|
-
|
109
|
-
|
110
|
-
def _serialize_data_entries(
|
111
|
-
data: FoamDict._SetData,
|
112
|
-
*,
|
113
|
-
assume_field: bool = False,
|
114
|
-
assume_dimensions: bool = False,
|
115
|
-
) -> str:
|
116
|
-
if isinstance(data, FoamDict.DimensionSet) or assume_dimensions:
|
117
|
-
with suppress(TypeError):
|
118
|
-
return _serialize_dimensions(data)
|
119
|
-
|
120
|
-
if assume_field:
|
121
|
-
with suppress(TypeError):
|
122
|
-
return _serialize_field(data)
|
123
|
-
|
124
|
-
if isinstance(data, tuple):
|
125
|
-
return " ".join(_serialize_data_entry(v) for v in data)
|
126
|
-
|
127
|
-
return _serialize_data_entry(data)
|
128
|
-
|
129
|
-
|
130
|
-
def _serialize_dictionary(
|
131
|
-
data: FoamDict._SetData,
|
132
|
-
) -> str:
|
133
|
-
if isinstance(data, Mapping):
|
134
|
-
return "\n".join(serialize_keyword_entry(k, v) for k, v in data.items())
|
135
|
-
else:
|
136
|
-
raise TypeError(f"Not a valid dictionary: {type(data)}")
|
137
|
-
|
138
|
-
|
139
|
-
def serialize_keyword_entry(
|
140
|
-
keyword: str,
|
141
|
-
data: FoamDict._SetData,
|
142
|
-
*,
|
143
|
-
assume_field: bool = False,
|
144
|
-
assume_dimensions: bool = False,
|
145
|
-
) -> str:
|
146
|
-
with suppress(TypeError):
|
147
|
-
return f"{keyword}\n{{\n{_serialize_dictionary(data)}\n}}"
|
148
|
-
|
149
|
-
data = _serialize_data_entries(
|
150
|
-
data, assume_field=assume_field, assume_dimensions=assume_dimensions
|
151
|
-
)
|
77
|
+
elif data is True:
|
78
|
+
return "yes"
|
79
|
+
elif data is False:
|
80
|
+
return "no"
|
152
81
|
|
153
|
-
if not data:
|
154
|
-
return f"{keyword};"
|
155
82
|
else:
|
156
|
-
return
|
83
|
+
return str(data)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
foamlib/__init__.py,sha256=tO70QZaLcvSKedqwp14wd35SdnVPg57RHiaPV_q33wI,381
|
2
|
+
foamlib/_cases.py,sha256=SkUthKb98zXPMYACaGyfGC0Fo1jzD6cC4qEGesHfNfc,20554
|
3
|
+
foamlib/_util.py,sha256=PBTpBwt_j1GXASncSDZUR8pH2u_h8UyJXm8GeFKebTY,2552
|
4
|
+
foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
+
foamlib/_files/__init__.py,sha256=m4y_7wFV2Voly1aJrTYALjT3S1Aq7EbaijxdGFbJrmU,160
|
6
|
+
foamlib/_files/_base.py,sha256=YA5a-i5HZuA3JslCD6r-DwZzpSA8r42dqSXef286Ako,2050
|
7
|
+
foamlib/_files/_fields.py,sha256=jVHOHEV70i6QrWzqhF_W5CywUZyJxN5Eh5S1MrNn1G0,5632
|
8
|
+
foamlib/_files/_files.py,sha256=EyZnvP9okZmElzL9db4FkOD82qizeJrgb_ZGIM4xoXI,5987
|
9
|
+
foamlib/_files/_io.py,sha256=alxMyxQh0zb6BZYqomZwOo9dQujMpRuS5yfpIKBEnaM,1976
|
10
|
+
foamlib/_files/_parsing.py,sha256=blyt1kpYruoW5I6DMDg8jhg6f5oz7gzedmAImuJ5b4k,5966
|
11
|
+
foamlib/_files/_serialization.py,sha256=Pq45Y3_DmblAk7ccUHf6RvipC_GQqU3SV89NOtV9CEg,2695
|
12
|
+
foamlib-0.3.1.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
|
13
|
+
foamlib-0.3.1.dist-info/METADATA,sha256=blCma_pulIQtuhsmJZ36RCRZxhdEnQxB6u2wfK-AVZY,4650
|
14
|
+
foamlib-0.3.1.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
15
|
+
foamlib-0.3.1.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
|
16
|
+
foamlib-0.3.1.dist-info/RECORD,,
|
foamlib-0.3.0.dist-info/RECORD
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
foamlib/__init__.py,sha256=erRnbigN4XBPDC0ykv-Lw7LVsw_xtuJ7h0tgkBTNQZ8,381
|
2
|
-
foamlib/_cases.py,sha256=SkUthKb98zXPMYACaGyfGC0Fo1jzD6cC4qEGesHfNfc,20554
|
3
|
-
foamlib/_util.py,sha256=PBTpBwt_j1GXASncSDZUR8pH2u_h8UyJXm8GeFKebTY,2552
|
4
|
-
foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
-
foamlib/_files/__init__.py,sha256=m4y_7wFV2Voly1aJrTYALjT3S1Aq7EbaijxdGFbJrmU,160
|
6
|
-
foamlib/_files/_base.py,sha256=vONemjJA-NxRuIjuxwz8PynjtF1UImrfBQm1EbgZEec,1768
|
7
|
-
foamlib/_files/_fields.py,sha256=jZZiMoBn0tixGnUGpITpRxcauqhPvYk65PIktBFvEHk,5073
|
8
|
-
foamlib/_files/_files.py,sha256=1fV1TUwtNsxzt1R1YKMbB_AGpjMl-W-7YQXl7Ynoyzc,6025
|
9
|
-
foamlib/_files/_io.py,sha256=alxMyxQh0zb6BZYqomZwOo9dQujMpRuS5yfpIKBEnaM,1976
|
10
|
-
foamlib/_files/_parsing.py,sha256=blyt1kpYruoW5I6DMDg8jhg6f5oz7gzedmAImuJ5b4k,5966
|
11
|
-
foamlib/_files/_serialization.py,sha256=kbl1OTvWqHwcDwS2k5CuqjDRmiv3NpA9hIXyv4boWBA,4268
|
12
|
-
foamlib-0.3.0.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
|
13
|
-
foamlib-0.3.0.dist-info/METADATA,sha256=hl5EKAs6_mbW-1qcuIFst4I6tDQqCkG4LyZlx3DMhZY,4650
|
14
|
-
foamlib-0.3.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
15
|
-
foamlib-0.3.0.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
|
16
|
-
foamlib-0.3.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|