foamlib 0.6.10__py3-none-any.whl → 0.6.12__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.
@@ -1,3 +1,5 @@
1
+ from __future__ import annotations
2
+
1
3
  import array
2
4
  import sys
3
5
  from typing import Tuple, Union, cast
@@ -80,19 +82,19 @@ def _keyword_entry_of(
80
82
 
81
83
  def _unpack_binary_field(
82
84
  tks: ParseResults,
83
- ) -> Sequence[Union[Sequence[float], Sequence[Sequence[float]]]]:
85
+ ) -> Sequence[Sequence[float] | Sequence[Sequence[float]]]:
84
86
  elsize = len(tks[0]) // 8
85
87
 
86
88
  arr = array.array("d", "".join(tks).encode("latin-1"))
87
89
 
88
- all: Union[Sequence[float], Sequence[Sequence[float]]]
90
+ values: Sequence[float] | Sequence[Sequence[float]]
89
91
 
90
92
  if elsize != 1:
91
- all = [arr[i : i + elsize].tolist() for i in range(0, len(arr), elsize)]
93
+ values = [arr[i : i + elsize].tolist() for i in range(0, len(arr), elsize)]
92
94
  else:
93
- all = arr.tolist()
95
+ values = arr.tolist()
94
96
 
95
- return [all]
97
+ return [values]
96
98
 
97
99
 
98
100
  _IDENTCHARS = identchars + "$"
@@ -207,28 +209,29 @@ _FILE = (
207
209
  )
208
210
 
209
211
 
210
- class Parsed(Mapping[Tuple[str, ...], Union[FoamFileBase.Data, EllipsisType]]):
212
+ class Parsed(Mapping[Tuple[str, ...], Union[FoamFileBase._DataEntry, EllipsisType]]):
211
213
  def __init__(self, contents: bytes) -> None:
212
214
  self._parsed: MutableMapping[
213
- Tuple[str, ...],
214
- Tuple[int, Union[FoamFileBase.Data, EllipsisType], int],
215
+ tuple[str, ...],
216
+ tuple[int, FoamFileBase._DataEntry | EllipsisType, int],
215
217
  ] = {}
216
- self._end = len(contents)
217
-
218
218
  for parse_result in _FILE.parse_string(
219
219
  contents.decode("latin-1"), parse_all=True
220
220
  ):
221
221
  self._parsed.update(self._flatten_result(parse_result))
222
222
 
223
+ self.contents = contents
224
+ self.modified = False
225
+
223
226
  @staticmethod
224
227
  def _flatten_result(
225
- parse_result: ParseResults, *, _keywords: Tuple[str, ...] = ()
228
+ parse_result: ParseResults, *, _keywords: tuple[str, ...] = ()
226
229
  ) -> Mapping[
227
- Tuple[str, ...], Tuple[int, Union[FoamFileBase.Data, EllipsisType], int]
230
+ tuple[str, ...], tuple[int, FoamFileBase._DataEntry | EllipsisType, int]
228
231
  ]:
229
232
  ret: MutableMapping[
230
- Tuple[str, ...],
231
- Tuple[int, Union[FoamFileBase.Data, EllipsisType], int],
233
+ tuple[str, ...],
234
+ tuple[int, FoamFileBase._DataEntry | EllipsisType, int],
232
235
  ] = {}
233
236
  start = parse_result.locn_start
234
237
  assert isinstance(start, int)
@@ -255,35 +258,75 @@ class Parsed(Mapping[Tuple[str, ...], Union[FoamFileBase.Data, EllipsisType]]):
255
258
  return ret
256
259
 
257
260
  def __getitem__(
258
- self, keywords: Union[str, Tuple[str, ...]]
259
- ) -> Union[FoamFileBase.Data, EllipsisType]:
260
- if isinstance(keywords, str):
261
- keywords = (keywords,)
262
-
261
+ self, keywords: tuple[str, ...]
262
+ ) -> FoamFileBase._DataEntry | EllipsisType:
263
263
  _, data, _ = self._parsed[keywords]
264
264
  return data
265
265
 
