- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想通过 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/
我正在尝试使用 flot 绘制 SQL 数据库中的数据图表,这是使用 php 收集的,然后使用 json 编码的。 目前看起来像: [{"month":"February","data":482},
我有一个来自 php 行的 json 结果,类似于 ["value"]["value"] 我尝试使用内爆函数,但得到的结果是“value”“value” |id_kategori|created_at
脚本 1 将记录 two 但浏览器仍会将 select 元素呈现为 One。该表单还将提交值 one。 脚本 2 将记录、呈现和提交 两个。我希望它们是同义词并做同样的事情。请解释它们为何不同,以及我
我的python字典结构是这样的: ips[host][ip] 每行 ips[host][ip] 看起来像这样: [host, ip, network, mask, broadcast, mac, g
在 C# 中 我正在关注的一本书对设置和获取属性提出了这样的建议: double pri_test; public double Test { get { return pri_test; }
您可能熟悉 enum 位掩码方案,例如: enum Flags { FLAG1 = 0x1, FLAG2 = 0x2, FLAG3 = 0x4, FLAG4 = 0x8
在一些地方我看到了(String)value。在一些地方value.toString() 这两者有什么区别,在什么情况下我需要使用哪一个。 new Long(value) 和 (Long)value
有没有什么时候 var result = !value ? null : value[0]; 不会等同于 var result = value ? value[0] : null; 最佳答案 在此处将
我正在使用扫描仪检测设备。目前,我的条形码的值为 2345345 A1。因此,当我扫描到记事本或文本编辑器时,输出将类似于 2345345 A1,这是正确的条形码值。 问题是: 当我第一次将条形码扫描
我正在读取 C# 中的资源文件并将其转换为 JSON 字符串格式。现在我想将该 JSON 字符串的值转换为键。 例子, [ { "key": "CreateAccount", "text":
我有以下问题: 我有一个数据框,最多可能有 600 万行左右。此数据框中的一列包含某些 ID。 ID NaN NaN D1 D1 D1 NaN D1 D1 NaN NaN NaN NaN D2 NaN
import java.util.*; import java.lang.*; class Main { public static void main (String[] args) thr
我目前正在开发我的应用程序,使其设计基于 Holo 主题。在全局范围内我想做的是工作,但我对文件夹 values、values-v11 和 values-v14. 所以我知道: values 的目标是
我遇到了一个非常奇怪的问题。 我的公司为我们的各种 Assets 使用集中式用户注册网络服务。我们一般通过HttpURLConnection使用请求方法GET向Web服务发送请求,通过qs设置参数。这
查询: UPDATE nominees SET votes = ( SELECT votes FROM nominees WHERE ID =1 ) +1 错误: You can't specify
如果我运行一段代码: obj = {}; obj['number'] = 1; obj['expressionS'] = 'Sin(0.5 * c1)'; obj['c
我正在为我的应用创建一个带有 Twitter 帐户的登录页面。当我构建我的项目时会发生上述错误。 values/strings.xml @dimen/abc_text_size_medium
我在搜索引擎中使用以下 View : CREATE VIEW msr_joined_view AS SELECT table1.id AS msr_id, table1.msr_number, tab
为什么验证会返回此错误。如何解决? ul#navigation li#navigation-3 a.current Value Error : background-position Too
我有一个数据名如下 import pandas as pd d = { 'Name' : ['James', 'John', 'Peter', 'Thomas', 'Jacob', 'Andr
我是一名优秀的程序员,十分优秀!