Turn Wrapper to singleton
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
include_directories(${Python3_INCLUDE_DIRS})
|
include_directories(${Python3_INCLUDE_DIRS})
|
||||||
|
|
||||||
add_executable(main main.cc PyWrap.cc)
|
add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc)
|
||||||
|
|
||||||
target_link_libraries(main ${Python3_LIBRARIES})
|
target_link_libraries(main ${Python3_LIBRARIES})
|
||||||
|
21
src/PyClassifier.cc
Normal file
21
src/PyClassifier.cc
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "PyClassifier.h"
|
||||||
|
|
||||||
|
namespace pywrap {
|
||||||
|
|
||||||
|
PyClassifier::PyClassifier(const std::string& module, const std::string& className) : module(module), className(className)
|
||||||
|
{
|
||||||
|
pyWrap = PyWrap::GetInstance();
|
||||||
|
pyWrap->importClass(module, className);
|
||||||
|
}
|
||||||
|
|
||||||
|
PyClassifier::~PyClassifier()
|
||||||
|
{
|
||||||
|
pyWrap->clean(module, className);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PyClassifier::callMethod(const std::string& method)
|
||||||
|
{
|
||||||
|
pyWrap->callMethod(module, className, method);
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* namespace PyWrap */
|
19
src/PyClassifier.h
Normal file
19
src/PyClassifier.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef PYCLASSIFER_H
|
||||||
|
#define PYCLASSIFER_H
|
||||||
|
#include <string>
|
||||||
|
#include "PyWrap.h"
|
||||||
|
|
||||||
|
namespace pywrap {
|
||||||
|
class PyClassifier {
|
||||||
|
public:
|
||||||
|
PyClassifier(const std::string& module, const std::string& className);
|
||||||
|
virtual ~PyClassifier();
|
||||||
|
void callMethod(const std::string& method);
|
||||||
|
private:
|
||||||
|
PyWrap* pyWrap;
|
||||||
|
std::string module;
|
||||||
|
std::string className;
|
||||||
|
};
|
||||||
|
|
||||||
|
} /* namespace pywrap */
|
||||||
|
#endif /* PYCLASSIFER_H */
|
211
src/PyWrap.cc
211
src/PyWrap.cc
@@ -5,14 +5,36 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
namespace pywrap {
|
namespace pywrap {
|
||||||
PyWrap::PyWrap(const std::string& moduleName, const std::string& className)
|
PyWrap* PyWrap::wrapper = nullptr;
|
||||||
|
|
||||||
|
PyWrap* PyWrap::GetInstance()
|
||||||
|
{
|
||||||
|
if (wrapper == nullptr) {
|
||||||
|
std::cout << "Creando instancia" << std::endl;
|
||||||
|
wrapper = new PyWrap();
|
||||||
|
std::cout << "Instancia creada" << std::endl;
|
||||||
|
}
|
||||||
|
return wrapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
PyWrap::PyWrap()
|
||||||
{
|
{
|
||||||
PyStatus status = initPython();
|
PyStatus status = initPython();
|
||||||
if (PyStatus_Exception(status)) {
|
if (PyStatus_Exception(status)) {
|
||||||
throw std::runtime_error("Error initializing Python");
|
throw std::runtime_error("Error initializing Python");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void PyWrap::importClass(const std::string& moduleName, const std::string& className)
|
||||||
|
{
|
||||||
|
std::cout << "Importando clase" << std::endl;
|
||||||
|
auto result = moduleClassMap.find({ moduleName, className });
|
||||||
|
if (result != moduleClassMap.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::cout << "No estaba en el mapa" << std::endl;
|
||||||
module = PyImport_ImportModule(moduleName.c_str());
|
module = PyImport_ImportModule(moduleName.c_str());
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
errorAbort("Could't import module " + moduleName);
|
errorAbort("Could't import module " + moduleName);
|
||||||
@@ -25,105 +47,132 @@ namespace pywrap {
|
|||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
errorAbort("Couldn't create instance of class " + className);
|
errorAbort("Couldn't create instance of class " + className);
|
||||||
}
|
}
|
||||||
|
moduleClassMap[{moduleName, className}] = { module, classObject, instance };
|
||||||
|
std::cout << "Clase importada" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PyWrap::clean(const std::string& moduleName, const std::string& className)
|
||||||
|
{
|
||||||
|
std::cout << "Limpiando" << std::endl;
|
||||||
|
auto result = moduleClassMap.find({ moduleName, className });
|
||||||
|
if (result == moduleClassMap.end()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::cout << "--> Limpiando" << std::endl;
|
||||||
|
Py_DECREF(std::get<0>(result->second));
|
||||||
|
Py_DECREF(std::get<1>(result->second));
|
||||||
|
Py_DECREF(std::get<2>(result->second));
|
||||||
|
moduleClassMap.erase(result);
|
||||||
|
std::cout << "Limpieza terminada" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
PyWrap::~PyWrap()
|
PyWrap::~PyWrap()
|
||||||
{
|
{
|
||||||
Py_DECREF(instance);
|
for (const auto& item : moduleClassMap) {
|
||||||
Py_DECREF(classObject);
|
Py_DECREF(std::get<0>(item.second));
|
||||||
Py_DECREF(module);
|
Py_DECREF(std::get<1>(item.second));
|
||||||
|
Py_DECREF(std::get<2>(item.second));
|
||||||
|
}
|
||||||
Py_Finalize();
|
Py_Finalize();
|
||||||
}
|
}
|
||||||
void PyWrap::errorAbort(const string& message)
|
void PyWrap::errorAbort(const std::string& message)
|
||||||
{
|
{
|
||||||
cerr << message << endl;
|
std::cerr << message << std::endl;
|
||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
void PyWrap::callMethod(const string& method)
|
void PyWrap::callMethod(const std::string& moduleName, const std::string& className, const std::string& method)
|
||||||
{
|
{
|
||||||
|
std::cout << "Llamando método" << std::endl;
|
||||||
|
auto item = moduleClassMap.find({ moduleName, className });
|
||||||
|
if (item == moduleClassMap.end()) {
|
||||||
|
errorAbort("Module " + moduleName + " and class " + className + " not found");
|
||||||
|
}
|
||||||
|
std::cout << "Clase encontrada" << std::endl;
|
||||||
|
instance = std::get<2>(item->second);
|
||||||
PyObject* result;
|
PyObject* result;
|
||||||
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
||||||
errorAbort("Couldn't call method " + method);
|
errorAbort("Couldn't call method " + method);
|
||||||
cout << "Result: " << PyUnicode_AsUTF8(result) << endl;
|
std::cout << "Result: " << PyUnicode_AsUTF8(result) << std::endl;
|
||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
}
|
}
|
||||||
void PyWrap::doCommand2()
|
// void PyWrap::doCommand2()
|
||||||
{
|
// {
|
||||||
PyObject* list = Py_BuildValue("[s]", "Stree");
|
// PyObject* list = Py_BuildValue("[s]", "Stree");
|
||||||
// PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list);
|
// // PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list);
|
||||||
PyObject* module = PyImport_ImportModule("stree");
|
// PyObject* module = PyImport_ImportModule("stree");
|
||||||
if (PyErr_Occurred()) {
|
|
||||||
PyErr_Print();
|
|
||||||
cout << "Fails to obtain the module.\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
cout << "Antes de empezar" << endl;
|
|
||||||
if (module != nullptr) {
|
|
||||||
cout << "Lo consiguió!!!" << endl;
|
|
||||||
// dict is a borrowed reference.
|
|
||||||
auto pdict = PyModule_GetDict(module);
|
|
||||||
if (pdict == nullptr) {
|
|
||||||
cout << "Fails to get the dictionary.\n";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Py_DECREF(module);
|
|
||||||
PyObject* pKeys = PyDict_Keys(pdict);
|
|
||||||
PyObject* pValues = PyDict_Values(pdict);
|
|
||||||
map<string, string> my_map;
|
|
||||||
cout << "size: " << PyDict_Size(pdict) << endl;
|
|
||||||
char* cstr_key = new char[100];
|
|
||||||
char* cstr_value = new char[500];
|
|
||||||
for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) {
|
|
||||||
PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key);
|
|
||||||
PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value);
|
|
||||||
//cout << cstr<< " "<< cstr2 <<endl;
|
|
||||||
my_map.emplace(cstr_key, cstr_value);
|
|
||||||
}
|
|
||||||
for (auto x : my_map) {
|
|
||||||
cout << x.first << " : " << x.second << endl;
|
|
||||||
}
|
|
||||||
// Builds the name of a callable class
|
|
||||||
const char* class_name = "Stree";
|
|
||||||
auto python_class = PyDict_GetItemString(pdict, class_name);
|
|
||||||
// if (PyErr_Occurred()) {
|
// if (PyErr_Occurred()) {
|
||||||
// PyErr_Print();
|
// PyErr_Print();
|
||||||
// cout << "Fails to obtain the class.\n";
|
// cout << "Fails to obtain the module.\n";
|
||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
if (python_class == nullptr) {
|
// cout << "Antes de empezar" << endl;
|
||||||
cout << "Fails to get the Python class.\n";
|
// if (module != nullptr) {
|
||||||
return;
|
// cout << "Lo consiguió!!!" << endl;
|
||||||
}
|
// // dict is a borrowed reference.
|
||||||
Py_DECREF(pdict);
|
// auto pdict = PyModule_GetDict(module);
|
||||||
cout << "Clase: " << python_class << endl;
|
// if (pdict == nullptr) {
|
||||||
PyObject* object;
|
// cout << "Fails to get the dictionary.\n";
|
||||||
// Creates an instance of the class
|
// return;
|
||||||
if (PyCallable_Check(python_class)) {
|
// }
|
||||||
cout << "Es callable" << endl;
|
// Py_DECREF(module);
|
||||||
object = PyObject_CallObject(python_class, NULL);
|
// PyObject* pKeys = PyDict_Keys(pdict);
|
||||||
Py_DECREF(python_class);
|
// PyObject* pValues = PyDict_Values(pdict);
|
||||||
} else {
|
// map<string, string> my_map;
|
||||||
std::cout << "Cannot instantiate the Python class" << endl;
|
// cout << "size: " << PyDict_Size(pdict) << endl;
|
||||||
Py_DECREF(python_class);
|
// char* cstr_key = new char[100];
|
||||||
return;
|
// char* cstr_value = new char[500];
|
||||||
}
|
// for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) {
|
||||||
if (PyErr_Occurred()) {
|
// PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key);
|
||||||
PyErr_Print();
|
// PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value);
|
||||||
cout << "Fails to create the Python object.\n";
|
// //cout << cstr<< " "<< cstr2 <<endl;
|
||||||
return;
|
// my_map.emplace(cstr_key, cstr_value);
|
||||||
}
|
// }
|
||||||
auto val = PyObject_CallMethod(object, "version", NULL);
|
// for (auto x : my_map) {
|
||||||
if (val != nullptr) {
|
// cout << x.first << " : " << x.second << endl;
|
||||||
cout << "Valor: " << val << endl;
|
// }
|
||||||
} else {
|
// // Builds the name of a callable class
|
||||||
cout << "No se pudo ejecutar" << endl;
|
// const char* class_name = "Stree";
|
||||||
}
|
// auto python_class = PyDict_GetItemString(pdict, class_name);
|
||||||
|
// // if (PyErr_Occurred()) {
|
||||||
|
// // PyErr_Print();
|
||||||
|
// // cout << "Fails to obtain the class.\n";
|
||||||
|
// // return;
|
||||||
|
// // }
|
||||||
|
// if (python_class == nullptr) {
|
||||||
|
// cout << "Fails to get the Python class.\n";
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// Py_DECREF(pdict);
|
||||||
|
// cout << "Clase: " << python_class << endl;
|
||||||
|
// PyObject* object;
|
||||||
|
// // Creates an instance of the class
|
||||||
|
// if (PyCallable_Check(python_class)) {
|
||||||
|
// cout << "Es callable" << endl;
|
||||||
|
// object = PyObject_CallObject(python_class, NULL);
|
||||||
|
// Py_DECREF(python_class);
|
||||||
|
// } else {
|
||||||
|
// std::cout << "Cannot instantiate the Python class" << endl;
|
||||||
|
// Py_DECREF(python_class);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (PyErr_Occurred()) {
|
||||||
|
// PyErr_Print();
|
||||||
|
// cout << "Fails to create the Python object.\n";
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// auto val = PyObject_CallMethod(object, "version", NULL);
|
||||||
|
// if (val != nullptr) {
|
||||||
|
// cout << "Valor: " << val << endl;
|
||||||
|
// } else {
|
||||||
|
// cout << "No se pudo ejecutar" << endl;
|
||||||
|
// }
|
||||||
|
|
||||||
} else {
|
// } else {
|
||||||
cout << "No lo consiguió :(" << endl;
|
// cout << "No lo consiguió :(" << endl;
|
||||||
}
|
// }
|
||||||
Py_RunMain();
|
// Py_RunMain();
|
||||||
}
|
// }
|
||||||
|
|
||||||
PyStatus PyWrap::initPython()
|
PyStatus PyWrap::initPython()
|
||||||
{
|
{
|
||||||
|
18
src/PyWrap.h
18
src/PyWrap.h
@@ -2,17 +2,29 @@
|
|||||||
#define PYWRAP_H
|
#define PYWRAP_H
|
||||||
#include <Python.h>
|
#include <Python.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
#include <tuple>
|
||||||
|
|
||||||
namespace pywrap {
|
namespace pywrap {
|
||||||
|
/*
|
||||||
|
Singleton class to handle Python interpreter.
|
||||||
|
*/
|
||||||
class PyWrap {
|
class PyWrap {
|
||||||
public:
|
public:
|
||||||
PyWrap(const std::string& moduleName, const std::string& className);
|
PyWrap(PyWrap& other) = delete;
|
||||||
|
static PyWrap* GetInstance();
|
||||||
|
void operator=(const PyWrap&) = delete;
|
||||||
~PyWrap();
|
~PyWrap();
|
||||||
void callMethod(const std::string& method);
|
void callMethod(const std::string& moduleName, const std::string& className, const std::string& method);
|
||||||
void doCommand2();
|
void importClass(const std::string& moduleName, const std::string& className);
|
||||||
|
void clean(const std::string& moduleName, const std::string& className);
|
||||||
|
// void doCommand2();
|
||||||
private:
|
private:
|
||||||
|
PyWrap();
|
||||||
void errorAbort(const std::string& message);
|
void errorAbort(const std::string& message);
|
||||||
PyStatus initPython();
|
PyStatus initPython();
|
||||||
|
static PyWrap* wrapper;
|
||||||
|
std::map<std::pair<std::string, std::string>, std::tuple<PyObject*, PyObject*, PyObject*>> moduleClassMap;
|
||||||
PyObject* module;
|
PyObject* module;
|
||||||
PyObject* classObject;
|
PyObject* classObject;
|
||||||
PyObject* instance;
|
PyObject* instance;
|
||||||
|
10
src/STree.cc
Normal file
10
src/STree.cc
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "STree.h"
|
||||||
|
|
||||||
|
namespace pywrap {
|
||||||
|
|
||||||
|
void STree::version()
|
||||||
|
{
|
||||||
|
callMethod("version");
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* namespace pywrap */
|
16
src/STree.h
Normal file
16
src/STree.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef STREE_H
|
||||||
|
#define STREE_H
|
||||||
|
#include "PyClassifier.h"
|
||||||
|
|
||||||
|
namespace pywrap {
|
||||||
|
class STree : public PyClassifier {
|
||||||
|
public:
|
||||||
|
STree() : PyClassifier("stree", "Stree") {};
|
||||||
|
~STree() = default;
|
||||||
|
void version();
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} /* namespace pywrap */
|
||||||
|
#endif /* STREE_H */
|
10
src/SVC.cc
Normal file
10
src/SVC.cc
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include "SVC.h"
|
||||||
|
|
||||||
|
namespace pywrap {
|
||||||
|
|
||||||
|
void SVC::version()
|
||||||
|
{
|
||||||
|
callMethod("_repr_html_");
|
||||||
|
}
|
||||||
|
|
||||||
|
} /* namespace pywrap */
|
16
src/SVC.h
Normal file
16
src/SVC.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef SVC_H
|
||||||
|
#define SVC_H
|
||||||
|
#include "PyClassifier.h"
|
||||||
|
|
||||||
|
namespace pywrap {
|
||||||
|
class SVC : public PyClassifier {
|
||||||
|
public:
|
||||||
|
SVC() : PyClassifier("sklearn.svm", "SVC") {};
|
||||||
|
~SVC() = default;
|
||||||
|
void version();
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
} /* namespace pywrap */
|
||||||
|
#endif /* STREE_H */
|
10
src/main.cc
10
src/main.cc
@@ -1,10 +1,14 @@
|
|||||||
#include "PyWrap.h"
|
#include "STree.h"
|
||||||
|
#include "SVC.h"
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
// auto wrap = pywrap::PyWrap("stree", "Stree");
|
// auto wrap = pywrap::PyWrap("stree", "Stree");
|
||||||
// wrap.callMethod("version");
|
// wrap.callMethod("version");
|
||||||
auto wrap2 = pywrap::PyWrap("sklearn.svm", "SVC");
|
auto stree = pywrap::STree();
|
||||||
wrap2.callMethod("_repr_html_");
|
stree.version();
|
||||||
|
auto svc = pywrap::SVC();
|
||||||
|
svc.version();
|
||||||
|
stree.version();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Reference in New Issue
Block a user