chore: Bind __str__ fns for py interop list types
The default __str__ for these list types shows item addresses (not the items themselves)...a bit of a headscratcher considering the lists' element types have proper __str__ functions.
This commit is contained in:
1 parent
c281bc49b5
commit
1e0ec5bb46
1 file changed
+28
-5
@@ -63,6 +63,24 @@ namespace py = pybind11;
|
||||
def(_STR(name), &type::name) \
|
||||
.def(_STR(set_##name), &type::set_##name, py::arg(#name))
|
||||
|
||||
#define PY_LIST_TO_STRING(type) \
|
||||
def("__str__", [](const std::vector<shared_ptr<type>> &py_list) { \
|
||||
std::ostringstream oss; \
|
||||
oss << "["; \
|
||||
for (size_t i = 0; i < py_list.size(); ++i) { \
|
||||
if (i > 0) { \
|
||||
oss << ", "; \
|
||||
} \
|
||||
if (py_list[i]) { \
|
||||
oss << py_list[i]->to_string(); \
|
||||
} else { \
|
||||
oss << "None"; \
|
||||
} \
|
||||
} \
|
||||
oss << "]"; \
|
||||
return oss.str(); \
|
||||
})
|
||||
|
||||
// Define python module with c++ classes.
|
||||
PYBIND11_MODULE(python_interop_module, m) {
|
||||
// price info
|
||||
@@ -75,7 +93,8 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
.PROP(PyContractPriceInfo, max_price)
|
||||
.def("__str__", &PyContractPriceInfo::to_string);
|
||||
py::bind_vector<std::vector<PyContractPriceInfo>>(
|
||||
m, "PyContractPriceInfoList");
|
||||
m, "PyContractPriceInfoList")
|
||||
.PY_LIST_TO_STRING(PyContractPriceInfo);
|
||||
// quantity info
|
||||
py::class_<PyContractQuantityInfo>(m, "PyContractQuantityInfo")
|
||||
.def(py::init<>())
|
||||
@@ -87,7 +106,8 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
.PROP(PyContractQuantityInfo, max_price)
|
||||
.def("__str__", &PyContractQuantityInfo::to_string);
|
||||
py::bind_vector<std::vector<PyContractQuantityInfo>>(
|
||||
m, "PyContractQuantityInfoList");
|
||||
m, "PyContractQuantityInfoList")
|
||||
.PY_LIST_TO_STRING(PyContractQuantityInfo);
|
||||
// contract spec
|
||||
py::class_<PyContractSpecification>(m, "PyContractSpecification")
|
||||
.def(py::init<>())
|
||||
@@ -124,7 +144,8 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
.PROP(PySecurity, option)
|
||||
.def("__str__", &PySecurity::to_string)
|
||||
.def("__format__", &PySecurity::format);
|
||||
py::bind_vector<std::vector<shared_ptr<PySecurity>>>(m, "PySecurityList");
|
||||
py::bind_vector<std::vector<shared_ptr<PySecurity>>>(m, "PySecurityList")
|
||||
.PY_LIST_TO_STRING(PySecurity);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// Md
|
||||
@@ -158,7 +179,8 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
py::arg("asks"))
|
||||
.def("__len__", &PyBook::size)
|
||||
.def("__str__", &PyBook::to_string);
|
||||
py::bind_vector<std::vector<shared_ptr<PyBook>>>(m, "PyBookList");
|
||||
py::bind_vector<std::vector<shared_ptr<PyBook>>>(m, "PyBookList")
|
||||
.PY_LIST_TO_STRING(PyBook);
|
||||
// trade
|
||||
py::class_<PyTrade, shared_ptr<PyTrade>>(m, "PyTrade")
|
||||
.def(py::init<>())
|
||||
@@ -169,7 +191,8 @@ PYBIND11_MODULE(python_interop_module, m) {
|
||||
.PROP(PyTrade, quantity)
|
||||
.PROP(PyTrade, aggressor_side)
|
||||
.PROP(PyTrade, timestamp);
|
||||
py::bind_vector<std::vector<shared_ptr<PyTrade>>>(m, "PyTradeList");
|
||||
py::bind_vector<std::vector<shared_ptr<PyTrade>>>(m, "PyTradeList")
|
||||
.PY_LIST_TO_STRING(PyTrade);
|
||||
// md bridge
|
||||
py::class_<MdBridge>(m, "MdBridge")
|
||||
.DEF_ARGS(MdBridge, on_securities, py::arg("securities"))
|
||||
|
||||
Reference in new issue
Block a user