当前位置: 首页 > article >正文

rust与python互通

互通三件套

rust侧与python互通的三个库:

pyo3

pythonize

serde

pyo3

pyo3跟用Python C API写python扩展有点类似,核心是:

#[pymodule]
#[pyfunction]

两个注解。前者对应Py_InitModule,后者对应PyMethodDef。

下面是其它博客上一个python C API写扩展的例子:

//Python.h has all the required function definitions to manipulate the Python objects
#include <Python.h>

//This is the function that is called from your python code
static PyObject* addList_add(PyObject* self, PyObject* args){

    PyObject * listObj;

    //The input arguments come as a tuple, we parse the args to get the various variables
    //In this case it's only one list variable, which will now be referenced by listObj
    if (! PyArg_ParseTuple( args, "O", &listObj ))
        return NULL;

    //length of the list
    long length = PyList_Size(listObj);

    //iterate over all the elements
    int i, sum =0;
    for (i = 0; i < length; i++) {
        //get an element out of the list - the element is also a python objects
        PyObject* temp = PyList_GetItem(listObj, i);
        //we know that object represents an integer - so convert it into C long
        long elem = PyInt_AsLong(temp);
        sum += elem;
    }

    //value returned back to python code - another python object
    //build value here converts the C long to a python integer
    return Py_BuildValue("i", sum);

}

//This is the docstring that corresponds to our 'add' function.
static char addList_docs[] =
"add(  ): add all elements of the list\n";

/* This table contains the relavent info mapping -
   <function-name in python module>, <actual-function>,
   <type-of-args the function expects>, <docstring associated with the function>
 */
static PyMethodDef addList_funcs[] = {
    {"add", (PyCFunction)addList_add, METH_VARARGS, addList_docs},
    {NULL, NULL, 0, NULL}

};

/*
   addList is the module name, and this is the initialization block of the module.
   <desired module name>, <the-info-table>, <module's-docstring>
 */
PyMODINIT_FUNC initaddList(void){
    Py_InitModule3("addList", addList_funcs,
            "Add all ze lists");

}

下面是截取的开源库sqloxide的样例代码:

#[pyfunction]
#[pyo3(text_signature = "(sql, dialect)")]
fn parse_sql(py: Python, sql: String, dialect: String) -> PyResult<PyObject> {
    let chosen_dialect = dialect_from_str(dialect).unwrap_or_else(|| {
        println!("The dialect you chose was not recognized, falling back to 'generic'");
        Box::new(GenericDialect {})
    });
    let parse_result = Parser::parse_sql(&*chosen_dialect, &sql);

    ...

    Ok(output.into())
}

#[pymodule]
fn sqloxide(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(parse_sql, m)?)?;
    m.add_function(wrap_pyfunction!(restore_ast, m)?)?;
    ...
    Ok(())
}

pythonize

pythonnize完成rust数据结构到python数据结构的互转。本质上是一个序列化/反序列化器。序列化的结果是一个json结构,可用常见的json库解析。


http://www.kler.cn/a/444940.html

相关文章:

  • React图标库: 使用React Icons实现定制化图标效果
  • PostgreSQL技术内幕21:SysLogger日志收集器的工作原理
  • python如何获取excel单元格文字是否加粗
  • 【docker】如何打包前端并运行
  • InternVL简读
  • WPF 关于界面UI菜单权限(或者任意控件的显示权限)的简单管理--只是简单简单简单简单
  • honle电源控制器维修UV灯高压电源EVG EPS200
  • 【C语言】一文讲通 和*
  • github快速查找已被删除的文件指令日志
  • Android定时任务实现每隔一段时间切分录音文件,audiorecord实现录音
  • Java全栈项目 - 学生档案管理系统
  • 网络安全等级保护—定级
  • 我在广州学 Mysql 系列——有关 Mysql 函数的练习
  • 发送webhook到飞书机器人
  • Kingbase数据库备份还原操作手册
  • 解锁 Jenkins+Ant+Jmeter 自动化框架搭建新思路
  • 【Ubuntu】设置静态Ip
  • HTML5+CSS3+JS制作精美的宠物主题网站(内附源码,含5个页面)
  • 前端之CSS光速入门
  • 在Win11系统上安装Android Studio
  • 【C#】方法参数的修饰符ref 与 out
  • 华纳云:虚拟服务器之间如何分布式运行?
  • PostgreSQL的交互式终端使用一系列命令来获取有关文本搜索配置对象的信息
  • WPF Binding 绑定
  • linux常用命令(touch、cat、less、head、tail)
  • Scala的惰性求值:深入理解与实践