gpt4 book ai didi

Python ctypes : get variable type and value problem

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:22 24 4
gpt4 key购买 nike

我想通过 ctypes 在一些 Python 代码中使用 C 库。我的问题是我不懂 C。几天来我一直在尝试阅读代码和 C 结构,但我被困在一个问题上。我对 Python 也很陌生,因为我目前正在从 Matlab 转换,因此我的 Python 仅限于科学 Python 应用程序。这个问题很具体,但我认为解决方案是一般的 C 库/Python 链接兴趣。

我正在使用一个名为 Iphreeqc 的库,这是一个地球化学模型,来自“http://wwwbrr.cr.usgs.gov/projects/GWC_coupled/phreeqc/index.html” (iphreeqc-2.18.0-5314.tar .gz,从 OS X 10.6 上的源代码编译。由于 SO 垃圾邮件预防,超链接不起作用)。我想从 C 库进行的评估中提取值到 Python 中。

我怀疑我在 Python 代码的结构和联合部分出错,但我似乎无法弄清楚。

Python代码:

import ctypes

iphreeqc = ctypes.CDLL("libiphreeqc.0.dylib", ctypes.RTLD_GLOBAL)

# C structures from var.h
class VAR_TYPE(ctypes.Structure):
_fields_ = [
("TT_EMPTY",ctypes.c_int),
("TT_ERROR",ctypes.c_int),
("TT_LONG",ctypes.c_int),
("TT_DOUBLE",ctypes.c_int),
("TT_STRING",ctypes.c_int)]


(TT_EMPTY,
TT_ERROR,
TT_LONG,
TT_DOUBLE,
TT_STRING)=map(ctypes.c_int, xrange(5))


class VRESULT(ctypes.Structure):
_fields_ = [
("VR_OK",ctypes.c_int),
("VR_OUTOFMEMORY",ctypes.c_int),
("VR_BADVARTYPE",ctypes.c_int),
("VR_INVALIDARG",ctypes.c_int),
("VR_INVALIDROW",ctypes.c_int),
("VR_INVALIDCOL",ctypes.c_int)]

(VR_OK,
VR_OUTOFMEMORY,
VR_BADVARTYPE,
VR_INVALIDARG,
VR_INVALIDROW,
VR_INVALIDCOL)=map(ctypes.c_int, xrange(0,-6,-1))

class _U(ctypes.Union):
_fields_ = [("lVal", ctypes.c_long),
("dVal", ctypes.c_double),
("sVal", ctypes.c_char),
("vresult", VRESULT)]

class VAR(ctypes.Structure):
_anonymous_ = ("pvar",)
_fields_ = [
("pvar", _U),
("type", VAR_TYPE)]

# Run model
Id=iphreeqc.CreateIPhreeqc()
dbloade = iphreeqc.LoadDatabase(Id,"phreeqc.dat")
estring=iphreeqc.OutputErrorString(Id)

# Model input
iphreeqc.AccumulateLine(Id,"TITLE Example 2.--Temperature dependence of solubility")
iphreeqc.AccumulateLine(Id," of gypsum and anhydrite ")
iphreeqc.AccumulateLine(Id,"SOLUTION 1 Pure water ")
iphreeqc.AccumulateLine(Id," pH 7.0 ")
iphreeqc.AccumulateLine(Id," temp 25.0 ")
iphreeqc.AccumulateLine(Id,"EQUILIBRIUM_PHASES 1 ")
iphreeqc.AccumulateLine(Id," Gypsum 0.0 1.0 ")
iphreeqc.AccumulateLine(Id," Anhydrite 0.0 1.0 ")
iphreeqc.AccumulateLine(Id,"REACTION_TEMPERATURE 1 ")
iphreeqc.AccumulateLine(Id," 25.0 75.0 in 50 steps ")
iphreeqc.AccumulateLine(Id,"SELECTED_OUTPUT ")
iphreeqc.AccumulateLine(Id," -file ex2.sel ")
iphreeqc.AccumulateLine(Id," -user_punch true ")
iphreeqc.AccumulateLine(Id," -reset false ")
iphreeqc.AccumulateLine(Id," -simulation false ")
iphreeqc.AccumulateLine(Id," -selected_out true ")
iphreeqc.AccumulateLine(Id," USER_PUNCH ")
iphreeqc.AccumulateLine(Id," -start ")
iphreeqc.AccumulateLine(Id," 10 punch - LA('H+') ")
iphreeqc.AccumulateLine(Id," -end ")
iphreeqc.AccumulateLine(Id,"END ")

