use crate::types::*;
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::{PyAny, PyDict};
use pyo3_stub_gen::derive::gen_stub_pyclass;
#[gen_stub_pyclass]
#[pyclass(skip_from_py_object)]
#[derive(Clone, Debug)]
pub(crate) struct QEiConfig {
#[pyo3(get, set)]
pub batch: usize,
#[pyo3(get, set)]
pub strategy: QEiStrategy,
#[pyo3(get, set)]
pub optmod: usize,
}
impl<'a, 'py> FromPyObject<'a, 'py> for QEiConfig {
type Error = PyErr;
fn extract(obj: pyo3::Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
if let Ok(cfg) = obj.extract::<PyRef<'py, Self>>() {
return Ok(cfg.clone());
}
let dict = obj.cast::<PyDict>()?;
let mut cfg = QEiConfig::default();
for key_any in dict.keys().iter() {
let key = key_any.extract::<String>()?;
match key.as_str() {
"batch" => cfg.batch = dict.get_item("batch")?.unwrap().extract()?,
"strategy" => cfg.strategy = dict.get_item("strategy")?.unwrap().extract()?,
"optmod" => cfg.optmod = dict.get_item("optmod")?.unwrap().extract()?,
_ => {
return Err(PyValueError::new_err(format!(
"unknown qei_config key '{key}'"
)));
}
}
}
Ok(cfg)
}
}
impl Default for QEiConfig {
fn default() -> Self {
QEiConfig::new(1, QEiStrategy::Kb, 1)
}
}
#[pymethods]
impl QEiConfig {
#[new]
#[pyo3(signature = (
batch=QEiConfig::default().batch,
strategy=QEiConfig::default().strategy,
optmod=QEiConfig::default().optmod,
))]
pub fn new(batch: usize, strategy: QEiStrategy, optmod: usize) -> Self {
QEiConfig {
batch,
strategy,
optmod,
}
}
}