|
14 | 14 | #include <cstdint> // <cstdint> requires c++11 support
|
15 | 15 | #include <functional>
|
16 | 16 | #include <string> // std::stod
|
| 17 | +#include <sstream> |
17 | 18 |
|
18 | 19 | #ifndef WITHOUT_NUMPY
|
19 | 20 | # define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
|
@@ -1457,6 +1458,14 @@ bool contour(const std::vector<NumericX>& x, const std::vector<NumericY>& y,
|
1457 | 1458 | return res;
|
1458 | 1459 | }
|
1459 | 1460 |
|
| 1461 | +bool isFloat(std::string s) { |
| 1462 | + std::istringstream iss(s); |
| 1463 | + float f; |
| 1464 | + iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid |
| 1465 | + // Check the entire string was consumed and if either failbit or badbit is set |
| 1466 | + return iss.eof() && !iss.fail(); |
| 1467 | +} |
| 1468 | + |
1460 | 1469 | template<typename NumericX, typename NumericY, typename NumericU, typename NumericW>
|
1461 | 1470 | bool quiver(const std::vector<NumericX>& x, const std::vector<NumericY>& y, const std::vector<NumericU>& u, const std::vector<NumericW>& w, const std::map<std::string, std::string>& keywords = {})
|
1462 | 1471 | {
|
@@ -1513,7 +1522,12 @@ bool quiver(const NumericX* x, const NumericY* y, const NumericU* u, const Numer
|
1513 | 1522 | PyObject* kwargs = PyDict_New();
|
1514 | 1523 | for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
|
1515 | 1524 | {
|
1516 |
| - PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); |
| 1525 | + if(isFloat(it->second)) { |
| 1526 | + PyObject *float_str = PyUnicode_FromString(it->second.c_str()); |
| 1527 | + PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromString(float_str)); |
| 1528 | + } else { |
| 1529 | + PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str())); |
| 1530 | + } |
1517 | 1531 | }
|
1518 | 1532 |
|
1519 | 1533 | PyObject* res = PyObject_Call(
|
@@ -1656,7 +1670,13 @@ bool quiver(const NumericX* x, const NumericY* y, const NumericZ* z, const Numer
|
1656 | 1670 | PyObject* kwargs = PyDict_New();
|
1657 | 1671 | for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
|
1658 | 1672 | {
|
| 1673 | + if(isFloat(it->second)) { |
| 1674 | + PyObject *float_str = PyUnicode_FromString(it->second.c_str()); |
| 1675 | + PyDict_SetItemString(kwargs, it->first.c_str(), PyFloat_FromString(float_str)); |
| 1676 | + } |
| 1677 | + else { |
1659 | 1678 | PyDict_SetItemString(kwargs, it->first.c_str(), PyUnicode_FromString(it->second.c_str()));
|
| 1679 | + } |
1660 | 1680 | }
|
1661 | 1681 |
|
1662 | 1682 | //get figure gca to enable 3d projection
|
|
0 commit comments