Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Fix legacy setting of python dicts where not set keys are set to zero…
Browse files Browse the repository at this point in the history
… when transforming to list
  • Loading branch information
maierbn committed Feb 11, 2020
1 parent 1109f80 commit 95c039e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/src/control/python_config.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ getOptionVector(std::string keyString, int nEntries, std::vector<T> &values) con
values.resize(nEntries, T{0.0});
return;
}
values = PythonUtility::convertFromPython<std::vector<T>>::get(pyLocalValues);
values = PythonUtility::convertFromPython<std::vector<T>>::get(pyLocalValues, T{0.0});
if (values.size() < nEntries)
{
LOG(WARNING) << pathString << "[\"" << keyString << "\"]: given vector has only " << values.size() << " entries, fill with 0's to size "
Expand Down
71 changes: 71 additions & 0 deletions core/src/utility/python_utility_convert.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,77 @@ struct PythonUtility::convertFromPython<std::vector<ValueType>>
return defaultValue;
}

//! convert a python object to its corresponding c type, with type checking
static std::vector<ValueType> get(PyObject *object, ValueType defaultValue)
{
// start critical section for python API calls
// PythonUtility::GlobalInterpreterLock lock;

std::vector<ValueType> result;
assert(object != nullptr);
if (PyList_Check(object))
{
int nEntries = (int)PyList_Size(object);
result.resize(nEntries);

for (int i = 0; i < nEntries; i++)
{
result[i] = PythonUtility::convertFromPython<ValueType>::get(PyList_GetItem(object, (Py_ssize_t)i));
}
return result;
}
else if (PyTuple_Check(object))
{
int nEntries = PyTuple_Size(object);
result.resize(nEntries + 1);

for (int i = 0; i < nEntries; i++)
{
result[i] = PythonUtility::convertFromPython<ValueType>::get(PyTuple_GetItem(object, (Py_ssize_t)i));
}
return result;
}
else if (PyDict_Check(object))
{
PyObject *itemList = PyDict_Items(object);

for (int itemListIndex = 0; itemListIndex < PyList_Size(itemList); itemListIndex++)
{
PyObject *tuple = PyList_GetItem(itemList, (Py_ssize_t)itemListIndex);
PyObject *pyKey = PyTuple_GetItem(tuple, (Py_ssize_t)0);
PyObject *pyValue = PyTuple_GetItem(tuple, (Py_ssize_t)1);

int key = convertFromPython<int>::get(pyKey);

if (key >= 0)
{
if (key >= result.size())
result.resize(key + 1, defaultValue);
result[key] = convertFromPython<ValueType>::get(pyValue);
}
}
return result;
}
else if (object == Py_None)
{
// object is None, this means empty list
return result;
}
else
{
ValueType valueDouble = PythonUtility::convertFromPython<ValueType>::get(object);

result.resize(1);
result[0] = valueDouble;

return result;
}

#ifndef __PGI
return std::vector<ValueType>();
#endif
}

//! convert a python object to its corresponding c type, with type checking, if conversion is not possible use trivial default value (0 or 0.0 or "")
static std::vector<ValueType> get(PyObject *object)
{
Expand Down
2 changes: 1 addition & 1 deletion testing/unit_testing/src/1_rank/poisson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ config = {
"FiniteElementMethod" : {
"nElements": [4, 5],
"physicalExtent": [1.0, 5.0/4.],
"rightHandSide": {4:0, 5:0, 2:5, 7:7.0, 3:9, "8":3, 0:1, 1:4, 6.0:5, "10":0},
"rightHandSide": {2:5, 7:7.0, 3:9, "8":3, 0:1, 1:4, 6.0:5, "10":0},
"dirichletBoundaryConditions": {0:0},
"relativeTolerance": 1e-15,
},
Expand Down

0 comments on commit 95c039e

Please sign in to comment.