当前位置: 首页 > news >正文

三星网站建设内容凌哥seo

三星网站建设内容,凌哥seo,蚌埠网站建设,wordpress分享文章插件环境: phyton版本:3.10 VS版本:VS2017 包含文件头:Python\Python310\include 包含库文件:Python\Python310\libs 程序运行期间,以下函数只需要调用一次即可,重复调用会导致崩溃 void Initial…

环境:

phyton版本:3.10

VS版本:VS2017


包含文件头:Python\Python310\include
包含库文件:Python\Python310\libs

程序运行期间,以下函数只需要调用一次即可,重复调用会导致崩溃
void Initialize();
void Finalize();

“C_Test.py”需要拷贝程序运行所有的目录。

下载:https://download.csdn.net/download/luo_sen/88094131

PythonHelper.h

#pragma once
#include<string>
using namespace std;
/*
phyton版本:3.10
包含文件头:Python\Python310\include
包含库文件:Python\Python310\libs
*/
// https://docs.python.org/3/c-api/arg.html
/*
b (int) [unsigned char]
Convert a nonnegative Python integer to an unsigned tiny int, stored in a C unsigned char.B (int) [unsigned char]
Convert a Python integer to a tiny int without overflow checking, stored in a C unsigned char.h (int) [short int]
Convert a Python integer to a C short int.H (int) [unsigned short int]
Convert a Python integer to a C unsigned short int, without overflow checking.i (int) [int]
Convert a Python integer to a plain C int.I (int) [unsigned int]
Convert a Python integer to a C unsigned int, without overflow checking.l (int) [long int]
Convert a Python integer to a C long int.k (int) [unsigned long]
Convert a Python integer to a C unsigned long without overflow checking.L (int) [long long]
Convert a Python integer to a C long long.K (int) [unsigned long long]
Convert a Python integer to a C unsigned long long without overflow checking.n (int) [Py_ssize_t]
Convert a Python integer to a C Py_ssize_t.c (bytes or bytearray of length 1) [char]
Convert a Python byte, represented as a bytes or bytearray object of length 1, to a C char.Changed in version 3.3: Allow bytearray objects.C (str of length 1) [int]
Convert a Python character, represented as a str object of length 1, to a C int.f (float) [float]
Convert a Python floating point number to a C float.d (float) [double]
Convert a Python floating point number to a C double.D (complex) [Py_complex]
Convert a Python complex number to a C Py_complex structure.
*/#define  PYTHON_FILE_NAME _T("C_Test")extern "C"
{
#include "Python.h"
}class CPythonHelper
{public:void			Initialize();void			Finalize();PyObject*		GetPyFunc(CString strModuleName, CString strFuncName);	PyObject*		RunPyFunc(CString strModuleName, CString strFuncName, PyObject *pArgs);int				PyTupleSetItem(PyObject *, Py_ssize_t, PyObject *);CString			PyObjectToCString(PyObject *pPyObj);int				PyObjectToInt(PyObject *pPyObj);double			PyObjectToFloat(PyObject *pPyObj);int				PyObjectToFloatArray(PyObject *pPyObj, CArray<double, double>& szData);PyObject*		IntToPyObject(int data);PyObject*		FloatToPyObject(double data);PyObject*       CStringToPyObject(CString data);PyObject*		FloatArrayToPyObject(CArray<double, double> &szData);PyObject*		CreateArgs(int size);protected:string			UTF8_To_string(const std::string & str);string			string_To_UTF8(const std::string & str);string			CStringToPyString(CString text);PyObject*		GetPyFunc(char* pModuleName, char* pFuncName);PyObject*		RunPyFunc(char* pModuleName, char* pFuncName, PyObject *pArgs);};

PythonHelper.cpp

