foamlib 0.2.0__py3-none-any.whl → 0.2.2__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.
foamlib/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = "0.2.0"
1
+ __version__ = "0.2.2"
2
2
 
3
3
  from ._cases import FoamCase, AsyncFoamCase, FoamCaseBase
4
4
  from ._dictionaries import FoamFile, FoamFieldFile
foamlib/_cases.py CHANGED
@@ -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():
foamlib/_dictionaries.py CHANGED
@@ -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:
@@ -205,6 +199,9 @@ class FoamFieldFile(FoamFile):
205
199
  )
206
200
  return ret
207
201
 
202
+ def __repr__(self) -> str:
203
+ return f"{type(self).__qualname__}({self._file}, {self._keywords})"
204
+
208
205
  class BoundaryDictionary(_FoamDictionary):
209
206
  """An OpenFOAM dictionary representing a boundary condition as a mutable mapping."""
210
207
 
@@ -262,7 +259,7 @@ class FoamFieldFile(FoamFile):
262
259
  del self["value"]
263
260
 
264
261
  def __repr__(self) -> str:
265
- return "FoamFieldFile.BoundaryDictionary"
262
+ return f"{type(self).__qualname__}({self._file}, {self._keywords})"
266
263
 
267
264
  def __getitem__(self, key: str) -> Union[FoamFile.Value, _FoamDictionary]:
268
265
  ret = super().__getitem__(key)
@@ -278,9 +275,6 @@ class FoamFieldFile(FoamFile):
278
275
  else:
279
276
  self._setitem(key, value)
280
277
 
281
- def __repr__(self) -> str:
282
- return "FoamFieldFile.BoundariesDictionary"
283
-
284
278
  @property
285
279
  def dimensions(self) -> FoamFile.DimensionSet:
286
280
  """
@@ -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.0
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
@@ -0,0 +1,10 @@
1
+ foamlib/__init__.py,sha256=EFlApZJu-niDqPz_q3gjFdJ4ScRjPAjKC1gfKtXLMDQ,241
2
+ foamlib/_cases.py,sha256=4f3c5BXnsHPhFvgXNjUcGGHyu7I0WZT6zxlvGhb9kMY,21213
3
+ foamlib/_dictionaries.py,sha256=5hElrfAk_2SjINqmFE1dYK4H7lg8rRTzO-vwzMWj5QM,14874
4
+ foamlib/_subprocesses.py,sha256=5vqdQvpN_2v4GgDqxi-s88NGhZ6doFxkh0XY89ZWuHA,1926
5
+ foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ foamlib-0.2.2.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
7
+ foamlib-0.2.2.dist-info/METADATA,sha256=3J72tulIUFzK32O6aGy_LDzNI1LXWzcLM-9yVRVeJVY,4600
8
+ foamlib-0.2.2.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
9
+ foamlib-0.2.2.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
10
+ foamlib-0.2.2.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- foamlib/__init__.py,sha256=s2updNwJoQGYD43xhqmQaUs_SSaeRTYKM2sjhjDRgjg,241
2
- foamlib/_cases.py,sha256=FL6d2_fCB9KCu9ps8Ur8ITlla3rSFsuvdvMYEXGCBM4,20923
3
- foamlib/_dictionaries.py,sha256=QF8f5bUg1JSMbCZK9CTULtE6y0SKnIDaUFipftXffMg,14820
4
- foamlib/_subprocesses.py,sha256=5vqdQvpN_2v4GgDqxi-s88NGhZ6doFxkh0XY89ZWuHA,1926
5
- foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
- foamlib-0.2.0.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
7
- foamlib-0.2.0.dist-info/METADATA,sha256=0-ZHRls6qUypSO521d02ocuMW05xJxG4EhgPR3byco4,4600
8
- foamlib-0.2.0.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
9
- foamlib-0.2.0.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
10
- foamlib-0.2.0.dist-info/RECORD,,