1 | /* Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | #include "absl/strings/string_view.h" |
17 | #include "pybind11/pybind11.h" |
18 | #include "pybind11/pytypes.h" |
19 | #include "tensorflow/core/util/events_writer.h" |
20 | #include "tensorflow/python/lib/core/pybind11_absl.h" |
21 | #include "tensorflow/python/lib/core/pybind11_proto.h" |
22 | #include "tensorflow/python/lib/core/pybind11_status.h" |
23 | |
24 | namespace py = pybind11; |
25 | |
26 | PYBIND11_MODULE(_pywrap_events_writer, m) { |
27 | py::class_<tensorflow::EventsWriter> events_writer_class(m, "EventsWriter" ); |
28 | events_writer_class.def(py::init<const std::string&>()) |
29 | .def("InitWithSuffix" , |
30 | [](tensorflow::EventsWriter& self, const std::string& suffix) { |
31 | return self.InitWithSuffix(suffix); |
32 | }) |
33 | .def("FileName" , |
34 | [](tensorflow::EventsWriter& self) { return self.FileName(); }) |
35 | .def("_WriteSerializedEvent" , |
36 | [](tensorflow::EventsWriter& self, const std::string& event_str) { |
37 | self.WriteSerializedEvent(event_str); |
38 | }) |
39 | .def("Flush" , [](tensorflow::EventsWriter& self) { return self.Flush(); }) |
40 | .def("Close" , [](tensorflow::EventsWriter& self) { return self.Close(); }) |
41 | .def("WriteEvent" , |
42 | [](tensorflow::EventsWriter& self, const py::object obj) { |
43 | // Verify the proto type is an event prior to writing. |
44 | tensorflow::CheckProtoType(obj, "tensorflow.Event" ); |
45 | self.WriteSerializedEvent( |
46 | obj.attr("SerializeToString" )().cast<std::string>()); |
47 | }); |
48 | }; |
49 | |