Skip to content

Commit e216395

Browse files
authored
Merge pull request #24 from gerlero/dictionary
Refactor dictionary parsing and serialization
2 parents 982ad3f + b655315 commit e216395

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

foamlib/_dictionaries.py

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,46 +64,46 @@ def __post_init__(self) -> None:
6464
A value that can be stored in an OpenFOAM dictionary.
6565
"""
6666

67-
_yes = Keyword("yes").set_parse_action(lambda s, loc, tks: True)
68-
_no = Keyword("no").set_parse_action(lambda s, loc, tks: False)
69-
_value = Forward()
70-
_list = Opt(
67+
_YES = Keyword("yes").set_parse_action(lambda s, loc, tks: True)
68+
_NO = Keyword("no").set_parse_action(lambda s, loc, tks: False)
69+
_VALUE = Forward()
70+
_LIST = Opt(
7171
Literal("List") + Literal("<") + common.identifier + Literal(">")
7272
).suppress() + (
7373
(
7474
Opt(common.integer).suppress()
7575
+ Literal("(").suppress()
76-
+ Group(ZeroOrMore(_value))
76+
+ Group(ZeroOrMore(_VALUE))
7777
+ Literal(")").suppress()
7878
)
7979
| (
80-
common.integer + Literal("{").suppress() + _value + Literal("}").suppress()
80+
common.integer + Literal("{").suppress() + _VALUE + Literal("}").suppress()
8181
).set_parse_action(lambda s, loc, tks: [tks[1]] * tks[0])
8282
)
83-
_uniform_field = Keyword("uniform").suppress() + _value
84-
_nonuniform_field = Keyword("nonuniform").suppress() + _list
85-
_dimensions = (
83+
_FIELD = (Keyword("uniform").suppress() + _VALUE) | (
84+
Keyword("nonuniform").suppress() + _LIST
85+
)
86+
_DIMENSIONS = (
8687
Literal("[").suppress() + common.number * 7 + Literal("]").suppress()
8788
).set_parse_action(lambda s, loc, tks: FoamDimensionSet(*tks))
88-
_dimensioned = (common.identifier + _dimensions + _value).set_parse_action(
89+
_DIMENSIONED = (common.identifier + _DIMENSIONS + _VALUE).set_parse_action(
8990
lambda s, loc, tks: FoamDimensioned(tks[0], tks[1], tks[2].as_list())
9091
)
9192

92-
_value << (
93-
_uniform_field
94-
| _nonuniform_field
95-
| _list
96-
| _dimensioned
97-
| _dimensions
93+
_VALUE << (
94+
_FIELD
95+
| _LIST
96+
| _DIMENSIONED
97+
| _DIMENSIONS
9898
| common.number
99-
| _yes
100-
| _no
99+
| _YES
100+
| _NO
101101
| common.identifier
102102
)
103103

104104

105105
def _parse(value: str) -> FoamValue:
106-
return cast(FoamValue, _value.parse_string(value, parse_all=True).as_list()[0])
106+
return cast(FoamValue, _VALUE.parse_string(value, parse_all=True).as_list()[0])
107107

108108

109109
def _serialize_bool(value: Any) -> str:
@@ -115,22 +115,24 @@ def _serialize_bool(value: Any) -> str:
115115
raise TypeError(f"Not a bool: {type(value)}")
116116

117117

118-
def _serialize_list(sequence: Any) -> str:
119-
if (
120-
isinstance(sequence, Sequence)
121-
and not isinstance(sequence, str)
118+
def _is_sequence(value: Any) -> bool:
119+
return (
120+
isinstance(value, Sequence)
121+
and not isinstance(value, str)
122122
or numpy
123-
and isinstance(sequence, np.ndarray)
124-
):
125-
return f"({' '.join(_serialize(v) for v in sequence)})"
123+
and isinstance(value, np.ndarray)
124+
)
125+
126+
127+
def _serialize_list(value: Any) -> str:
128+
if _is_sequence(value):
129+
return f"({' '.join(_serialize(v) for v in value)})"
126130
else:
127-
raise TypeError(f"Not a valid sequence: {type(sequence)}")
131+
raise TypeError(f"Not a valid sequence: {type(value)}")
128132

129133

130134
def _serialize_field(value: Any) -> str:
131-
if isinstance(value, (int, float)):
132-
return f"uniform {value}"
133-
else:
135+
if _is_sequence(value):
134136
try:
135137
s = _serialize_list(value)
136138
except TypeError:
@@ -152,15 +154,12 @@ def _serialize_field(value: Any) -> str:
152154
f"Unsupported sequence length for field: {len(value[0])}"
153155
)
154156
return f"nonuniform List<{kind}> {len(value)}{s}"
157+
else:
158+
return f"uniform {value}"
155159

156160

157161
def _serialize_dimensions(value: Any) -> str:
158-
if (
159-
isinstance(value, Sequence)
160-
and not isinstance(value, str)
161-
or numpy
162-
and isinstance(value, np.ndarray)
163-
) and len(value) == 7:
162+
if _is_sequence(value) and len(value) == 7:
164163
return f"[{' '.join(str(v) for v in value)}]"
165164
else:
166165
raise TypeError(f"Not a valid dimension set: {type(value)}")

0 commit comments

Comments
 (0)