pub fn py_exec_module(
py: Python<'_>,
pymod: &RUMPyModule,
func_name: &str,
args: &RUMPyList,
) -> RUMResult<RUMPyAny>Expand description
Function for executing a python module’s function.
If you set the argument func_name to an empty string, py_exec will do nothing. Allegedly,
the module executed upon import.
It is recommended you have a function to call from the module!!!
§Examples
§Executing Function Within Module
use compact_str::format_compact;
use pyo3::{Python, IntoPyObjectExt};
use pyo3::types::PyModule;
use crate::rumtk_core::scripting::python_utils::{RUMPyAny, RUMPyArgs, RUMPyModule, RUMPyList};
use crate::rumtk_core::scripting::python_utils::{py_load, py_exec_module, py_buildargs, py_list_to_tuple};
use uuid::Uuid;
use crate::rumtk_core::strings::RUMString;
let expected: &str = "print('Hello World!')\ndef test():\n\treturn 'Hello'";
let fpath: RUMString = format_compact!("/tmp/{}.py", Uuid::new_v4());
std::fs::write(&fpath, expected.as_bytes()).expect("Failure to write test module.");
let expect: Vec<&str> = vec![];
Python::attach( |py| {
let py_obj: RUMPyModule = py_load(py, &fpath).expect("Failure to load module!");
let args: RUMPyList = py_buildargs(py, &expect).unwrap();
let result = py_exec_module(py, &py_obj, "test", &args).expect("Failed to extract result!");
});
std::fs::remove_file(&fpath).unwrap()§Executing Module
use compact_str::format_compact;
use pyo3::{Python, IntoPyObjectExt};
use pyo3::types::PyModule;
use crate::rumtk_core::scripting::python_utils::{RUMPyAny, RUMPyArgs, RUMPyModule, RUMPyList};
use crate::rumtk_core::scripting::python_utils::{py_load, py_exec_module, py_new_args};
use uuid::Uuid;
use crate::rumtk_core::strings::RUMString;
let expected: &str = "print('Hello World!')\ndef test():\n\treturn 'Hello'";
let fpath: RUMString = format_compact!("/tmp/{}.py", Uuid::new_v4());
std::fs::write(&fpath, expected.as_bytes()).expect("Failure to write test module.");
let expect: Vec<&str> = vec![];
Python::attach( |py| {
let py_obj: RUMPyModule = py_load(py, &fpath).expect("Failure to load module!");
let args: RUMPyList = py_new_args(py);
let result = py_exec_module(py, &py_obj, "", &args).expect("Failed to extract result!");
});
std::fs::remove_file(&fpath).unwrap()