Skip to content

Commit 0a55824

Browse files
authored
Merge pull request #584 from gerlero/files
Update parsing of binary lists
2 parents 97c09d0 + 531c9ee commit 0a55824

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

foamlib/_files/_parsing/_elements.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -151,39 +151,42 @@ def parseImpl(
151151

152152

153153
def binary_numeric_list(
154-
dtype: DTypeLike, *, nested: int | None = None, empty_ok: bool = False
154+
dtype: DTypeLike, elshape: tuple[()] | tuple[int] = (), *, empty_ok: bool = False
155155
) -> ParserElement:
156156
dtype = np.dtype(dtype)
157157

158-
elsize = nested if nested is not None else 1
159-
160158
list_ = Forward()
161159

162160
def process_count(tks: ParseResults) -> None:
163161
nonlocal list_
164-
(size,) = tks
165-
assert isinstance(size, int)
162+
(count,) = tks
163+
assert isinstance(count, int)
166164

167-
if size == 0 and not empty_ok:
165+
if count == 0 and not empty_ok:
168166
list_ <<= NoMatch()
169167
return
170168

171-
list_ <<= Regex(rf"\((?s:{'.' * dtype.itemsize * elsize}){{{size}}}\)")
169+
if not elshape:
170+
elsize = 1
171+
else:
172+
(elsize,) = elshape
173+
174+
list_ <<= Regex(rf"\((?s:{'.' * dtype.itemsize * elsize}){{{count}}}\)")
172175

173176
def to_array(
174177
tks: ParseResults,
175178
) -> np.ndarray[tuple[int] | tuple[int, int], np.dtype[np.integer | np.floating]]:
176-
size, s = tks
177-
assert isinstance(size, int)
178-
assert isinstance(s, str)
179-
assert s[0] == "("
180-
assert s[-1] == ")"
181-
s = s[1:-1]
179+
count, contents = tks
180+
assert isinstance(count, int)
181+
assert isinstance(contents, str)
182+
assert contents[0] == "("
183+
assert contents[-1] == ")"
184+
contents = contents[1:-1]
182185

183-
ret = np.frombuffer(s.encode("latin-1"), dtype=dtype)
186+
ret = np.frombuffer(contents.encode("latin-1"), dtype=dtype)
184187

185-
if nested is not None:
186-
ret = ret.reshape(-1, nested)
188+
if elshape:
189+
ret = ret.reshape(count, *elshape)
187190

188191
return ret
189192

foamlib/_files/_parsing/_grammar.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@
9191
).suppress()
9292
+ (
9393
ASCIINumericList(dtype=float, elshape=(3,), empty_ok=True)
94-
| binary_numeric_list(np.float64, nested=3, empty_ok=True)
95-
| binary_numeric_list(np.float32, nested=3, empty_ok=True)
94+
| binary_numeric_list(np.float64, elshape=(3,), empty_ok=True)
95+
| binary_numeric_list(np.float32, elshape=(3,), empty_ok=True)
9696
)
9797
)
9898
| (
@@ -101,8 +101,8 @@
101101
).suppress()
102102
+ (
103103
ASCIINumericList(dtype=float, elshape=(6,), empty_ok=True)
104-
| binary_numeric_list(np.float64, nested=6, empty_ok=True)
105-
| binary_numeric_list(np.float32, nested=6, empty_ok=True)
104+
| binary_numeric_list(np.float64, elshape=(6,), empty_ok=True)
105+
| binary_numeric_list(np.float32, elshape=(6,), empty_ok=True)
106106
)
107107
)
108108
| (
@@ -111,8 +111,8 @@
111111
).suppress()
112112
+ (
113113
ASCIINumericList(dtype=float, elshape=(9,), empty_ok=True)
114-
| binary_numeric_list(np.float64, nested=9, empty_ok=True)
115-
| binary_numeric_list(np.float32, nested=9, empty_ok=True)
114+
| binary_numeric_list(np.float64, elshape=(9,), empty_ok=True)
115+
| binary_numeric_list(np.float32, elshape=(9,), empty_ok=True)
116116
)
117117
)
118118
)
@@ -156,8 +156,8 @@
156156
binary_numeric_list(dtype=np.int32) + Opt(binary_numeric_list(dtype=np.int32))
157157
).add_parse_action(lambda tks: tuple(tks) if len(tks) > 1 else tks[0])
158158
| binary_numeric_list(dtype=np.float64)
159-
| binary_numeric_list(dtype=np.float64, nested=3)
160-
| binary_numeric_list(dtype=np.float32, nested=3)
159+
| binary_numeric_list(dtype=np.float64, elshape=(3,))
160+
| binary_numeric_list(dtype=np.float32, elshape=(3,))
161161
| _DATA
162162
).add_parse_action(lambda tks: [None, tks[0]])
163163

0 commit comments

Comments
 (0)