#include "pch.h"
#include "PythonHelper.h"std::string CPythonHelper::UTF8_To_string(const std::string & str)
{int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 memset(pwBuf, 0, nwLen * 2 + 2);MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char * pBuf = new char[nLen + 1];memset(pBuf, 0, nLen + 1);WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr = pBuf;delete[]pBuf;delete[]pwBuf;pBuf = NULL;pwBuf = NULL;return retStr;
}
std::string CPythonHelper::string_To_UTF8(const std::string & str)
{int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);wchar_t * pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴 ZeroMemory(pwBuf, nwLen * 2 + 2);::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);char * pBuf = new char[nLen + 1];ZeroMemory(pBuf, nLen + 1);::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);std::string retStr(pBuf);delete[]pwBuf;delete[]pBuf;pwBuf = NULL;pBuf = NULL;return retStr;
}string CPythonHelper::CStringToPyString(CString text)
{string tt = CT2A(text);string strUTF8 = string_To_UTF8(tt);return strUTF8;
}CString CPythonHelper::PyObjectToCString(PyObject *pPyObj)
{PyObject* str = PyUnicode_AsEncodedString(pPyObj, "utf-8", "Error");char *result = (PyBytes_AsString(str));string temp = UTF8_To_string(result);CString strResult = CString(temp.c_str());return strResult;
}
int CPythonHelper::PyObjectToInt(PyObject *pPyObj)
{int data = 0;PyArg_Parse(pPyObj, "i", &data);return data;
}
double CPythonHelper::PyObjectToFloat(PyObject *pPyObj)
{double data = 0;PyArg_Parse(pPyObj, "d", &data);return data;
}int CPythonHelper::PyObjectToFloatArray(PyObject *pPyObj, CArray<double, double>& szData)
{szData.RemoveAll();int SizeOfList = PyList_Size(pPyObj);for (int i = 0; i < SizeOfList; i++){PyObject *Item = PyList_GetItem(pPyObj, i);double result=0;PyArg_Parse(Item, "d", &result);szData.Add(result);}return SizeOfList;}void CPythonHelper::Initialize()
{if (!Py_IsInitialized()){Py_Initialize();}}void CPythonHelper::Finalize()
{if (Py_IsInitialized()){Py_Finalize();}}
PyObject* CPythonHelper::GetPyFunc(CString strModuleName, CString strFuncName)
{string pModuleName = CStringToPyString(strModuleName);string pFuncName = CStringToPyString(strFuncName);return GetPyFunc((char*)pModuleName.c_str(), (char*)pFuncName.c_str());}PyObject* CPythonHelper::GetPyFunc(char* pModuleName, char* pFuncName)
{PyObject *pModule = PyImport_ImportModule(pModuleName);PyObject *pFunc = PyObject_GetAttrString(pModule, pFuncName);//Py_CLEAR(pModule);return pFunc;
}PyObject * CPythonHelper::RunPyFunc(char* pModuleName, char* pFuncName, PyObject *pArgs)
{PyObject *pFunc = GetPyFunc(pModuleName, pFuncName);PyObject *pRetrun = PyObject_CallObject(pFunc, pArgs);	//Py_CLEAR(pFunc);return pRetrun;
}
PyObject * CPythonHelper::RunPyFunc(CString strModuleName, CString strFuncName, PyObject *pArgs)
{string pModuleName = CStringToPyString(strModuleName);string pFuncName = CStringToPyString(strFuncName);PyObject *pRetrun = RunPyFunc((char*)pModuleName.c_str(), (char*)pFuncName.c_str(), pArgs);	return pRetrun;
}
int CPythonHelper::PyTupleSetItem(PyObject * pObject, Py_ssize_t index, PyObject * pData)
{return PyTuple_SetItem(pObject, index, pData);
}PyObject* CPythonHelper::IntToPyObject(int data)
{return Py_BuildValue("i", data);
}
PyObject* CPythonHelper::FloatToPyObject(double data)
{return Py_BuildValue("d", data);
}PyObject* CPythonHelper::FloatArrayToPyObject(CArray<double, double> &szData)
{int nSize = szData.GetSize();PyObject *PyList = PyList_New(nSize);for (int i = 0; i < PyList_Size(PyList); i++){PyList_SetItem(PyList, i, PyFloat_FromDouble(szData[i]));}return PyList;
}
PyObject* CPythonHelper::CStringToPyObject(CString data)
{return Py_BuildValue("s", (CStringToPyString(data).c_str())); }PyObject* CPythonHelper::CreateArgs(int size)
{return PyTuple_New(size);
}