266
+ def put(
267
+ self,
268
+ keywords: tuple[str, ...],
269
+ data: FoamFileBase._DataEntry | EllipsisType,
270
+ content: bytes,
271
+ ) -> None:
272
+ start, end = self.entry_location(keywords, missing_ok=True)
273
+
274
+ diff = len(content) - (end - start)
275
+ for k, (s, d, e) in self._parsed.items():
276
+ if s > end:
277
+ self._parsed[k] = (s + diff, d, e + diff)
278
+ elif e > start:
279
+ self._parsed[k] = (s, d, e + diff)
280
+
281
+ self._parsed[keywords] = (start, data, end + diff)
282
+
283
+ self.contents = self.contents[:start] + content + self.contents[end:]
284
+ self.modified = True
285
+
286
+ for k in list(self._parsed):
287
+ if keywords != k and keywords == k[: len(keywords)]:
288
+ del self._parsed[k]
289
+
290
+ def __delitem__(self, keywords: tuple[str, ...]) -> None:
291
+ start, end = self.entry_location(keywords)
292
+ del self._parsed[keywords]
293
+
294
+ for k in list(self._parsed):
295
+ if keywords == k[: len(keywords)]:
296
+ del self._parsed[k]
297
+
298
+ diff = end - start
299
+ for k, (s, d, e) in self._parsed.items():
300
+ if s > end:
301
+ self._parsed[k] = (s - diff, d, e - diff)
302
+ elif e > start:
303
+ self._parsed[k] = (s, d, e - diff)
304
+
305
+ self.contents = self.contents[:start] + self.contents[end:]
306
+ self.modified = True
307
+
266
308
  def __contains__(self, keywords: object) -> bool:
267
309
  return keywords in self._parsed
268
310
 
269
- def __iter__(self) -> Iterator[Tuple[str, ...]]:
311
+ def __iter__(self) -> Iterator[tuple[str, ...]]:
270
312
  return iter(self._parsed)
271
313
 
272
314
  def __len__(self) -> int:
273
315
  return len(self._parsed)
274
316
 
275
317
  def entry_location(
276
- self, keywords: Tuple[str, ...], *, missing_ok: bool = False
277
- ) -> Tuple[int, int]:
318
+ self, keywords: tuple[str, ...], *, missing_ok: bool = False
319
+ ) -> tuple[int, int]:
278
320
  try:
279
321
  start, _, end = self._parsed[keywords]
280
322
  except KeyError:
281
323
  if missing_ok:
282
324
  if len(keywords) > 1:
283
- _, _, end = self._parsed[keywords[:-1]]
284
- end -= 1
325
+ assert self[keywords[:-1]] is ...
326
+ start, end = self.entry_location(keywords[:-1])
327
+ end = self.contents.rindex(b"}", start, end)
285
328
  else:
286
- end = self._end
329
+ end = len(self.contents)
287
330
 
288
331
  start = end
289
332
  else:
@@ -1,12 +1,15 @@
1
+ from __future__ import annotations
2
+
1
3
  import array
2
4
  import itertools
3
5
  import sys
4
6
  from enum import Enum, auto
7
+ from typing import cast
5
8
 
6
9
  if sys.version_info >= (3, 9):
7
- from collections.abc import Mapping
10
+ from collections.abc import Mapping, Sequence
8
11
  else:
9
- from typing import Mapping
12
+ from typing import Mapping, Sequence
10
13
 
11
14
  from ._base import FoamFileBase
12
15
  from ._util import is_sequence
@@ -28,7 +31,7 @@ class Kind(Enum):
28
31
 
29
32
 
30
33
  def dumps(
31
- data: FoamFileBase._SetData,
34
+ data: FoamFileBase.Data,
32
35
  *,
33
36
  kind: Kind = Kind.DEFAULT,
34
37
  ) -> bytes:
@@ -53,7 +56,7 @@ def dumps(
53
56
  ):
54
57
  return b"[" + b" ".join(dumps(v) for v in data) + b"]"
55
58
 
