У меня следующая проблема. Я хочу встроить этот Python API в мою программу на C ++ через API Python / C.
Как передать аргументы функции, указанной ниже? Особенно, что мне делать с этим специальным питоном ** kwargs ?
__init__
(имя пользователя, пароль, ** kwargs)
Параметры:
- имя пользователя - Логин имя пользователя
- пароль - Пароль для входа
- kwargs - см. Ниже
Аргументы по ключевым словам:
- auto_patch: исправление объектов API для соответствия общедоступному API. По умолчанию: False
- drop_incompat_key: удалить ключи объекта API, которых нет в публичном API. По умолчанию: False
- timeout: интервал ожидания в секундах. По умолчанию: 15
- api_url: переопределить базу URL api по умолчанию
- cookie: сохраненная строка cookie из предыдущего сеанса
- настройки: набор настроек из предыдущего сеанса
- on_login: обратный вызов после успешного входа в систему
- прокси: укажите прокси, например: ‘http://127.0.0.1:8888’ (ALPHA)
- proxy_handler: укажите свой собственный обработчик прокси
Из того, что я до сих пор читал, это будет правильно для первых двух аргументов:
#include <Python.h>
#include <iostream>
int main()
{
// Initialize the Python interpreter.
Py_Initialize();
// Create some Python objects that will later be assigned values.
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
// Convert the file name to a Python string.
pName = PyUnicode_FromString("Sample");
// Import the file as a Python module.
pModule = PyImport_Import(pName);
// Create a dictionary for the contents of the module.
pDict = PyModule_GetDict(pModule);
// Get the add method from the dictionary.
pFunc = PyDict_GetItemString(pDict, "__init__");
// Create a Python tuple to hold the arguments to the method.
pArgs = PyTuple_New(2);
// make arguments
PyObject *pValue1 = PyUnicode_FromString("IGusername");
PyObject *pValue2 = PyUnicode_FromString("IGpassword");
// Set the Python strings as the first and second arguments to the method.
PyTuple_SetItem(pArgs, 0, pValue);
PyTuple_SetItem(pArgs, 1, pValue);
// Call the function with the arguments.
PyObject* pResult = PyObject_CallObject(pFunc, pArgs);
// Print a message if calling the method failed.
if (pResult == NULL)
printf("Calling the add method failed.\n");
// Convert the result to a long from a Python object.
//#########how to get the returned string##############
long result = XXXXXXXXXXXXX(pResult);
// Destroy the Python interpreter.
Py_Finalize();
// Print the result.
printf("The result is %d.\n", result);
std::cin.ignore();
return 0;
}
Кроме того, как насчет возвращаемого значения?
Как бы получить возвращаемое значение, если бы это была строка?
Вот оно для int:
long result = PyLong_AsLong(pResult);