-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_python.cpp
More file actions
65 lines (51 loc) · 1.87 KB
/
Copy pathtest_python.cpp
File metadata and controls
65 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <Python.h>
#include <iostream>
int main() {
std::cout << "Initializing Python..." << std::endl;
Py_Initialize();
if (!Py_IsInitialized()) {
std::cerr << "Failed to initialize Python" << std::endl;
return -1;
}
std::cout << "Adding current directory to sys.path..." << std::endl;
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.insert(0, '.')");
std::cout << "Importing fuzzy_module..." << std::endl;
PyObject* pName = PyUnicode_DecodeFSDefault("fuzzy_module");
PyObject* pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (!pModule) {
std::cerr << "Failed to import module" << std::endl;
PyErr_Print();
return -1;
}
std::cout << "Getting compute_quality function..." << std::endl;
PyObject* pFunc = PyObject_GetAttrString(pModule, "compute_quality");
if (!pFunc || !PyCallable_Check(pFunc)) {
std::cerr << "Function not found or not callable" << std::endl;
PyErr_Print();
return -1;
}
std::cout << "Calling compute_quality(60, 60, 50, 50, 50)..." << std::endl;
PyObject* pArgs = PyTuple_New(5);
PyTuple_SetItem(pArgs, 0, PyFloat_FromDouble(60.0));
PyTuple_SetItem(pArgs, 1, PyFloat_FromDouble(60.0));
PyTuple_SetItem(pArgs, 2, PyFloat_FromDouble(50.0));
PyTuple_SetItem(pArgs, 3, PyFloat_FromDouble(50.0));
PyTuple_SetItem(pArgs, 4, PyFloat_FromDouble(50.0));
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (!pResult) {
std::cerr << "Function call failed" << std::endl;
PyErr_Print();
return -1;
}
long result = PyLong_AsLong(pResult);
Py_DECREF(pResult);
std::cout << "Result: " << result << std::endl;
std::cout << "SUCCESS!" << std::endl;
Py_XDECREF(pFunc);
Py_DECREF(pModule);
Py_Finalize();
return 0;
}