我正在为c ++类编写一个接受文件指针的python绑定-
test <- data.table(col1 = c("cow, pig, horse"), col2 = c("fish, aardvark, moose"))
test2 <- data.table(col1 = c("orange, pig, frog"), col2 = c("whale, aardvark, elk"))
test <- rbind(test, test2)
cols <- c("col1", "col2")
test[, (cols) := (lapply(.SD, function(x) {
paste(sort(trimws(strsplit(x, ',')[[1]])), collapse=',')
})), .SDcols = cols]
我正在这样调用c ++函数-
PYBIND11_MODULE(pywrapper, m) {
...
py::class_<Dog, Animal>(m, "Dog")
.def(py::init<FILE * const>());
}
我收到了预期的错误-
f = open("test.log","w")
c = Dog(f)
如何在此处编写构造函数的包装?
答案 0 :(得分:1)
我相信pybind11中没有实现 input 缓冲区。这是 output 缓冲区https://github.com/pybind/pybind11/blob/master/include/pybind11/iostream.h#L24
的实现以下是使用缓冲区作为 output 流的示例:
.def("read_from_file_like_object",
[](MyClass&, py::object fileHandle) {
if (!(py::hasattr(fileHandle,"write") &&
py::hasattr(fileHandle,"flush") )){
throw py::type_error("MyClass::read_from_file_like_object(file): incompatible function argument: `file` must be a file-like object, but `"
+(std::string)(py::repr(fileHandle))+"` provided"
);
}
py::detail::pythonbuf buf(fileHandle);
std::ostream stream(&buf);
//... use the stream
},
py::arg("buf")
)