C_Test.py

def Hello():print("hello Python")def Add(a,b):import numpy as npprint(np.pi)return a+bdef GetText(msg):print(msg)return  msg;

程序调用示例-整数(一):

CPythonHelper PY;PY.Initialize();int a = 10;int b = 20;int sum = 0;	//参数设置PyObject *pArgs = PyTuple_New(2);PY.PyTupleSetItem(pArgs, 0,Py_BuildValue("i", a));PY.PyTupleSetItem(pArgs, 1, Py_BuildValue("i", b));//调用函数PyObject *pRetrun = PY.RunPyFunc(PYTHON_FILE_NAME, _T("Add"), pArgs);//返回值转换PyArg_Parse(pRetrun, "i", &sum);//输出CString strResult = _T("");strResult.Format(_T("%d+%d=%d"), a, b, sum);AfxMessageBox(strResult);PY.Finalize();

程序调用示例-字符串(二)

  CPythonHelper PY;PY.Initialize();CString strText = _T("ABC中国人123");//参数设置PyObject *pArgs = PyTuple_New(1);PY.PyTupleSetItem(pArgs, 0, Py_BuildValue("s", PY.CStringToPyString(strText).c_str()));//调用函数PyObject *pRetrun = PY.RunPyFunc(PYTHON_FILE_NAME, _T("GetText"), pArgs);//返回值转换CString strResult = PY.PyObjectToCString(pRetrun);//输出AfxMessageBox(strResult);PY.Finalize();

程序调用示例-数组(三)

 

   CPythonHelper PY;PY.Initialize();CString strText = _T("ABC中国人123");//参数设置PyObject *pArgs = PY.CreateArgs(1);PyObject* pObject = PY.CStringToPyObject(strText);CArray<double, double> szData;szData.Add(1.0);szData.Add(1.1);szData.Add(2);PyObject *PyList = PY.FloatArrayToPyObject(szData);PY.PyTupleSetItem(pArgs, 0, PyList);//调用函数PyObject *pReturn = PY.RunPyFunc(PYTHON_FILE_NAME, _T("SetVaule"), pArgs);//返回值转换PY.PyObjectToFloatArray(pReturn, szData);PY.Finalize();

http://www.yayakq.cn/news/691730/

相关文章:

  • 网站建设是虚拟行业吗houzz室内设计app
  • 外贸俄罗斯俄语网站制作wordpress上传到哪个文件夹
  • 设计网站平台母婴网站 模板
  • 手机网站制作教程下载自己可以申请网站做外卖吗
  • 公司网站开发完成后怎么办网站设计建设公司怎么做
  • 宁波外贸网站做网站链接
  • 电商网站里的水果图片怎么做的智慧团建的网址
  • 加强网站建设的通知googleseo排名公司
  • 广州seo建站怎么建设分销模式手机网站
  • 公司做网站需要什么内容股票网站模板
  • 做可动模型的网站怎样进入国外网站
  • 做网站需要了解的知识常见的c2c平台有
  • 二级域名分发网站源码wordpress使用教学
  • 深圳做网站佰达科技三十什么是网络营销中最容易出现问题的步骤
  • filetype doc 网站建设苗木企业网站源码
  • 建站及推广新手怎么开婚庆公司
  • 用php做的网站论文wordpress 链接失效
  • 莞城区做网站俄语免费网站制作
  • 做公司+网站建设前端微信小程序开发教程
  • html官方下载徐州seo外包
  • 网站建设视频教程。企业在线培训系统
  • 辽宁网站开发wordpress 完整备份
  • 怎么用织梦源代码做网站五金制品东莞网站建设技术支持
  • 网站不设置关键词描述高端企业网站公司
  • 卡密网站怎么做的win7网站服务器制作软件
  • 网站开发大概要多少钱dedecms做网站和thinkphp
  • 济南招考院网站泰州网站建设专业团队
  • 郑州建设高端网站大型网站 建设意义
  • 湖南网站seo公司怎么做网站开发
  • 专业做外贸的网站临安区做网站的公司