rust与python互通

互通三件套

rust侧与python互通的三个库:

pyo3

pythonize

serde

pyo3

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

复制代码
#[pymodule]
#[pyfunction]

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

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

C 复制代码
//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的样例代码:

rust 复制代码
#[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库解析。

相关推荐
zhangfeng1133几秒前
GitHub博主hiyouga与LlamaFactory项目研究报告
python·大语言模型
brucelee1862 分钟前
Java 开发AWS Lambda 实战指南(SAM CLI + IntelliJ)
java·开发语言
tobias.b2 分钟前
408真题解析-2010-37-计算机网络-子网划分与CIDR
开发语言·计算机网络·计算机考研·408真题解析
wanderful_3 分钟前
自定义用户体系下 Django 业务模块开发踩坑与通用解决方案(技术分享版)
后端·python·django
纯.Pure_Jin(g)5 分钟前
【Python练习五】Python 正则与网络爬虫实战:专项练习(2道经典练习带你巩固基础——看完包会)
开发语言·vscode·python
喵手6 分钟前
Python爬虫实战:招聘会参会企业数据采集实战 - 分页抓取、去重与增量更新完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·增量·零基础python爬虫教学·招聘会参会企业数据采集·分页抓取去重
hoiii1878 分钟前
基于C#实现的高性能实时MP4录屏方案
开发语言·c#
JasonSJX9 分钟前
海海软件正式发布全新 DRM-X官网 Next.js 重构、多语言升级与 SEO 优化,助力全球数字版权保护
开发语言·javascript·安全·重构·视频防录屏·开源drm·加密保护课程
系统修复专家14 分钟前
UG12.0官方未公开修复方法:彻底解决C++异常崩溃问题
开发语言·c++·安全·bug·dll·游戏报错
yongui4783417 分钟前
基于C#实现Modbus RTU通信
开发语言·c#