Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename internal serialization function #240

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions foamlib/_files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -255,7 +255,7 @@ def __setitem__(
self._write(
before
+ indentation
+ dumpb(keywords[-1])
+ dumps(keywords[-1])
+ b"\n"
+ indentation
+ b"{\n"
Expand All @@ -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:
Expand Down
36 changes: 18 additions & 18 deletions foamlib/_files/_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)):
Expand All @@ -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":
Expand All @@ -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"
Expand Down
60 changes: 30 additions & 30 deletions tests/test_files/test_dumpb.py
Original file line number Diff line number Diff line change
@@ -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<scalar> 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<vector> 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<scalar> 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<vector> 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),
Expand All @@ -49,19 +49,19 @@ 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
)
)
== 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)"
Loading