hikyuu2/hikyuu_pywrap/_Block.cpp
2024-04-23 18:04:05 +08:00

119 lines
3.5 KiB
C++

/*
* _Block.cpp
*
* Created on: 2015年2月10日
* Author: fasiondog
*/
#include <hikyuu/serialization/Block_serialization.h>
#include "pybind_utils.h"
using namespace hku;
namespace py = pybind11;
#if defined(_MSC_VER)
#pragma warning(disable : 4267)
#endif
string (Block::*getCategory)() const = &Block::category;
void (Block::*setCategory)(const string&) = &Block::category;
string (Block::*getName)() const = &Block::name;
void (Block::*setName)(const string&) = &Block::name;
bool (Block::*add_1)(const Stock&) = &Block::add;
bool (Block::*add_2)(const string&) = &Block::add;
bool (Block::*remove_1)(const Stock&) = &Block::remove;
bool (Block::*remove_2)(const string&) = &Block::remove;
void export_Block(py::module& m) {
py::class_<Block>(m, "Block", "板块类,可视为证券的容器")
.def(py::init<>())
.def(py::init<const string&, const string&>())
.def(py::init<const Block&>())
.def("__str__", to_py_str<Block>)
.def("__repr__", to_py_str<Block>)
.def_property("category", setCategory, getCategory, "板块所属分类")
.def_property("name", getName, setName, "板块名称")
.def_property("index_stock", &Block::getIndexStock, &Block::setIndexStock,
py::return_value_policy::copy, "对应指数")
.def("empty", &Block::empty, R"(empty(self)
)")
.def("add", add_1, R"(add(self, stock)
:param Stock stock:
:return:
:rtype: bool)",
py::keep_alive<1, 2>())
.def("add", add_2, R"(add(self, market_code)
"市场简称证券代码"
:param str market_code:
:return:
:rtype: bool)",
py::keep_alive<1, 2>())
.def("remove", remove_1, R"(remove(self, stock)
:param Stock stock:
:return:
:rtype: bool)")
.def("remove", remove_2, R"(remove(market_code)
:param str market_code:
:return: True | False
:rtype: bool)")
.def("clear", &Block::clear, "移除包含的所有证券")
.def("__len__", &Block::size, "包含的证券数量")
.def("__getitem__", &Block::get, R"(__getitem__(self, market_code)
:param str market_code:
:return: Stock )")
.def(
"__iter__",
[](const Block& blk) {
return py::make_iterator<py::return_value_policy::reference_internal, StockMapIterator,
StockMapIterator, const Stock&>(blk.begin(), blk.end());
},
py::keep_alive<0, 1>())
.def(
"get_stock_list",
[](const Block& self, py::object filter) {
StockList ret;
if (filter.is_none()) {
ret = self.getStockList();
} else {
HKU_CHECK(py::hasattr(filter, "__call__"), "filter not callable!");
py::object filter_func = filter.attr("__call__");
ret = self.getStockList(
[&](const Stock& stk) { return filter_func(stk).cast<bool>(); });
}
return ret;
},
py::arg("filter") = py::none(), R"(get_stock_list(self[, filter=None])
:param func filter: stock, True | False )")
DEF_PICKLE(Block);
}