Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit a2c822d

Browse files
committed
feat: use latest PyO3
1 parent 704e42d commit a2c822d

File tree

4 files changed

+64
-50
lines changed

4 files changed

+64
-50
lines changed

Cargo.lock

Lines changed: 35 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mongodb = { version = "2.8.2", features = ["async-std"] }
2929
once_cell = "1.19.0"
3030
pem = { version = "3.0.4", features = ["serde"] }
3131
polars = "0.39.2"
32-
pyo3 = { version = "0.16", features = ["auto-initialize"] }
32+
pyo3 = { version = "0.21.2", features = ["auto-initialize"] }
3333
rand = "0.8.5"
3434
reqwest = "0.12.3"
3535
rsa = "0.9.6"

src/utils/format/convert.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pandas as pd
2+
3+
def read_and_save_csv(input_path, output_path):
4+
df = pd.read_csv(input_path, encoding='utf-8')
5+
df.to_excel(output_path, index=False)

src/utils/format/csv_to_excel.rs

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
1-
use pyo3::prelude::*;
2-
use pyo3::types::IntoPyDict;
1+
use pyo3::types::{PyAnyMethods, PyModule};
2+
use pyo3::{Py, PyAny, PyResult, Python};
33

4-
pub fn to_excel() -> Result<(), String> {
5-
let gil = Python::acquire_gil();
6-
let py = gil.python();
7-
8-
let code = r#"
9-
import pandas as pd
10-
11-
def read_and_save_csv(input_path, output_path):
12-
df = pd.read_csv(input_path, encoding='utf-8')
13-
df.to_excel(output_path, index=False)
14-
"#;
15-
16-
py.run(code, None, None).unwrap();
17-
18-
let pandas = py.import("pandas");
19-
if let Err(_) = pandas {
20-
return Err("Failed to import pandas".to_string());
21-
}
22-
let pandas = pandas.unwrap();
23-
let locales = [("pd", pandas)].into_py_dict(py);
24-
let func = py.eval("read_and_save_csv", None, Some(locales));
25-
if let Err(_) = func {
26-
return Err("Failed to get Python function".to_string());
27-
}
28-
let func = func.unwrap().extract();
29-
if let Err(_) = func {
30-
return Err("Failed to extract Python function".to_string());
31-
}
32-
let func: Py<PyAny> = func.unwrap();
33-
let _ = func.call1(py, ("output.csv", "output.xlsx"));
34-
Ok(())
4+
pub fn to_excel() -> PyResult<()> {
5+
Python::with_gil(|py| {
6+
let bound = PyModule::from_code_bound(
7+
py,
8+
r#"
9+
def to_excel(input_path: str, output_path: str):
10+
import pandas as pd
11+
pd.read_csv(input_path, encoding='utf-8').to_excel(output_path, index=False)
12+
"#,
13+
"",
14+
"",
15+
);
16+
if let Err(_) = bound {
17+
return Err(pyo3::PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(
18+
"Failed to get Python function",
19+
));
20+
}
21+
let bound: Py<PyAny> = bound.unwrap().getattr("to_excel")?.into();
22+
let _ = bound.call1(py, ("output.csv", "output.xlsx"));
23+
Ok(())
24+
})
3525
}

0 commit comments

Comments
 (0)