First Functional module
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
# PyWrap
|
||||
|
||||
Python Wrapper to use Python classifiers from C++
|
||||
Python Wrapper to use Python classifiers from C++
|
||||
|
||||
Explain issues with conda and libstdc++ and how to fix them
|
||||
(moving conda libstdc++ to a different folder)
|
||||
|
173
src/PyWrap.cc
173
src/PyWrap.cc
@@ -1,88 +1,139 @@
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
#include <stdexcept>
|
||||
#include "PyWrap.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
using namespace std;
|
||||
namespace pywrap {
|
||||
void PyWrap::doCommand(const std::string& command)
|
||||
PyWrap::PyWrap(const std::string& moduleName, const std::string& className)
|
||||
{
|
||||
auto status = initPython(command);
|
||||
PyStatus status = initPython();
|
||||
if (PyStatus_Exception(status)) {
|
||||
throw std::runtime_error("Error initializing Python");
|
||||
}
|
||||
PyObject* module = PyImport_ImportModule("stree.Stree");
|
||||
module = PyImport_ImportModule(moduleName.c_str());
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Could't import module " + moduleName);
|
||||
}
|
||||
classObject = PyObject_GetAttrString(module, className.c_str());
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Couldn't find class " + className);
|
||||
}
|
||||
instance = PyObject_CallObject(classObject, NULL);
|
||||
if (PyErr_Occurred()) {
|
||||
errorAbort("Couldn't create instance of class " + className);
|
||||
}
|
||||
}
|
||||
PyWrap::~PyWrap()
|
||||
{
|
||||
Py_DECREF(instance);
|
||||
Py_DECREF(classObject);
|
||||
Py_DECREF(module);
|
||||
Py_Finalize();
|
||||
}
|
||||
void PyWrap::errorAbort(const string& message)
|
||||
{
|
||||
cerr << message << endl;
|
||||
PyErr_Print();
|
||||
exit(1);
|
||||
}
|
||||
void PyWrap::callMethod(const string& method)
|
||||
{
|
||||
PyObject* result;
|
||||
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
||||
errorAbort("Couldn't call method " + method);
|
||||
cout << "Result: " << PyUnicode_AsUTF8(result) << endl;
|
||||
Py_DECREF(result);
|
||||
}
|
||||
void PyWrap::doCommand2()
|
||||
{
|
||||
PyObject* list = Py_BuildValue("[s]", "Stree");
|
||||
// PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list);
|
||||
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) {
|
||||
// // dict is a borrowed reference.
|
||||
// auto pdict = PyModule_GetDict(module);
|
||||
// if (pdict == nullptr) {
|
||||
// cout << "Fails to get the dictionary.\n";
|
||||
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()) {
|
||||
// PyErr_Print();
|
||||
// cout << "Fails to obtain the class.\n";
|
||||
// return;
|
||||
// }
|
||||
// //Py_DECREF(module);
|
||||
// PyObject* pKeys = PyDict_Keys(pdict);
|
||||
// PyObject* pValues = PyDict_Values(pdict);
|
||||
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;
|
||||
}
|
||||
|
||||
// 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;
|
||||
// }
|
||||
// system("pause");
|
||||
|
||||
// // Builds the name of a callable class
|
||||
// auto python_class = PyDict_GetItemString(pdict, "MyClass1");
|
||||
|
||||
// system("pause");
|
||||
// if (python_class == nullptr) {
|
||||
// cout << "Fails to get the Python class.\n";
|
||||
// return;
|
||||
// }
|
||||
// //Py_DECREF(pdict);
|
||||
|
||||
|
||||
// cout << python_class;
|
||||
// PyObject* object;
|
||||
|
||||
// // Creates an instance of the class
|
||||
// if (PyCallable_Check(python_class)) {
|
||||
// object = PyObject_CallObject(python_class, NULL);
|
||||
// Py_DECREF(python_class);
|
||||
// } else {
|
||||
// std::cout << "Cannot instantiate the Python class" << endl;
|
||||
// Py_DECREF(python_class);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// auto val = PyObject_CallMethod(object, "is_jit_model_available", NULL);
|
||||
// if (!val)
|
||||
// cout << "error!";
|
||||
|
||||
// cout << val;
|
||||
} else {
|
||||
cout << "No lo consiguió :(" << endl;
|
||||
}
|
||||
Py_RunMain();
|
||||
}
|
||||
|
||||
PyStatus PyWrap::initPython(const std::string& command)
|
||||
PyStatus PyWrap::initPython()
|
||||
{
|
||||
PyStatus status;
|
||||
|
||||
PyConfig config;
|
||||
PyConfig_InitPythonConfig(&config);
|
||||
config.isolated = 0;
|
||||
wchar_t* commandCoded = Py_DecodeLocale(command.c_str(), NULL);
|
||||
config.run_command = commandCoded;
|
||||
status = PyConfig_Read(&config);
|
||||
if (PyStatus_Exception(status)) {
|
||||
throw std::runtime_error("Error reading config");
|
||||
errorAbort("Error reading config");
|
||||
}
|
||||
status = Py_InitializeFromConfig(&config);
|
||||
PyConfig_Clear(&config);
|
||||
|
14
src/PyWrap.h
14
src/PyWrap.h
@@ -6,12 +6,16 @@
|
||||
namespace pywrap {
|
||||
class PyWrap {
|
||||
public:
|
||||
PyWrap() = default;
|
||||
~PyWrap() = default;
|
||||
void doCommand(const std::string& command);
|
||||
PyWrap(const std::string& moduleName, const std::string& className);
|
||||
~PyWrap();
|
||||
void callMethod(const std::string& method);
|
||||
void doCommand2();
|
||||
private:
|
||||
PyStatus initPython(const std::string& command);
|
||||
|
||||
void errorAbort(const std::string& message);
|
||||
PyStatus initPython();
|
||||
PyObject* module;
|
||||
PyObject* classObject;
|
||||
PyObject* instance;
|
||||
};
|
||||
} /* namespace python */
|
||||
#endif /* PYWRAP_H */
|
@@ -2,7 +2,9 @@
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
auto wrap = pywrap::PyWrap();
|
||||
wrap.doCommand("from stree import Stree; print(Stree().version())");
|
||||
// auto wrap = pywrap::PyWrap("stree", "Stree");
|
||||
// wrap.callMethod("version");
|
||||
auto wrap2 = pywrap::PyWrap("sklearn.svm", "SVC");
|
||||
wrap2.callMethod("_repr_html_");
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user