简体   繁体   English

如何使用python变量而不是硬编码值创建ctypes变量?

[英]How to creating ctypes variables using python variables instead of hard-coded values?

When creating ctypes variables, can one not pass values using python variables? 创建ctypes变量时,是否可以不使用python变量传递值?

I have some code where I am calling a shared C library. 我在调用共享C库的地方有一些代码。 If I pass the parameters to this C library using Method 1 (see below) things work well. 如果我使用方法1(参见下文)将参数传递给此C库,则一切正常。 But if I use Method 2, I get garbage. 但是,如果我使用方法2,则会得到垃圾。 There are other parts to the code. 该代码还有其他部分。 But I have confirmed that when I replace Method 2 with Method 1, things work well. 但是我已经确认,当我用方法1替换方法2时,一切正常。 So something is wrong here. 所以这里有些问题。

If what I am doing in Method 2 is not valid, what is the alternative if I want to automate the process of running the code for different values of a given variable(s)? 如果我在方法2中所做的操作无效,那么如果我想针对给定变量的不同值自动运行代码的过程,该怎么办?

Method 1 (This works well) 方法1(效果很好)

import ctypes as C


c_thresholds = (C.c_double * 4)()
for idx, value in enumerate(thresholds):
    c_thresholds[idx] = value

goodH = Good(C.c_char('H'), C.c_double(0.5), C.c_int(100), C.c_int(20))
goodL = Good(C.c_char('L'), C.c_double(0.5), C.c_int(75), C.c_int(20))

c_parameters = Params(
            var1 = C.c_int(100),
            var2 = C.c_int(4),
            var3 = C.c_int(5),
            var4 = C.c_int(5000),
            var5 = C.c_char_p("modelname"),
            var6 = C.c_double(0.5),
            var7 = C.c_double(90),
            var8 = c_thresholds,
            var9 = C.c_int(2),
            H = goodH,
            L = goodL
)

runsimulation(c_parameters)

Method 2 (This does not work, outputs garbage) 方法2(这不起作用,输出垃圾)

import ctypes as C

def create_cparams(var1, var2, var3, var4, var5, var6, var7, var8, var9):

    c_thresholds = (C.c_double * 4)()
    for idx, value in enumerate(var8):
        c_thresholds[idx] = value

    goodH = Good(C.c_char('H'), C.c_double(0.5), C.c_int(100), C.c_int(20))
    goodL = Good(C.c_char('L'), C.c_double(0.5), C.c_int(75), C.c_int(20))

    c_parameters = Params(
                var1 = C.c_int(var1),
                var2 = C.c_int(var2),
                var3 = C.c_int(var3),
                var4 = C.c_int(var4),
                var5 = C.c_char_p(var5),
                var6 = C.c_double(var6),
                var7 = C.c_double(var7),
                var8 = c_thresholds,
                var9 = C.c_int(var9),
                H = goodH,
                L = goodL
    )

    return c_parameters

# These are python variables
var1 = 100
var2 = 4
var3 = 5
var4 = 5000
var5 = "modelname"
var6 = 0.5
var7 = 90
var8 = [1, 0.9, 0.8, 0.7]
var9 = 2

# Calling the create_cparams function defined above
c_parameters = create_cparams(var1, var2, var3, var4, var5, var6, var7, var8, var9)
runsimulation(c_parameters)

In case it is helpful the Params class is given by (does not change across the two methods): 如果有帮助的话,Params类是由给定的(在这两种方法中不变):

class Params(C.Structure):
    _fields_ = [
            ("var1", C.c_int),
            ("var2", C.c_int),
            ("var3", C.c_int),
            ("var4", C.c_int),
            ("var5", C.c_char_p ),
            ("var6", C.c_double),
            ("var7", C.c_double),
            ("var8", (C.c_double * 4) ),
            ("var9", C.c_int),
            ("H", Good),
            ("L", Good)
    ]

C function prototype C函数原型

// runsimulation() function above calls this C function

void run_multiple_reps (struct params parameters, struct repdata *data,
                    int len_timepdsarr, int *timepdsarr)

// params struct on C side, which Params class duplicates

struct params
{
    int var1;
    int var2;
    int var3;
    int var4;
    char *var5;
    double var6;
    double var7;
    double var8[4];
    int var9;
    struct good H;
    struct good L;
};

The field attributes of a Structure are CField descriptor objects. Structure的字段属性是CField描述符对象。 A descriptor is like a Python property or like a __slots__ attribute, if you're familiar with either of those. 如果您熟悉描述符,则描述符就像Python property__slots__属性。 A CField knows the data type of the field and its offset into the buffer. CField知道字段的数据类型及其在缓冲区中的偏移量。 Each C data type has a associated get/set function that converts to and from Python objects. 每个C数据类型都有一个关联的get / set函数,该函数在Python对象之间来回转换。 So generally you can assign a Python object directly to the field. 因此,通常您可以将Python对象直接分配给该字段。 For example: 例如:

thresholds = [1, 0.9, 0.8, 0.7]    

c_parameters = Params(
    var1 = 100,
    var2 = 4,
    var3 = 5,
    var4 = 5000,
    var5 = "modelname",
    var6 = 0.5,
    var7 = 90,
    var8 = (C.c_double * 4)(*thresholds),
    var9 = 2,
    H = Good('H', 0.5, 100, 20),
    L = Good('L', 0.5, 75, 20),
)

If ctypes needs to hold a reference to a Python object to keep it alive, the reference is stored in the _objects dict of the Structure . 如果ctypes需要保留对Python对象的引用以使其保持活动状态,则该引用将存储在Structure_objects dict中。 In this case the array in var8 , for example, is simply copied into the buffer, so c_parameters doesn't need to hold a reference to the original. 例如,在这种情况下,只需将var8的数组复制到缓冲区中,因此c_parameters不需要保留对原始c_parameters的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python函数中的硬编码变量 - Hard coded variables in python function python 中未硬编码的条件 - Conditions in python that are not hard-coded 如何通过python脚本将硬编码事件发送到piwik - How to send hard-coded events to piwik via python script 使用 Python AST 确定字符串是否已被硬编码到打印调用中 - Using Python AST to determine if a string has been hard-coded into a print call Python Web-Scraping数据没有硬编码到HTML中 - Python Web-Scraping data that's not hard-coded into the HTML 如何在不使用引号的情况下定义/输入硬编码的多行列表 - How to define/ingest a hard-coded multi-line list without using quotes Python 调试器:交互式调试与硬编码断点 - Python debugger: interactive debugging vs. hard-coded breakpoint 如果字符串没有硬编码,则脚本中的Python编码问题 - Python encoding issue in script if string not hard-coded 使用RK4的车道嵌入式解决方案(硬编码) - Lane-emden solutions using RK4 (hard-coded) 为什么在逐行导入文本文件以进行情感分析而不是使用硬编码的句子时出现TypeError? - Why do I get a TypeError when importing a textfile line by line for sentiment analysis instead of using a sentence hard-coded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM