mlpack 3.4.2
print_class_defn.hpp
Go to the documentation of this file.
1
12#ifndef MLPACK_BINDINGS_PYTHON_PRINT_CLASS_DEFN_HPP
13#define MLPACK_BINDINGS_PYTHON_PRINT_CLASS_DEFN_HPP
14
15#include "strip_type.hpp"
16
17namespace mlpack {
18namespace bindings {
19namespace python {
20
25template<typename T>
27 util::ParamData& /* d */,
28 const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
29 const typename boost::disable_if<data::HasSerialize<T>>::type* = 0)
30{
31 // Do nothing.
32}
33
37template<typename T>
39 util::ParamData& /* d */,
40 const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
41{
42 // Do nothing.
43}
44
48template<typename T>
51 const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
52 const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
53{
54 // First, we have to parse the type. If we have something like, e.g.,
55 // 'LogisticRegression<>', we must convert this to 'LogisticRegression[].'
56 std::string strippedType, printedType, defaultsType;
57 StripType(d.cppType, strippedType, printedType, defaultsType);
58
82 std::cout << "cdef class " << strippedType << "Type:" << std::endl;
83 std::cout << " cdef " << printedType << "* modelptr" << std::endl;
84 std::cout << std::endl;
85 std::cout << " def __cinit__(self):" << std::endl;
86 std::cout << " self.modelptr = new " << printedType << "()" << std::endl;
87 std::cout << std::endl;
88 std::cout << " def __dealloc__(self):" << std::endl;
89 std::cout << " del self.modelptr" << std::endl;
90 std::cout << std::endl;
91 std::cout << " def __getstate__(self):" << std::endl;
92 std::cout << " return SerializeOut(self.modelptr, \"" << printedType
93 << "\")" << std::endl;
94 std::cout << std::endl;
95 std::cout << " def __setstate__(self, state):" << std::endl;
96 std::cout << " SerializeIn(self.modelptr, state, \"" << printedType
97 << "\")" << std::endl;
98 std::cout << std::endl;
99 std::cout << " def __reduce_ex__(self, version):" << std::endl;
100 std::cout << " return (self.__class__, (), self.__getstate__())"
101 << std::endl;
102 std::cout << std::endl;
103}
104
113template<typename T>
115 const void* /* input */,
116 void* /* output */)
117{
118 PrintClassDefn<typename std::remove_pointer<T>::type>(d);
119}
120
121} // namespace python
122} // namespace bindings
123} // namespace mlpack
124
125#endif
python
Definition: CMakeLists.txt:6
void StripType(const std::string &inputType, std::string &strippedType, std::string &printedType, std::string &defaultsType)
Given an input type like, e.g., "LogisticRegression<>", return three types that can be used in Python...
Definition: strip_type.hpp:28
void PrintClassDefn(util::ParamData &, const typename boost::disable_if< arma::is_arma_type< T > >::type *=0, const typename boost::disable_if< data::HasSerialize< T > >::type *=0)
Non-serializable models don't require any special definitions, so this prints nothing.
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:53
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84