# run model
runout=iphreeqc.RunAccumulated(Id)
estring2=iphreeqc.OutputErrorString(Id)

a=iphreeqc.GetSelectedOutputRowCount(Id)
b=iphreeqc.GetSelectedOutputColumnCount(Id)
print a
print b # this works, gives correct number of rows and columns

vart=VAR()
iphreeqc.VarInit(ctypes.byref(vart))

c=iphreeqc.GetSelectedOutputValue(Id, 43, 0, ctypes.byref(vart)) #tries to extract value from row 43, column 1

print c # c is here VRESULT. this works properly giving the right error number (0 to -6). Gives 0 in this case which is VR_OK

所选行、列中的值是 double 值,但我也尝试过使用字符串值和长(整数)值但没有成功。我希望 vart.dVal(或 .sVal 或 lVal,如果是字符串或长)包含我想要的值,但它没有。我还希望 VAR_TYPE 的 TT_ 字段之一为 1,但它们都是 0。.dVal、.sVal 和 .lVal 似乎实际上包含 VAR_TYPE 数字(介于 0 和 4 之间),并且正确报告了这一点(即 3 表示 double 型,2 表示长型)。

我的问题是:如何修复代码以使 VAR_TYPE 字段反射(reflect)变量类型,该类型现在在 .lVal 字段中返回。我怎样才能得到我想提取到正确的 vart.xVal 字段的值?我是否缺少 Python 结构/联合代码中的一些指针?

结构和联合来自的 C 源代码 (Var.h):

http://wwwbrr.cr.usgs.gov/projects/GWC_coupled/iphreeqc/Var_8h_source.html

我试图在 Python 中重现的 C 示例(v 是我的 Python 代码中的变量“vart”,我现在忽略循环等):

VAR v;
VarInit(&v);

printf("selected-output:\n");
for (i = 0; i < GetSelectedOutputRowCount(id); ++i) {
for (j = 0; j < GetSelectedOutputColumnCount(id); ++j) {
if (GetSelectedOutputValue(id, i, j, &v) == VR_OK) {
switch (v.type) {
case TT_LONG:
printf("%ld ", v.lVal);
break;
case TT_DOUBLE:
printf("%g ", v.dVal);
break;
case TT_STRING:
printf("%s ", v.sVal);
break;
}
}
VarClear(&v);
}
printf("\n");
}

C 示例取自:(向下滚动一点) http://wwwbrr.cr.usgs.gov/projects/GWC_coupled/iphreeqc/IPhreeqc_8h.html#a9f0ffd11e25a7e8f05d800623b14acf5

我在 OS X 10.6.6 上使用 ctypes 1.1.0、Python 2.6.6

抱歉这个问题的长度,希望一些聪明的头脑可以帮助我,或者给我指明正确的方向。

非常感谢

最佳答案

一个问题是枚举不是结构。还要确保结构中的字段与 C header 的顺序相同(“类型”在前)。试试这个:

import ctypes

VAR_TYPE = ctypes.c_int
TT_EMPTY = 0
TT_ERROR = 1
TT_LONG = 2
TT_DOUBLE = 3
TT_STRING = 4

VRESULT = ctypes.c_int
VR_OK = 0
VR_OUTOFMEMORY = -1
VR_BADVARTYPE = -2
VR_INVALIDARG = -3
VR_INVALIDROW = -4
VR_INVALIDCOL = -5

class _U(ctypes.Union):
_fields_ = [
('lVal',ctypes.c_long),
('dVal',ctypes.c_double),
('sVal',ctypes.c_char_p),
('vresult',VRESULT)]

class VAR(ctypes.Structure):
_anonymous_ = ('u',)
_fields_ = [
('type',VAR_TYPE),
('u',_U)]

关于Python ctypes : get variable type and value problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5990674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com