pybind.cc 3.9 KB
Newer Older
S
superjom 已提交
1 2
#include <ctype.h>
#include <pybind11/pybind11.h>
S
superjom 已提交
3
#include <pybind11/stl.h>
S
superjom 已提交
4

S
superjom 已提交
5
#include "visualdl/logic/sdk.h"
S
superjom 已提交
6 7 8 9

namespace py = pybind11;
namespace vs = visualdl;

Q
Qiao Longfei 已提交
10 11 12
PYBIND11_PLUGIN(core) {
  py::module m("core", "C++ core of VisualDL");
  //  m.doc() = "visualdl python core API";
S
superjom 已提交
13 14 15 16 17 18 19 20 21

  py::class_<vs::TabletHelper>(m, "Tablet")
      // other member setter and getter
      .def("record_buffer", &vs::TabletHelper::record_buffer)
      .def("records_size", &vs::TabletHelper::records_size)
      .def("buffer", &vs::TabletHelper::buffer)
      .def("human_readable_buffer", &vs::TabletHelper::human_readable_buffer)
      .def("set_buffer",
           (void (vs::TabletHelper::*)(const std::string&)) &
S
superjom 已提交
22 23 24
               vs::TabletHelper::SetBuffer)
      // scalar interface
      .def("as_int32_scalar",
S
Superjom 已提交
25 26
           [](vs::TabletHelper& self, vs::ImHelper& im) {
             return vs::components::ScalarHelper<int32_t>(self, &im.handler());
S
superjom 已提交
27 28
           })
      .def("as_int64_scalar",
S
Superjom 已提交
29 30 31
           [](vs::TabletHelper& self, vs::ImHelper& im) {
             return vs::components::ScalarHelper<int64_t>(&self.data(),
                                                          &im.handler());
S
superjom 已提交
32 33
           })
      .def("as_float_scalar",
S
Superjom 已提交
34 35 36
           [](vs::TabletHelper& self, vs::ImHelper& im) {
             return vs::components::ScalarHelper<float>(&self.data(),
                                                        &im.handler());
S
superjom 已提交
37
           })
S
Superjom 已提交
38 39 40
      .def("as_double_scalar", [](vs::TabletHelper& self, vs::ImHelper& im) {
        return vs::components::ScalarHelper<double>(&self.data(),
                                                    &im.handler());
S
superjom 已提交
41
      });
S
superjom 已提交
42 43

  py::class_<vs::StorageHelper>(m, "Storage")
S
debug  
superjom 已提交
44
      .def("tags", &vs::StorageHelper::tags)
S
superjom 已提交
45 46
      .def("timestamp", &vs::StorageHelper::timestamp)
      .def("dir", &vs::StorageHelper::dir)
S
superjom 已提交
47
      .def("set_dir", &vs::StorageHelper::SetDir)
S
superjom 已提交
48 49 50 51 52 53 54 55
      .def("tablets_size", &vs::StorageHelper::tablets_size)
      .def("buffer", &vs::StorageHelper::buffer)
      .def("human_readable_buffer", &vs::StorageHelper::human_readable_buffer)
      .def("set_buffer",
           (void (vs::StorageHelper::*)(const std::string&)) &
               vs::StorageHelper::SetBuffer);

  py::class_<vs::ImHelper>(m, "Im")
S
Superjom 已提交
56 57
      .def("__init__",
           [](vs::ImHelper& instance) { new (&instance) vs::ImHelper(); })
S
superjom 已提交
58 59
      .def("storage", &vs::ImHelper::storage)
      .def("tablet", &vs::ImHelper::tablet)
S
superjom 已提交
60 61
      .def("add_tablet", &vs::ImHelper::AddTablet)
      .def("persist_to_disk", &vs::ImHelper::PersistToDisk)
S
Superjom 已提交
62 63 64 65 66 67 68 69 70 71
      .def("clear_tablets", &vs::ImHelper::ClearTablets)
      .def("start_read_service",
           &vs::ImHelper::StartReadService,
           "start a thread to maintain read service")
      .def("start_write_service",
           &vs::ImHelper::StartWriteSerice,
           "start a thread to maintain write service")
      .def("stop_service",
           &vs::ImHelper::StopService,
           "stop the service thread");
S
superjom 已提交
72

S
Superjom 已提交
73
// interfaces for components begin
S
superjom 已提交
74

S
Superjom 已提交
75
// different data type of scalar conponent
S
superjom 已提交
76 77 78 79 80 81 82
#define ADD_SCALAR_TYPED_INTERFACE(T, name__)                             \
  py::class_<vs::components::ScalarHelper<T>>(m, #name__)                 \
      .def("add_record", &vs::components::ScalarHelper<T>::AddRecord)     \
      .def("set_captions", &vs::components::ScalarHelper<T>::SetCaptions) \
      .def("get_records", &vs::components::ScalarHelper<T>::GetRecords)   \
      .def("get_captions", &vs::components::ScalarHelper<T>::GetCaptions) \
      .def("get_ids", &vs::components::ScalarHelper<T>::GetIds)           \
S
superjom 已提交
83
      .def("get_record_size", &vs::components::ScalarHelper<T>::GetSize)  \
S
superjom 已提交
84 85 86 87 88 89
      .def("get_timestamps", &vs::components::ScalarHelper<T>::GetTimestamps);
  ADD_SCALAR_TYPED_INTERFACE(int32_t, ScalarInt32);
  ADD_SCALAR_TYPED_INTERFACE(int64_t, ScalarInt64);
  ADD_SCALAR_TYPED_INTERFACE(float, ScalarFloat);
  ADD_SCALAR_TYPED_INTERFACE(double, ScalarDouble);
#undef ADD_SCALAR_TYPED_INTERFACE
S
superjom 已提交
90
}