diff --git a/Cargo.toml b/Cargo.toml index 40939a2..d223761 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ome-metadata" -version = "0.2.1" +version = "0.2.2" edition = "2024" rust-version = "1.85.1" authors = ["Wim Pomp "] diff --git a/py/ome_metadata/__init__.py b/py/ome_metadata/__init__.py index d96f131..cfbb99e 100644 --- a/py/ome_metadata/__init__.py +++ b/py/ome_metadata/__init__.py @@ -27,6 +27,12 @@ class Ome(UserDict): else: return new + def __getstate__(self) -> dict[str, Any]: + return self.data + + def __setstate__(self, state: dict[str, Any]) -> None: + self.data = state + class OmeList(UserList): def __getitem__(self, item: int) -> Ome | OmeList | int | float | str: diff --git a/src/py.rs b/src/py.rs index 72c0343..ca37cb5 100644 --- a/src/py.rs +++ b/src/py.rs @@ -1,4 +1,3 @@ -use anyhow::anyhow; use crate::Ome; use crate::ome::{ Convert, UnitsElectricPotential, UnitsFrequency, UnitsLength, UnitsPower, @@ -6,39 +5,7 @@ use crate::ome::{ }; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; -use pyo3::IntoPyObjectExt; -/// helper class to be used for unpickling -#[pyclass(module = "ome_metadata.ome_metadata_rs")] -struct Constructor; - -#[pymethods] -impl Constructor { - #[new] - fn new() -> Self { - Self - } - - fn __getstate__(&self) -> (u8,) { - (0,) - } - - fn __setstate__(&self, _state: (u8,)) {} - - #[staticmethod] - fn __call__(py: Python, class: String, variant: String) -> PyResult> { - match class.as_str() { - "ElectricPotential" => ElectricPotential::new(&variant)?.into_bound_py_any(py), - "Frequency" => Frequency::new(&variant)?.into_bound_py_any(py), - "Length" => Length::new(&variant)?.into_bound_py_any(py), - "Power" => Power::new(&variant)?.into_bound_py_any(py), - "Pressure" => Pressure::new(&variant)?.into_bound_py_any(py), - "Temperature" => Temperature::new(&variant)?.into_bound_py_any(py), - "Time" => Time::new(&variant)?.into_bound_py_any(py), - _ => Err(anyhow!("cannot parse class {}", class).into()) - } - } -} macro_rules! impl_enum_into_py_object { ($($s:ident: $t:ty $(,)?)*) => { @@ -79,9 +46,9 @@ macro_rules! impl_enum_into_py_object { fn __str__(&self) -> String { format!("{:?}", self.inner) } - - fn __reduce__(&self) -> PyResult<(Constructor, (String, String))> { - Ok((Constructor, (stringify!($s).to_string(), format!("{:?}", self.inner)))) + + fn __getnewargs__(&self) -> (String,) { + (format!("{:?}", self.inner),) } } @@ -110,7 +77,6 @@ impl_enum_into_py_object! { #[pymodule] #[pyo3(name = "ome_metadata_rs")] fn ome_metadata_rs(m: &Bound<'_, PyModule>) -> PyResult<()> { - m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?;