pybind.cc 2.4 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

namespace py = pybind11;
namespace vs = visualdl;
S
superjom 已提交
9
namespace cp = visualdl::components;
S
superjom 已提交
10

Q
Qiao Longfei 已提交
11 12
PYBIND11_PLUGIN(core) {
  py::module m("core", "C++ core of VisualDL");
S
superjom 已提交
13

S
superjom 已提交
14 15 16 17
#define ADD_SCALAR(T)                                      \
  py::class_<cp::ScalarReader<T>>(m, "ScalarReader__" #T)  \
      .def("records", &cp::ScalarReader<T>::records)       \
      .def("timestamps", &cp::ScalarReader<T>::timestamps) \
S
superjom 已提交
18
      .def("ids", &cp::ScalarReader<T>::ids)               \
S
superjom 已提交
19
      .def("caption", &cp::ScalarReader<T>::caption);
S
superjom 已提交
20 21 22 23 24
  ADD_SCALAR(int);
  ADD_SCALAR(float);
  ADD_SCALAR(double);
  ADD_SCALAR(int64_t);
#undef ADD_SCALAR
S
superjom 已提交
25

S
superjom 已提交
26 27 28 29 30 31 32 33
#define ADD_SCALAR_WRITER(T)                          \
  py::class_<cp::Scalar<T>>(m, "ScalarWriter__" #T)   \
      .def("set_caption", &cp::Scalar<T>::SetCaption) \
      .def("add_record", &cp::Scalar<T>::AddRecord);
  ADD_SCALAR_WRITER(int);
  ADD_SCALAR_WRITER(float);
  ADD_SCALAR_WRITER(double);
#undef ADD_SCALAR_WRITER
S
superjom 已提交
34

S
superjom 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48
#define ADD_SCALAR(T)                                                   \
  .def("get_scalar_" #T, [](vs::Reader& self, const std::string& tag) { \
    auto tablet = self.tablet(tag);                                     \
    return vs::components::ScalarReader<T>(std::move(tablet));          \
  })
  py::class_<vs::Reader>(m, "Reader")
      .def(
          "__init__",
          [](vs::Reader& instance,
             const std::string& mode,
             const std::string& dir) { new (&instance) vs::Reader(mode, dir); })
      // clang-format off
    ADD_SCALAR(float)
    ADD_SCALAR(double)
S
superjom 已提交
49
    ADD_SCALAR(int);
S
superjom 已提交
50 51
// clang-format on
#undef ADD_SCALAR
S
superjom 已提交
52

S
superjom 已提交
53 54 55 56 57
#define ADD_SCALAR(T)                                                   \
  .def("new_scalar_" #T, [](vs::Writer& self, const std::string& tag) { \
    auto tablet = self.AddTablet(tag);                                  \
    return cp::Scalar<T>(tablet);                                       \
  })
S
superjom 已提交
58

S
superjom 已提交
59
  py::class_<vs::Writer>(m, "Writer")
S
superjom 已提交
60
      .def("__init__",
S
superjom 已提交
61
           [](vs::Writer& instance, const std::string& dir, int sync_cycle) {
S
superjom 已提交
62 63 64 65
             new (&instance) vs::Writer(dir);
             instance.storage().meta.cycle = sync_cycle;
           })
      .def("as_mode", &vs::Writer::AsMode)
S
superjom 已提交
66
      // clang-format off
S
superjom 已提交
67 68 69
      ADD_SCALAR(float)
      ADD_SCALAR(double)
      ADD_SCALAR(int);
S
superjom 已提交
70 71 72 73
// clang-format on
#undef ADD_SCALAR

}  // end pybind