foamlib 0.2.1__tar.gz → 0.2.2__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.2.1
3
+ Version: 0.2.2
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
@@ -1,4 +1,4 @@
1
- __version__ = "0.2.1"
1
+ __version__ = "0.2.2"
2
2
 
3
3
  from ._cases import FoamCase, AsyncFoamCase, FoamCaseBase
4
4
  from ._dictionaries import FoamFile, FoamFieldFile
@@ -64,6 +64,14 @@ class FoamCaseBase(Sequence["FoamCaseBase.TimeDirectory"]):
64
64
  except FileNotFoundError as e:
65
65
  raise KeyError(key) from e
66
66
 
67
+ def __contains__(self, obj: object) -> bool:
68
+ if isinstance(obj, FoamFieldFile):
69
+ return obj.path.parent == self.path
70
+ elif isinstance(obj, str):
71
+ return (self.path / obj).is_file()
72
+ else:
73
+ return False
74
+
67
75
  def __iter__(self) -> Iterator[FoamFieldFile]:
68
76
  for p in self.path.iterdir():
69
77
  if p.is_file():
@@ -1,6 +1,5 @@
1
1
  from pathlib import Path
2
2
  from dataclasses import dataclass
3
- from collections import namedtuple
4
3
  from contextlib import suppress
5
4
  from typing import (
6
5
  Any,
@@ -10,6 +9,7 @@ from typing import (
10
9
  Optional,
11
10
  Mapping,
12
11
  MutableMapping,
12
+ NamedTuple,
13
13
  cast,
14
14
  )
15
15
 
@@ -20,16 +20,15 @@ from pyparsing import (
20
20
  Keyword,
21
21
  LineEnd,
22
22
  Literal,
23
- Located,
24
23
  Opt,
24
+ ParserElement,
25
25
  QuotedString,
26
26
  Word,
27
27
  c_style_comment,
28
28
  common,
29
29
  cpp_style_comment,
30
- printables,
31
- identchars,
32
30
  identbodychars,
31
+ printables,
33
32
  )
34
33
 
35
34
  try:
@@ -76,8 +75,7 @@ class _FoamDictionary(MutableMapping[str, Union["FoamFile.Value", "_FoamDictiona
76
75
  ) from None
77
76
 
78
77
  def __getitem__(self, key: str) -> Union["FoamFile.Value", "_FoamDictionary"]:
79
- contents = self._file.path.read_text()
80
- value = _DICTIONARY.parse_string(contents, parse_all=True).as_dict()
78
+ value = _DICTIONARY.parse_file(self._file.path, parse_all=True).as_dict()
81
79
 
82
80
  for key in [*self._keywords, key]:
83
81
  value = value[key]
@@ -85,8 +83,7 @@ class _FoamDictionary(MutableMapping[str, Union["FoamFile.Value", "_FoamDictiona
85
83
  if isinstance(value, dict):
86
84
  return _FoamDictionary(self._file, [*self._keywords, key])
87
85
  else:
88
- start, end = value
89
- return _VALUE.parse_string(contents[start:end], parse_all=True).as_list()[0]
86
+ return value
90
87
 
91
88
  def _setitem(
92
89
  self,
@@ -101,7 +98,6 @@ class _FoamDictionary(MutableMapping[str, Union["FoamFile.Value", "_FoamDictiona
101
98
  elif isinstance(value, Mapping):
102
99
  self._cmd(["-set", "{}"], key=key)
103
100
  subdict = self[key]
104
- print(subdict)
105
101
  assert isinstance(subdict, _FoamDictionary)
106
102
  for k, v in value.items():
107
103
  subdict[k] = v
@@ -139,7 +135,7 @@ class _FoamDictionary(MutableMapping[str, Union["FoamFile.Value", "_FoamDictiona
139
135
  return len(list(iter(self)))
140
136
 
141
137
  def __repr__(self) -> str:
142
- return "FoamFile.Dictionary"
138
+ return f"FoamFile.Dictionary({self._file}, {self._keywords})"
143
139
 
144
140
 
145
141
  class FoamFile(_FoamDictionary):
@@ -147,19 +143,17 @@ class FoamFile(_FoamDictionary):
147
143
 
148
144
  Dictionary = _FoamDictionary
149
145
 
150
- DimensionSet = namedtuple(
151
- "DimensionSet",
152
- [
153
- "mass",
154
- "length",
155
- "time",
156
- "temperature",
157
- "moles",
158
- "current",
159
- "luminous_intensity",
160
- ],
161
- defaults=(0, 0, 0, 0, 0, 0, 0),
162
- )
146
+ class DimensionSet(NamedTuple):
147
+ mass: Union[int, float] = 0
148
+ length: Union[int, float] = 0
149
+ time: Union[int, float] = 0
150
+ temperature: Union[int, float] = 0
151
+ moles: Union[int, float] = 0
152
+ current: Union[int, float] = 0
153
+ luminous_intensity: Union[int, float] = 0
154
+
155
+ def __repr__(self) -> str:
156
+ return f"{type(self).__qualname__}({', '.join(f'{n}={v}' for n, v in zip(self._fields, self) if v != 0)})"
163
157
 
164
158
  @dataclass
165
159
  class Dimensioned:
@@ -206,7 +200,7 @@ class FoamFieldFile(FoamFile):
206
200
  return ret
207
201
 
208
202
  def __repr__(self) -> str:
209
- return "FoamFieldFile.BoundariesDictionary"
203
+ return f"{type(self).__qualname__}({self._file}, {self._keywords})"
210
204
 
211
205
  class BoundaryDictionary(_FoamDictionary):
212
206
  """An OpenFOAM dictionary representing a boundary condition as a mutable mapping."""
@@ -265,7 +259,7 @@ class FoamFieldFile(FoamFile):
265
259
  del self["value"]
266
260
 
267
261
  def __repr__(self) -> str:
268
- return "FoamFieldFile.BoundaryDictionary"
262
+ return f"{type(self).__qualname__}({self._file}, {self._keywords})"
269
263
 
270
264
  def __getitem__(self, key: str) -> Union[FoamFile.Value, _FoamDictionary]:
271
265
  ret = super().__getitem__(key)
@@ -343,62 +337,65 @@ _NO = Keyword("no").set_parse_action(lambda: False)
343
337
  _DIMENSIONS = (
344
338
  Literal("[").suppress() + common.number * 7 + Literal("]").suppress()
345
339
  ).set_parse_action(lambda tks: FoamFile.DimensionSet(*tks))
346
- _TOKEN = QuotedString('"', unquote_results=False) | Word(
347
- identchars + "$", identbodychars
348
- )
349
- _ITEM = Forward()
350
- _LIST = Opt(
351
- Literal("List") + Literal("<") + common.identifier + Literal(">")
352
- ).suppress() + (
353
- (
354
- Opt(common.integer).suppress()
355
- + Literal("(").suppress()
356
- + Group(_ITEM[...])
357
- + Literal(")").suppress()
340
+
341
+
342
+ def _list_of(elem: ParserElement) -> ParserElement:
343
+ return Opt(
344
+ Literal("List") + Literal("<") + common.identifier + Literal(">")
345
+ ).suppress() + (
346
+ (
347
+ Opt(common.integer).suppress()
348
+ + (
349
+ Literal("(").suppress()
350
+ + Group((elem)[...]).set_parse_action(lambda tks: tks.as_list())
351
+ + Literal(")").suppress()
352
+ )
353
+ )
354
+ | (
355
+ common.integer + Literal("{").suppress() + elem + Literal("}").suppress()
356
+ ).set_parse_action(lambda tks: [[tks[1]] * tks[0]])
358
357
  )
359
- | (
360
- common.integer + Literal("{").suppress() + _ITEM + Literal("}").suppress()
361
- ).set_parse_action(lambda tks: [tks[1]] * tks[0])
362
- )
363
- _FIELD = (Keyword("uniform").suppress() + _ITEM) | (
364
- Keyword("nonuniform").suppress() + _LIST
365
- )
366
- _DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action(
358
+
359
+
360
+ _TENSOR = _list_of(common.number) | common.number
361
+ _IDENTIFIER = Word(identbodychars + "$", identbodychars + "({,./:^!)}")
362
+ _DIMENSIONED = (Opt(_IDENTIFIER) + _DIMENSIONS + _TENSOR).set_parse_action(
367
363
  lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list()))
368
364
  )
365
+ _FIELD = (Keyword("uniform").suppress() + _TENSOR) | (
366
+ Keyword("nonuniform").suppress() + _list_of(_TENSOR)
367
+ )
368
+ _TOKEN = QuotedString('"', unquote_results=False) | _IDENTIFIER
369
+ _DICTIONARY = Forward()
370
+ _SUBDICT = Literal("{").suppress() + _DICTIONARY + Literal("}").suppress()
371
+ _ITEM = Forward()
372
+ _LIST = _list_of(_ITEM)
369
373
  _ITEM <<= (
370
- _FIELD | _LIST | _DIMENSIONED | _DIMENSIONS | common.number | _YES | _NO | _TOKEN
374
+ _FIELD
375
+ | _LIST
376
+ | _SUBDICT
377
+ | _DIMENSIONED
378
+ | _DIMENSIONS
379
+ | common.number
380
+ | _YES
381
+ | _NO
382
+ | _TOKEN
371
383
  )
372
-
373
384
  _TOKENS = (
374
- QuotedString('"', unquote_results=False)
375
- | Word(printables.replace(";", "").replace("{", "").replace("}", ""))
385
+ QuotedString('"', unquote_results=False) | Word(printables.replace(";", ""))
376
386
  )[2, ...].set_parse_action(lambda tks: " ".join(tks))
377
387
 
378
- _VALUE = (_ITEM ^ _TOKENS).ignore(c_style_comment).ignore(cpp_style_comment)
379
-
388
+ _VALUE = _ITEM ^ _TOKENS
380
389
 
381
- _UNPARSED_VALUE = (
382
- QuotedString('"', unquote_results=False)
383
- | Word(printables.replace(";", "").replace("{", "").replace("}", ""))
384
- )[...]
385
- _KEYWORD = QuotedString('"', unquote_results=False) | Word(
386
- identchars + "$(,.)", identbodychars + "$(,.)"
387
- )
388
- _DICTIONARY = Forward()
389
- _ENTRY = _KEYWORD + (
390
- (
391
- Located(_UNPARSED_VALUE).set_parse_action(lambda tks: (tks[0], tks[2]))
392
- + Literal(";").suppress()
393
- )
394
- | (Literal("{").suppress() + _DICTIONARY + Literal("}").suppress())
390
+ _ENTRY = _IDENTIFIER + (
391
+ (Opt(_VALUE, default=None) + Literal(";").suppress()) | _SUBDICT
395
392
  )
396
393
  _DICTIONARY <<= (
397
394
  Dict(Group(_ENTRY)[...])
398
395
  .set_parse_action(lambda tks: {} if not tks else tks)
399
396
  .ignore(c_style_comment)
400
397
  .ignore(cpp_style_comment)
401
- .ignore(Literal("#include") + ... + LineEnd()) # type: ignore
398
+ .ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]
402
399
  )
403
400
 
404
401
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: foamlib
3
- Version: 0.2.1
3
+ Version: 0.2.2
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
@@ -25,17 +25,26 @@ def test_parse_value() -> None:
25
25
  assert _VALUE.parse_string("(1.0 2.0 3.0)").as_list()[0] == [1.0, 2.0, 3.0]
26
26
  assert _VALUE.parse_string("uniform (1 2 3)").as_list()[0] == [1, 2, 3]
27
27
  assert _VALUE.parse_string("nonuniform List<scalar> 2(1 2)").as_list()[0] == [1, 2]
28
+ assert _VALUE.parse_string("nonuniform List<scalar> 2{1}").as_list()[0] == [1, 1]
28
29
  assert _VALUE.parse_string("3(1 2 3)").as_list()[0] == [1, 2, 3]
29
30
  assert _VALUE.parse_string("2((1 2 3) (4 5 6))").as_list()[0] == [
30
31
  [1, 2, 3],
31
32
  [4, 5, 6],
32
33
  ]
34
+ assert _VALUE.parse_string("2{(1 2 3)}").as_list()[0] == [
35
+ [1, 2, 3],
36
+ [1, 2, 3],
37
+ ]
33
38
  assert _VALUE.parse_string("nonuniform List<vector> 2((1 2 3) (4 5 6))").as_list()[
34
39
  0
35
40
  ] == [
36
41
  [1, 2, 3],
37
42
  [4, 5, 6],
38
43
  ]
44
+ assert _VALUE.parse_string("nonuniform List<vector> 2{(1 2 3)}").as_list()[0] == [
45
+ [1, 2, 3],
46
+ [1, 2, 3],
47
+ ]
39
48
  assert _VALUE.parse_string("[1 1 -2 0 0 0 0]").as_list()[
40
49
  0
41
50
  ] == FoamFile.DimensionSet(mass=1, length=1, time=-2)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes