Skip to content

Commit 04074d2

Browse files
committed
Add ParmParse.to_dict()
Convert the `ParmParse` entries to a dict. Use type hints whenever possible to create a proper value variable. Only file inputs and CLI inputs are string values that way.
1 parent b470243 commit 04074d2

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/Base/ParmParse.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,53 @@ void init_ParmParse(py::module &m)
103103
)
104104

105105
// TODO: dumpTable, hasUnusedInputs, getUnusedInputs, getEntries
106+
107+
.def(
108+
"to_dict",
109+
[](ParmParse &pp) {
110+
py::dict d;
111+
112+
auto g_table = pp.table();
113+
114+
std::vector<std::string> sorted_names;
115+
sorted_names.reserve(g_table.size());
116+
for (auto const& [name, entry] : g_table) {
117+
sorted_names.push_back(name);
118+
}
119+
std::sort(sorted_names.begin(), sorted_names.end());
120+
121+
for (auto const& name : sorted_names) {
122+
auto const& entry = g_table[name];
123+
for (auto const & vals : entry.m_vals) {
124+
if (vals.size() == 1) {
125+
std::visit(
126+
[&d,&name,&pp](auto&& arg) {
127+
using T = std::remove_pointer_t<std::decay_t<decltype(arg)>>;
128+
T v;
129+
pp.get(name.c_str(), v);
130+
d[name.c_str()] = v;
131+
},
132+
entry.m_typehint
133+
);
134+
} else {
135+
std::visit(
136+
[&d,&name,&pp](auto&& arg) {
137+
using T = std::remove_pointer_t<std::decay_t<decltype(arg)>>;
138+
if constexpr (!std::is_same_v<T, bool>) {
139+
std::vector<T> valarr;
140+
pp.getarr(name.c_str(), valarr);
141+
d[name.c_str()] = valarr;
142+
}
143+
},
144+
entry.m_typehint
145+
);
146+
}
147+
}
148+
}
149+
150+
return d;
151+
},
152+
"DOC GOES HERE TODO TODO"
153+
)
106154
;
107155
}

tests/test_parmparse.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,36 @@ def test_parmparse():
1313
dt = pp_param.get_real("dt")
1414
dopml = pp_param.get_bool("do_pml")
1515

16+
pp_param.add("ncell", 42) # overwrite file
17+
pp_param.add(
18+
"question", "What is the answer to life, the universe, and everything?"
19+
)
20+
pp_param.add("answer", 41)
21+
pp_param.add("answer", 42) # last wins
22+
pp_param.add("pi_approx", 3.1415)
23+
pp_param.addarr("floats", [1.0, 2.0, 3.0])
24+
pp_param.addarr("ints", [4, 5, 6])
25+
pp_param.addarr("strs", ["Who", "Where", "What", "When", "How"])
26+
1627
assert dopml
1728
assert dt == 1.0e-5
1829
assert ncell == 100
1930

31+
# printing
2032
pp.pretty_print_table()
33+
34+
# type hints
35+
d = pp.to_dict()
36+
assert isinstance(d["param.ncell"], int) # overwritten
37+
assert isinstance(d["param.dt"], str) # file
38+
assert isinstance(d["param.do_pml"], str) # file
39+
assert isinstance(d["param.question"], str)
40+
assert isinstance(d["param.answer"], int)
41+
assert d["param.answer"] == 42 # last wins
42+
assert isinstance(d["param.pi_approx"], float)
43+
assert isinstance(d["param.floats"], list)
44+
assert isinstance(d["param.ints"], list)
45+
assert isinstance(d["param.strs"], list)
46+
assert d["param.floats"] == [1.0, 2.0, 3.0]
47+
assert d["param.ints"] == [4, 5, 6]
48+
assert d["param.strs"] == ["Who", "Where", "What", "When", "How"]

0 commit comments

Comments
 (0)