Skip to content

Commit 2fdad8d

Browse files
authored
Merge pull request #678 from gerlero/files
Fix serialization of face lists
2 parents 90a6347 + dea9577 commit 2fdad8d

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/foamlib/_files/_serialization.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,6 @@ def normalized(
177177
return data # ty: ignore[invalid-return-type]
178178
return data.astype(float, copy=False) # ty: ignore[possibly-missing-attribute]
179179

180-
# Other possible numeric standalone data (n integers or floats)
181-
case [Real(), *rest], () if not isinstance(data, tuple) and all(
182-
isinstance(r, Real) for r in rest
183-
):
184-
return normalized(np.asarray(data), keywords=keywords, format_=format_)
185-
186-
# Other possible numeric standalone data (n x 3 floats)
187-
case [_, *_], () if not isinstance(data, tuple) and all(
188-
(
189-
isinstance(r, Sequence)
190-
and not isinstance(r, tuple)
191-
and len(r) == 3
192-
and all(isinstance(x, Real) for x in r)
193-
)
194-
or (
195-
isinstance(r, np.ndarray)
196-
and r.shape == (3,)
197-
and (
198-
np.issubdtype(r.dtype, np.floating)
199-
or np.issubdtype(r.dtype, np.integer)
200-
)
201-
)
202-
for r in data # ty: ignore[not-iterable]
203-
):
204-
return normalized(np.asarray(data), keywords=keywords, format_=format_)
205-
206180
# ASCII faces-like list
207181
case [*_], () if not isinstance(data, tuple) and all(
208182
isinstance(e, np.ndarray)
@@ -232,6 +206,32 @@ def normalized(
232206
):
233207
return [np.asarray(e) for e in data] # ty: ignore[not-iterable]
234208

209+
# Other possible numeric standalone data (n integers or floats)
210+
case [Real(), *rest], () if not isinstance(data, tuple) and all(
211+
isinstance(r, Real) for r in rest
212+
):
213+
return normalized(np.asarray(data), keywords=keywords, format_=format_)
214+
215+
# Other possible numeric standalone data (n x 3 floats)
216+
case [_, *_], () if not isinstance(data, tuple) and all(
217+
(
218+
isinstance(r, Sequence)
219+
and not isinstance(r, tuple)
220+
and len(r) == 3
221+
and all(isinstance(x, Real) for x in r)
222+
)
223+
or (
224+
isinstance(r, np.ndarray)
225+
and r.shape == (3,)
226+
and (
227+
np.issubdtype(r.dtype, np.floating)
228+
or np.issubdtype(r.dtype, np.integer)
229+
)
230+
)
231+
for r in data # ty: ignore[not-iterable]
232+
):
233+
return normalized(np.asarray(data), keywords=keywords, format_=format_)
234+
235235
# Uniform field (scalar)
236236
case Real(), _common.FIELD_KEYWORDS:
237237
return float(data) # ty: ignore[invalid-argument-type]
@@ -487,14 +487,14 @@ def dumps(
487487
+ b")"
488488
)
489489

490-
case np.ndarray(), (_, *_) | None, _:
490+
case np.ndarray(), (_, *_) | None, "ascii" | None:
491491
return dumps(len(data), keywords=None, format_=None) + dumps( # ty: ignore[invalid-argument-type]
492492
data.tolist(), # ty: ignore[possibly-missing-attribute]
493493
keywords=None,
494494
format_=format_,
495495
)
496496

497-
case np.ndarray(), (), _:
497+
case np.ndarray(), (), "ascii" | None:
498498
return dumps(data.tolist(), keywords=None, format_=format_) # ty: ignore[invalid-argument-type,possibly-missing-attribute]
499499

500500
case DimensionSet(), _, _:
@@ -552,7 +552,7 @@ def dumps(
552552
+ b" ".join(
553553
dumps(
554554
v, # ty: ignore[invalid-argument-type]
555-
keywords=keywords,
555+
keywords=None,
556556
format_=format_,
557557
_tuple_is_keyword_entry=True,
558558
)

tests/test_files/test_dumps.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ def test_serialize_file() -> None:
185185
)
186186
assert FoamFile.dumps([1, 2, 3], ensure_header=False) == b"(1 2 3)"
187187
assert FoamFile.dumps([1.0, 2, 3], ensure_header=False) == b"(1.0 2.0 3.0)"
188+
assert (
189+
FoamFile.dumps([[1, 2, 3], [4, 5.0, 6]], ensure_header=False)
190+
== b"((1.0 2.0 3.0) (4.0 5.0 6.0))"
191+
)
188192
assert (
189193
FoamFile.dumps(
190194
{
@@ -199,6 +203,18 @@ def test_serialize_file() -> None:
199203
FoamFile.dumps({"#include": "$FOAM_CASE/simControls"}, ensure_header=False)
200204
== b"\n#include $FOAM_CASE/simControls\n"
201205
)
206+
assert (
207+
FoamFile.dumps([[1, 2, 3, 4], [5, 6, 7, 8]], ensure_header=False)
208+
== b"(4(1 2 3 4) 4(5 6 7 8))"
209+
)
210+
assert (
211+
FoamFile.dumps([[1, 2, 3], [4, 5, 6, 7]], ensure_header=False)
212+
== b"(3(1 2 3) 4(4 5 6 7))"
213+
)
214+
assert (
215+
FoamFile.dumps([[1, 2, 3], [4, 5, 6]], ensure_header=False)
216+
== b"(3(1 2 3) 3(4 5 6))"
217+
)
202218
indices = np.array([0, 3, 7], dtype=np.int32)
203219
values = np.array(
204220
[904040, 904479, 924424, 3516631, 3516634, 3516633, 3516632], dtype=np.int32

0 commit comments

Comments
 (0)