diff --git a/foamlib/_files/_files.py b/foamlib/_files/_files.py index cb54615..95b5daf 100644 --- a/foamlib/_files/_files.py +++ b/foamlib/_files/_files.py @@ -13,7 +13,7 @@ from ._base import FoamFileBase from ._io import FoamFileIO -from ._serialization import Kind, dumpb +from ._serialization import Kind, dumps from ._util import is_sequence if TYPE_CHECKING: @@ -255,7 +255,7 @@ def __setitem__( self._write( before + indentation - + dumpb(keywords[-1]) + + dumps(keywords[-1]) + b"\n" + indentation + b"{\n" @@ -271,15 +271,15 @@ def __setitem__( self._write( before + indentation - + dumpb(keywords[-1]) + + dumps(keywords[-1]) + b" " - + dumpb(data, kind=kind) + + dumps(data, kind=kind) + b";" + after ) else: - self._write(before + dumpb(data, kind=kind) + after) + self._write(before + dumps(data, kind=kind) + after) def __delitem__(self, keywords: Optional[Union[str, Tuple[str, ...]]]) -> None: if not keywords: diff --git a/foamlib/_files/_serialization.py b/foamlib/_files/_serialization.py index 5362229..2d76263 100644 --- a/foamlib/_files/_serialization.py +++ b/foamlib/_files/_serialization.py @@ -27,31 +27,31 @@ class Kind(Enum): DIMENSIONS = auto() -def dumpb( +def dumps( data: FoamFileBase._SetData, *, kind: Kind = Kind.DEFAULT, ) -> bytes: if numpy and isinstance(data, np.ndarray): - return dumpb(data.tolist(), kind=kind) + return dumps(data.tolist(), kind=kind) if isinstance(data, Mapping): entries = [] for k, v in data.items(): - b = dumpb(v, kind=kind) + b = dumps(v, kind=kind) if isinstance(v, Mapping): - entries.append(dumpb(k) + b" {" + b + b"}") + entries.append(dumps(k) + b" {" + b + b"}") elif not b: - entries.append(dumpb(k) + b";") + entries.append(dumps(k) + b";") else: - entries.append(dumpb(k) + b" " + b + b";") + entries.append(dumps(k) + b" " + b + b";") return b" ".join(entries) if isinstance(data, FoamFileBase.DimensionSet) or ( kind == Kind.DIMENSIONS and is_sequence(data) and len(data) == 7 ): - return b"[" + b" ".join(dumpb(v) for v in data) + b"]" + return b"[" + b" ".join(dumps(v) for v in data) + b"]" if (kind == Kind.FIELD or kind == Kind.BINARY_FIELD) and ( isinstance(data, (int, float)) @@ -60,7 +60,7 @@ def dumpb( and isinstance(data[0], (int, float)) and len(data) in (3, 6, 9) ): - return b"uniform " + dumpb(data, kind=Kind.SINGLE_ENTRY) + return b"uniform " + dumps(data, kind=Kind.SINGLE_ENTRY) if (kind == Kind.FIELD or kind == Kind.BINARY_FIELD) and is_sequence(data): if isinstance(data[0], (int, float)): @@ -72,7 +72,7 @@ def dumpb( elif len(data[0]) == 9: tensor_kind = b"tensor" else: - return dumpb(data) + return dumps(data) if kind == Kind.BINARY_FIELD: if tensor_kind == b"scalar": @@ -84,30 +84,30 @@ def dumpb( + b")" ) else: - contents = dumpb(data, kind=Kind.SINGLE_ENTRY) + contents = dumps(data, kind=Kind.SINGLE_ENTRY) - return b"nonuniform List<" + tensor_kind + b"> " + dumpb(len(data)) + contents + return b"nonuniform List<" + tensor_kind + b"> " + dumps(len(data)) + contents if kind != Kind.SINGLE_ENTRY and isinstance(data, tuple): - return b" ".join(dumpb(v) for v in data) + return b" ".join(dumps(v) for v in data) if isinstance(data, FoamFileBase.Dimensioned): if data.name is not None: return ( - dumpb(data.name) + dumps(data.name) + b" " - + dumpb(data.dimensions, kind=Kind.DIMENSIONS) + + dumps(data.dimensions, kind=Kind.DIMENSIONS) + b" " - + dumpb(data.value, kind=Kind.SINGLE_ENTRY) + + dumps(data.value, kind=Kind.SINGLE_ENTRY) ) return ( - dumpb(data.dimensions, kind=Kind.DIMENSIONS) + dumps(data.dimensions, kind=Kind.DIMENSIONS) + b" " - + dumpb(data.value, kind=Kind.SINGLE_ENTRY) + + dumps(data.value, kind=Kind.SINGLE_ENTRY) ) if is_sequence(data): - return b"(" + b" ".join(dumpb(v, kind=Kind.SINGLE_ENTRY) for v in data) + b")" + return b"(" + b" ".join(dumps(v, kind=Kind.SINGLE_ENTRY) for v in data) + b")" if data is True: return b"yes" diff --git a/tests/test_files/test_dumpb.py b/tests/test_files/test_dumpb.py index b19a7c3..7e3bff0 100644 --- a/tests/test_files/test_dumpb.py +++ b/tests/test_files/test_dumpb.py @@ -1,45 +1,45 @@ from foamlib import FoamFile -from foamlib._files._serialization import Kind, dumpb +from foamlib._files._serialization import Kind, dumps def test_serialize_data() -> None: - assert dumpb(1) == b"1" - assert dumpb(1.0) == b"1.0" - assert dumpb(1.0e-3) == b"0.001" - assert dumpb(True) == b"yes" - assert dumpb(False) == b"no" - assert dumpb("word") == b"word" - assert dumpb(("word", "word")) == b"word word" - assert dumpb('"a string"') == b'"a string"' - assert dumpb(1, kind=Kind.FIELD) == b"uniform 1" - assert dumpb(1.0, kind=Kind.FIELD) == b"uniform 1.0" - assert dumpb(1.0e-3, kind=Kind.FIELD) == b"uniform 0.001" - assert dumpb([1.0, 2.0, 3.0]) == b"(1.0 2.0 3.0)" - assert dumpb([1, 2, 3], kind=Kind.FIELD) == b"uniform (1 2 3)" + assert dumps(1) == b"1" + assert dumps(1.0) == b"1.0" + assert dumps(1.0e-3) == b"0.001" + assert dumps(True) == b"yes" + assert dumps(False) == b"no" + assert dumps("word") == b"word" + assert dumps(("word", "word")) == b"word word" + assert dumps('"a string"') == b'"a string"' + assert dumps(1, kind=Kind.FIELD) == b"uniform 1" + assert dumps(1.0, kind=Kind.FIELD) == b"uniform 1.0" + assert dumps(1.0e-3, kind=Kind.FIELD) == b"uniform 0.001" + assert dumps([1.0, 2.0, 3.0]) == b"(1.0 2.0 3.0)" + assert dumps([1, 2, 3], kind=Kind.FIELD) == b"uniform (1 2 3)" assert ( - dumpb([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], kind=Kind.FIELD) + dumps([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], kind=Kind.FIELD) == b"nonuniform List 10(1 2 3 4 5 6 7 8 9 10)" ) assert ( - dumpb([[1, 2, 3], [4, 5, 6]], kind=Kind.FIELD) + dumps([[1, 2, 3], [4, 5, 6]], kind=Kind.FIELD) == b"nonuniform List 2((1 2 3) (4 5 6))" ) - assert dumpb(1, kind=Kind.BINARY_FIELD) == b"uniform 1" - assert dumpb(1.0, kind=Kind.BINARY_FIELD) == b"uniform 1.0" - assert dumpb([1, 2, 3], kind=Kind.BINARY_FIELD) == b"uniform (1 2 3)" + assert dumps(1, kind=Kind.BINARY_FIELD) == b"uniform 1" + assert dumps(1.0, kind=Kind.BINARY_FIELD) == b"uniform 1.0" + assert dumps([1, 2, 3], kind=Kind.BINARY_FIELD) == b"uniform (1 2 3)" assert ( - dumpb([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], kind=Kind.BINARY_FIELD) + dumps([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], kind=Kind.BINARY_FIELD) == b'nonuniform List 10(\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@\x00\x00\x00\x00\x00\x00\x1c@\x00\x00\x00\x00\x00\x00 @\x00\x00\x00\x00\x00\x00"@\x00\x00\x00\x00\x00\x00$@)' ) assert ( - dumpb([[1, 2, 3], [4, 5, 6]], kind=Kind.BINARY_FIELD) + dumps([[1, 2, 3], [4, 5, 6]], kind=Kind.BINARY_FIELD) == b"nonuniform List 2(\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x08@\x00\x00\x00\x00\x00\x00\x10@\x00\x00\x00\x00\x00\x00\x14@\x00\x00\x00\x00\x00\x00\x18@)" ) assert ( - dumpb(FoamFile.DimensionSet(mass=1, length=1, time=-2)) == b"[1 1 -2 0 0 0 0]" + dumps(FoamFile.DimensionSet(mass=1, length=1, time=-2)) == b"[1 1 -2 0 0 0 0]" ) assert ( - dumpb( + dumps( FoamFile.Dimensioned( name="g", dimensions=FoamFile.DimensionSet(mass=1, length=1, time=-2), @@ -49,7 +49,7 @@ def test_serialize_data() -> None: == b"g [1 1 -2 0 0 0 0] 9.81" ) assert ( - dumpb( + dumps( FoamFile.Dimensioned( dimensions=FoamFile.DimensionSet(mass=1, length=1, time=-2), value=9.81 ) @@ -57,11 +57,11 @@ def test_serialize_data() -> None: == b"[1 1 -2 0 0 0 0] 9.81" ) assert ( - dumpb(("hex", [0, 1, 2, 3, 4, 5, 6, 7], [1, 1, 1], "simpleGrading", [1, 1, 1])) + dumps(("hex", [0, 1, 2, 3, 4, 5, 6, 7], [1, 1, 1], "simpleGrading", [1, 1, 1])) == b"hex (0 1 2 3 4 5 6 7) (1 1 1) simpleGrading (1 1 1)" ) - assert dumpb([{"a": "b"}, {"c": "d"}]) == b"(a b; c d;)" - assert dumpb([{"a": {"b": "c"}}, {"d": {"e": "g"}}]) == b"(a {b c;} d {e g;})" - assert dumpb([{"a": [0, 1, 2]}, {"b": {}}]) == b"(a (0 1 2); b {})" - assert dumpb(["water", "oil", "mercury", "air"]) == b"(water oil mercury air)" - assert dumpb("div(phi,U)") == b"div(phi,U)" + assert dumps([{"a": "b"}, {"c": "d"}]) == b"(a b; c d;)" + assert dumps([{"a": {"b": "c"}}, {"d": {"e": "g"}}]) == b"(a {b c;} d {e g;})" + assert dumps([{"a": [0, 1, 2]}, {"b": {}}]) == b"(a (0 1 2); b {})" + assert dumps(["water", "oil", "mercury", "air"]) == b"(water oil mercury air)" + assert dumps("div(phi,U)") == b"div(phi,U)"