From 009124d4d28738ee6cfc385262f913a13f08a0da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kotori=E3=81=AE=E3=81=AD=E3=81=93?= Date: Tue, 7 Nov 2023 20:24:00 -0500 Subject: [PATCH] :sparkles: Add `__dict__` for CalculatorConfig --- docs/build.md | 23 +++++++++---------- src/applications/analysis.rs | 22 +++++++++---------- src/applications/input/artifact.rs | 11 ++++++---- src/applications/input/buff.rs | 5 ++++- src/applications/input/calculator.rs | 33 +++++++++++++++++++++++++++- src/applications/input/skill.rs | 6 +++-- 6 files changed, 67 insertions(+), 33 deletions(-) diff --git a/docs/build.md b/docs/build.md index 8f868e1..655f4e7 100644 --- a/docs/build.md +++ b/docs/build.md @@ -20,14 +20,13 @@ ## Build -- Navigate to core -> run cargo build +- (Optional) Run cargo build ``` - $ cd python_genshin_artifact_core - $ cargo build + $ cargo build --no-default-features ... Compiling pyo3-ffi v0.19.2 Compiling pyo3 v0.19.2 - Compiling genshin_artifact_core v0.1.1 (/home/kotori/projects/python/python-genshin-artifact/python_genshin_artifact_core) + Compiling python_genshin_artifact v0.1.1 (/home/kotori/projects/python/python-genshin-artifact/python_genshin_artifact_core) Finished dev [unoptimized + debuginfo] target(s) in 3.12s ``` - Use maturin to install the rust library into a python library @@ -35,20 +34,18 @@ $ maturin develop ... 📦 Built wheel for CPython 3.11 to /tmp/.tmpicGM3M/genshin_artifact_core-0.1.1-cp311-cp311-linux_x86_64.whl - 🛠 Installed genshin_artifact_core-0.1.1 + 🛠 Installed python_genshin_artifact-0.1.1 ``` -- Navigate back to project root and build with poetry: +- Use maturin to build the wheel ``` - $ poetry build - Building Python-Genshin-Artifact (0.1.1) - - Building sdist - - Built python_genshin_artifact-0.1.1.tar.gz - - Building wheel - - Built python_genshin_artifact-0.1.1-py3-none-any.whl + $ maturin build --out dist + ... + Finished dev [unoptimized + debuginfo] target(s) in 3.88s + 📦 Built wheel for CPython 3.11 to dist/python_genshin_artifact-0.1.4-cp311-cp311-manylinux_2_34_x86_64.whl ``` - Install the built wheel with pip: ``` - $ pip install dist/python_genshin_artifact-0.1.1-py3-none-any.whl + $ pip install dist/python_genshin_artifact-xxx.whl ``` diff --git a/src/applications/analysis.rs b/src/applications/analysis.rs index 5994b1b..13e4569 100644 --- a/src/applications/analysis.rs +++ b/src/applications/analysis.rs @@ -49,12 +49,11 @@ pub fn get_damage_analysis(calculator_config: PyCalculatorConfig) -> PyResult config.as_ref(py).repr()?.to_str()?.to_string(), None => "None".to_string(), }; - Ok(format!("BuffInterface(name={}, config={})", name, config_repr)) + Ok(format!( + "BuffInterface(name={}, config={})", + name, config_repr + )) } #[getter] diff --git a/src/applications/input/calculator.rs b/src/applications/input/calculator.rs index bfaa951..dcf25d9 100644 --- a/src/applications/input/calculator.rs +++ b/src/applications/input/calculator.rs @@ -6,7 +6,7 @@ use crate::applications::input::skill::PySkillInterface; use crate::applications::input::weapon::PyWeaponInterface; use pyo3::prelude::*; -use pyo3::types::PyDict; +use pyo3::types::{PyDict, PyList}; #[pyclass(name = "CalculatorConfig")] #[derive(Clone)] @@ -50,4 +50,35 @@ impl PyCalculatorConfig { enemy, }) } + + #[getter] + pub fn __dict__(&self, py: Python) -> PyResult { + let dict = PyDict::new(py); + dict.set_item("character", self.character.__dict__(py)?)?; + dict.set_item("weapon", self.weapon.__dict__(py)?)?; + let buffs = self + .buffs + .iter() + .map(|b| b.__dict__(py)) + .collect::, PyErr>>()?; + dict.set_item("buffs", PyList::new(py, buffs))?; + let artifacts = self + .artifacts + .iter() + .map(|ar| ar.__dict__(py)) + .collect::, PyErr>>()?; + dict.set_item("artifacts", PyList::new(py, artifacts))?; + if let Some(artifact_config) = self.artifact_config.as_ref().map(|c| c.as_ref(py)) { + dict.set_item("artifact_config", artifact_config)?; + } else { + dict.set_item("artifact_config", py.None())?; + } + dict.set_item("skill", self.skill.__dict__(py)?)?; + if let Some(enemy) = self.enemy.as_ref().map(|e| e.__dict__(py)).transpose()? { + dict.set_item("enemy", enemy)?; + } else { + dict.set_item("enemy", py.None())?; + } + Ok(dict.into()) + } } diff --git a/src/applications/input/skill.rs b/src/applications/input/skill.rs index acad3f6..9014d21 100644 --- a/src/applications/input/skill.rs +++ b/src/applications/input/skill.rs @@ -22,7 +22,10 @@ impl PySkillInterface { Ok(Self { index, config }) } pub fn __repr__(&self) -> PyResult { - Ok(format!("SkillInterface(index: {}, config: {:?})", self.index, self.config)) + Ok(format!( + "SkillInterface(index: {}, config: {:?})", + self.index, self.config + )) } #[getter] @@ -36,7 +39,6 @@ impl PySkillInterface { } Ok(dict.into()) } - } impl TryInto for PySkillInterface {