为 C 和 Python 创建共享参数文件

标签 c python-3.x

I need to create a parameter file that can be managed across a Python 3.7 and a C code base. This file needs to be modifiable either by the C or the Python program with the changes being taking effect on the other software (an update function will handle reading the updated file). It's best if the file is not human readable, as it contains information that is better left obfuscated.

**有推荐的方法吗? **

我可以创建单独的 python 和 C 文件,但参数集会随着时间的推移而改变(用于代码维护),并且这些值将由这些程序更改。这个名单也会很长。维护两个不同的文件并随着时间的推移更新它们会很麻烦。此外,文件可能需要在用户之间交换,使得用户1运行的软件修改的版本需要能够被用户2运行的软件读取。这个想法是,两个代码的其他部分都可以访问参数列表的部分内容,而无需知道列表的完整内容。

为了澄清这个例子,我可以有一个 parameter.h 文件,其中包含:

struct {
  double par1 =1.1;
  int par 2   =2;
} par_list

我可以有一个 parameter.py :

class par_list:
    def(__self__):
        self.par1 = double(1.1)
        self.par2 = int(2)

然后,通过在 Python 中进行导入或在 C 中进行包含,我可以初始化参数列表。但在这种情况下,参数是在不同的文件上读取的。

我正在考虑使用某种二进制文件来保存值,并创建一个脚本来编写读取和更新值的 Python 和 C 代码。我很担心,因为二进制文件需要在运行 Linux 的 ARM 架构和运行 Windows 的 x86 架构之间互换。

最佳答案

这是一个使用 numpy 的示例:

C 代码:

#include <stdio.h>
#include <stdint.h>

struct Struct_format{
    uint8_t the_unsigned_int8;
    int32_t the_signed_int32[2];
    double the_double;
};

typedef struct Struct_format upperStruct;

//Use separate file to define default value:

void printStruct(upperStruct test_struct){
    printf("test_struct.the_unsigned_int8 = %d\n", test_struct.the_unsigned_int8);
    printf("test_struct.the_signed_int32[0]  = %d\n", test_struct.the_signed_int32[0]);
    printf("test_struct.the_signed_int32[1]  = %d\n", test_struct.the_signed_int32[1]);
    printf("test_struct.the_double        = %f\n", test_struct.the_double);
}

void main(){
    //Define a "default" value:
    upperStruct fromC2Python = {4U,{-3,-1},2.1};

    printf("Printing fromC2Python\n");
    printStruct(fromC2Python);

    //Save this default in a file:
    FILE * fid = fopen("fromC2Python.bin","w");
    fwrite((void *)&fromC2Python, sizeof(fromC2Python) ,1, fid);
    fclose(fid);

    //Now load the file created by Python:
    upperStruct fromPython2C;
    FILE * fid_py = fopen("fromPython2C.bin","r");
    fread(&fromPython2C, sizeof(fromPython2C) ,1, fid_py);
    fclose(fid_py);

    printf("Printing fromPython2C\n");
    printStruct(fromPython2C);

}

Python 代码:

import numpy


datatype = numpy.dtype([('potato',
                                  [('time', numpy.uint8),
                                   ('sec', numpy.int32, 2)]),
                        ('temp', numpy.float64)],
                       align=True)
fromPython2C = numpy.array([((5, (-6, -7)), 61.55)], dtype=datatype)

print(fromPython2C)

fromPython2C.tofile("fromPython2C.bin", sep="")

fromC2Python = numpy.fromfile("fromC2Python.bin", dtype=datatype, count=-1, sep="")

print(fromC2Python)
print(fromC2Python['potato'])
print(fromC2Python['potato']['time'])
print(fromC2Python['temp'])

这个想法是numpy允许读取和写入结构化二进制文件。因此,使用文本解析器创建 dtype 规范就足够了。

关于为 C 和 Python 创建共享参数文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55345025/

相关文章:

c - 我在C编程中遇到错误。 ([Error]预期标识符或 '(' token 之前的 '{')。有人可以帮帮我,让我知道为什么吗?

c - 在同时填充数组的同时从数组中找到两个最大值(差值大于一个)的可能性

C语法解释

python - 无法打开数据库文件 - Mayan EDMS

python 3.2.3 忽略字符串中的转义序列

python-3.x - 在 CVXPY 中广播

c - 不可见的 GCC 优化标志?

c - 此代码是否有资格成为 block 密码

python - 如何返回一个以列表作为值的字典,但它只返回键

python - For/While 循环生成 *-三角形