Python调用C库-数据传递(数组、指针)

    Python调用C库函数做计算,出现需要传递多维数组数据时,可以利用多级指针完成数据的传递。在下面示例中,实现了二级和三级指针数据的传递:

   C库函数实现数据相加功能,编译库为CDllForPython.dll

extern "C"
{
	int CDll_API DoAddPP(int n, int* ni, int** data);

	int CDll_API DoAddPPP(int n, int* ni, int** nij, int*** data);
}


int DoAddPP(int n, int* ni, int** data)
{
	int res = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < ni[i]; j++)
		{
			int value = data[i][j];
			res = res + value;
		}
	}
	return res;
}

int DoAddPPP(int n, int* ni, int** nij, int*** data)
{
	int res = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < ni[i]; j++)
		{
			for (int k = 0; k < nij[i][j]; k++)
			{
				int value = data[i][j][k];
				res = res + value;
			}
		}
	}
	return res;
}

    Python实现数组转为二级指针和三级指针,调用C库函数

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import platform
import ctypes


class CApi:
    # 初始化类
    def __init__(self):
        if platform.system() == "Windows":
            self.cdll = ctypes.cdll.LoadLibrary("./CDllForPython.dll")
        elif platform.system() == "Linux":
            self.cdll = ctypes.cdll.LoadLibrary("./CDllForPython.so")

    # 向C库函数传递二级指针
    # @param n    代表ni的个数   =一维
    # @param ni   代表data的个数 =二维
    # @param data 数据
    # return res   计算结果
    def set_params_twos(self, n, ni, data):
        c_n = ctypes.c_int()
        c_n.value = n

        ni_type = ctypes.c_int * n
        c_ni = ni_type()
        for i in range(0, int(n)):
            c_ni[i] = ni[i]

        # 转换二级指针
        c_data = []
        for i in range(0, int(n)):
            tmp_arr = (ctypes.c_int * int(ni[i]))()
            for j in range(0, int(ni[i])):
                tmp_arr[j] = data[i][j]
            c_data.append(ctypes.cast(tmp_arr, ctypes.POINTER(ctypes.c_int)))
        p_c_data = (ctypes.POINTER(ctypes.c_int) * n)(*c_data)
        res = self.cdll.DoAddPP(c_n, ctypes.byref(c_ni), ctypes.byref(p_c_data))
        return res

    # 向C库函数传递三级指针
    # @param n     代表ni的个数   =一维
    # @param ni    代表nij的个数  =二维
    # @param nij   代表data的个数 =三维
    # @param data  数据
    # return res   计算结果
    def set_params_threes(self, n, ni, nij, data):
        c_n = ctypes.c_int()
        c_n.value = n

        ni_type = ctypes.c_int * n
        c_ni = ni_type()
        for i in range(0, int(n)):
            c_ni[i] = ni[i]

        # 转换二级指针
        c_nij = []
        for i in range(0, int(n)):
            one_arr = (ctypes.c_int * int(ni[i]))()
            for j in range(0, int(ni[i])):
                one_arr[j] = nij[i][j]
            c_nij.append(ctypes.cast(one_arr, ctypes.POINTER(ctypes.c_int)))
        p_c_nij = (ctypes.POINTER(ctypes.c_int) * n)(*c_nij)

        # 转换三级指针
        p_type = ctypes.POINTER(ctypes.c_int)
        pp_type = ctypes.POINTER(ctypes.POINTER(ctypes.c_int))
        c_data = []
        for i in range(0, int(n)):
            one_arr1 = []
            for j in range(0, int(ni[i])):
                two_arr1 = (ctypes.c_int * int(nij[i][j]))()
                for k in range(0, int(nij[i][j])):
                    two_arr1[k] = data[i][j][k]
                one_arr1.append(ctypes.cast(two_arr1, p_type))
            p_one_arr1 = (p_type * ni[i])(*one_arr1)
            c_data.append(p_one_arr1)
        p_c_data = (pp_type * n)(*c_data)
        res = self.cdll.DoAddPPP(c_n, ctypes.byref(c_ni), ctypes.byref(p_c_nij), ctypes.byref(p_c_data))
        return res

    Python准备试验数据,并调用计算类

if __name__ == '__main__':
    obj = CApi()

    n_count = 2
    arr_n_i_data = [3, 3]
    arr_n_i_j_data = [[2, 2, 2], [2, 2, 2]]
    arr_data = [[[1, 1], [1, 1], [1, 1]], [[1, 1], [1, 1], [1, 1]]]
    res_v = obj.set_params_threes(n_count, arr_n_i_data, arr_n_i_j_data, arr_data)
    print(res_v)

注意:

    1 Python代码执行和C库编译的操作系统位数是否相同(32位或64位)

    2 Python导入C库时,注意C库所在目录。Python不同的版本寻找库的规则不同。

    3 C库函数编译要加上 extern "C",C++在编译时会更改函数名,导致Python执行时找不到导出函数

其他:

    如有修改建议,请不吝赐教!多谢~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值