56
- if (kind == Kind.FIELD or kind == Kind.BINARY_FIELD) and (
59
+ if kind in (Kind.FIELD, Kind.BINARY_FIELD) and (
57
60
  isinstance(data, (int, float))
58
61
  or is_sequence(data)
59
62
  and data
@@ -62,22 +65,27 @@ def dumps(
62
65
  ):
63
66
  return b"uniform " + dumps(data, kind=Kind.SINGLE_ENTRY)
64
67
 
65
- if (kind == Kind.FIELD or kind == Kind.BINARY_FIELD) and is_sequence(data):
66
- if isinstance(data[0], (int, float)):
68
+ if kind in (Kind.FIELD, Kind.BINARY_FIELD) and is_sequence(data):
69
+ if data and isinstance(data[0], (int, float)):
67
70
  tensor_kind = b"scalar"
68
- elif len(data[0]) == 3:
69
- tensor_kind = b"vector"
70
- elif len(data[0]) == 6:
71
- tensor_kind = b"symmTensor"
72
- elif len(data[0]) == 9:
73
- tensor_kind = b"tensor"
71
+ elif is_sequence(data[0]) and data[0] and isinstance(data[0][0], (int, float)):
72
+ if len(data[0]) == 3:
73
+ tensor_kind = b"vector"
74
+ elif len(data[0]) == 6:
75
+ tensor_kind = b"symmTensor"
76
+ elif len(data[0]) == 9:
77
+ tensor_kind = b"tensor"
78
+ else:
79
+ return dumps(data)
74
80
  else:
75
81
  return dumps(data)
76
82
 
77
83
  if kind == Kind.BINARY_FIELD:
78
84
  if tensor_kind == b"scalar":
85
+ data = cast(Sequence[float], data)
79
86
  contents = b"(" + array.array("d", data).tobytes() + b")"
80
87
  else:
88
+ data = cast(Sequence[Sequence[float]], data)
81
89
  contents = (
82
90
  b"("
83
91
  + array.array("d", itertools.chain.from_iterable(data)).tobytes()
foamlib/_files/_util.py CHANGED
@@ -1,5 +1,7 @@
1
+ from __future__ import annotations
2
+
1
3
  import sys
2
- from typing import Any
4
+ from typing import TYPE_CHECKING
3
5
 
4
6
  if sys.version_info >= (3, 9):
5
7
  from collections.abc import Sequence
@@ -11,8 +13,11 @@ if sys.version_info >= (3, 10):
11
13
  else:
12
14
  from typing_extensions import TypeGuard
13
15
 
16
+ if TYPE_CHECKING:
17
+ from ._base import FoamFileBase
18
+
14
19
 
15
20
  def is_sequence(
16
- value: Any,
17
- ) -> TypeGuard[Sequence[Any]]:
21
+ value: FoamFileBase.Data,
22
+ ) -> TypeGuard[Sequence[FoamFileBase.Data]]:
18
23
  return isinstance(value, Sequence) and not isinstance(value, str)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.6.10
3
+ Version: 0.6.12
4
4
  Summary: A Python interface for interacting with OpenFOAM
5
5
  Author-email: "Gabriel S. Gerlero" <ggerlero@cimec.unl.edu.ar>
6
6
  Project-URL: Homepage, https://github.com/gerlero/foamlib
@@ -0,0 +1,22 @@
1
+ foamlib/__init__.py,sha256=fzKDNF8_CX5rU1KVBoVQZHdjJ_nEiRbLv_sfj6-CHAY,487
2
+ foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
+ foamlib/_cases/__init__.py,sha256=wTUHcUgU1CBgpu0cUMtksQ5VKG6B8CFu9xc3dWwsQuo,358
4
+ foamlib/_cases/_async.py,sha256=i6g4EBHqvI-1PkdrxsRto2ynW7sxsOga2bSYk1XVG1U,7795
5
+ foamlib/_cases/_base.py,sha256=FKfZxP7HspWfSswQ6yZ5bGJRdZUlupQxj2tDqngXPmc,6785
6
+ foamlib/_cases/_run.py,sha256=lveqKZium_qK_eTxYE8jOjwx0eiIoolCBbi56-zLw1o,14420
7
+ foamlib/_cases/_slurm.py,sha256=kj4wqgr3foMyAoUkoHOZODRBmVqH1B9KqAIEEjM8ZBg,2328
8
+ foamlib/_cases/_subprocess.py,sha256=6BlBRxknj2-BFcGkx7oVcuL63_utSaY1Axmsc1qV9j8,3887
9
+ foamlib/_cases/_sync.py,sha256=2BJXB7Nzldb4OgPukqupgYqdceUGkI2mYhhtGPWEBrc,5901
10
+ foamlib/_cases/_util.py,sha256=lhVca3ERY0zwYjDam6W2QMROt0yX5vAF-9_DS5RuMbM,1547
11
+ foamlib/_files/__init__.py,sha256=-UqB9YTH6mrJfXCX00kPTAAY20XG64u1MGPw_1ewLVs,148
12
+ foamlib/_files/_base.py,sha256=ZY_6Zxr3LZHVzJEex6SUTi9Pgo7Oi7i3Jo-wre9G3yE,1968
13
+ foamlib/_files/_files.py,sha256=WF_FWiDhnkA4dtZ6zR1A0iGrv76eI3SetlQu6Zl6HY8,14949
14
+ foamlib/_files/_io.py,sha256=IQLqoqnA1TpHf21NbUho2wsYWevyqC6MKo-wfpaObUU,2226
15
+ foamlib/_files/_parsing.py,sha256=qLZx_VZMNEoyfmrkC_r85QBuABa26-6Y0fHnxCq74Q8,10880
16
+ foamlib/_files/_serialization.py,sha256=HN9XCDbZ0I-b_UdpEYgDVNX6U4tPd6u-uYBKOoX3uqg,3681
17
+ foamlib/_files/_util.py,sha256=VXUTD0B3NbnlaE_KCqWzrFcWvBz_2JfnIWpNp3snX3Y,526
18
+ foamlib-0.6.12.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
19
+ foamlib-0.6.12.dist-info/METADATA,sha256=fN1pEtQzMHd-yuzMkugYgTtpVwZwV-U0CPxU6rRTnzA,7742
20
+ foamlib-0.6.12.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
21
+ foamlib-0.6.12.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
22
+ foamlib-0.6.12.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (75.2.0)
2
+ Generator: setuptools (75.3.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,22 +0,0 @@
1
- foamlib/__init__.py,sha256=7AIRMyiByWRuEUEkR_MGE1zy0GuXaEzOlzXnUkCvdtw,487
2
- foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- foamlib/_cases/__init__.py,sha256=wTUHcUgU1CBgpu0cUMtksQ5VKG6B8CFu9xc3dWwsQuo,358
4
- foamlib/_cases/_async.py,sha256=oQ2XnD9sansJLJDuUl3_vHaUM4L9hq2G97IcRjQAOPY,7920
5
- foamlib/_cases/_base.py,sha256=HV7ifv60pAhgZdC3s5IxvdrDQ2vvlhzPwyB-80QQLe8,6733
6
- foamlib/_cases/_run.py,sha256=XFDBr5G1hJo0402RcZClLL_rv0d5jVgq2be25FgZcyQ,14725
7
- foamlib/_cases/_slurm.py,sha256=pUXcFSrwOK5URsoooiUOJg61X0WLLNHyv7bhUxIb7Bs,2346
8
- foamlib/_cases/_subprocess.py,sha256=tzsBGe44EnQw3GWqRW1ktEZ4sNQ1sSHdJ_89zIfF5L0,3952
9
- foamlib/_cases/_sync.py,sha256=ENvn8zseRh6YOjAgqXHzmKhKFYXdzGIW5NoMHS_NKPY,6049
10
- foamlib/_cases/_util.py,sha256=GNndpqw3Jg_S-Hxzl5vwRgD0czcTNb9NYHMhcfBoMBg,1493
11
- foamlib/_files/__init__.py,sha256=-UqB9YTH6mrJfXCX00kPTAAY20XG64u1MGPw_1ewLVs,148
12
- foamlib/_files/_base.py,sha256=mrZuk90sdIrSGiOeEtCcEHoz6RK4qUc6GFoDSuo7U3w,1920
13
- foamlib/_files/_files.py,sha256=6ZbUxb2VF225ceEHBg4L23oxHvD7M18QC9zqMPm0ZS4,16025
14
- foamlib/_files/_io.py,sha256=ArD9lgMw-Kjj-wl_5qU6eRdW4lZWyGBIEMWLCNV4a0Y,2467
15
- foamlib/_files/_parsing.py,sha256=SvPAbNb_xJwOBRGii4oLOUvhj9OdJ8auMIzdx77AYwI,9394
16
- foamlib/_files/_serialization.py,sha256=iIiPUbgoQzg6xeYeeTsRPWKWqrYFtAQPgglBbylPkms,3341
17
- foamlib/_files/_util.py,sha256=UMzXmTFgvbp46w6k3oEZJoYC98pFgEK6LN5uLOwrlCg,397
18
- foamlib-0.6.10.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
19
- foamlib-0.6.10.dist-info/METADATA,sha256=0IYRom9JRsqS6OE6x3rlRDdL6zYUubtpYl5hafW_2Bg,7742
20
- foamlib-0.6.10.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
21
- foamlib-0.6.10.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
22
- foamlib-0.6.10.dist-info/RECORD,,