Skip to content

Commit bbd202c

Browse files
committed
publish: v1.1.2 - May stable
- Fix `Calculator`: `get_`, `set_`, `del_` api
1 parent 90845c4 commit bbd202c

File tree

6 files changed

+138
-123
lines changed

6 files changed

+138
-123
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "peace-performance-python"
3-
version = "1.1.1"
3+
version = "1.1.2"
44
authors = ["Pure-Peace <940857703@qq.com>"]
55
edition = "2018"
66

peace_performance_python/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
__author__ = 'Pure-Peace'
88
__license__ = 'MIT'
99
__copyright__ = 'Copyright 2021 Pure-Peace'
10-
__version__ = '1.1.1'
10+
__version__ = '1.1.2'
1111

1212
from .objects import *
1313
from . import functions

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ requires = [
1010

1111
[project]
1212
name = "peace-performance-python"
13-
version = "1.1.1"
13+
version = "1.1.2"
1414
description = "Rust binding for python. To calculate star ratings and performance points for all osu! gamemodes, and quickly parse Beatmap into python objects."
1515
keywords = "rust peace osu pp beatmap parse rosu-pp peace-performance"
1616
author = "PurePeace"

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
setup(
2121
name="peace-performance-python",
22-
version="1.1.1",
22+
version="1.1.2",
2323
description="Rust binding for python. To calculate star ratings and performance points for all osu! gamemodes, and quickly parse Beatmap into python objects.",
2424
long_description=open("README.md", encoding="utf-8").read(),
2525
long_description_content_type="text/markdown",

src/objects/calculator.rs

Lines changed: 123 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,36 @@ use pyo3::{
88
use super::CalcResult;
99
use crate::{methods::pp, objects::Beatmap, set_calculator};
1010

11-
macro_rules! generate_func {
12-
({$($attr:ident: $type:ty),*}) => {
11+
macro_rules! create_py_methods {
12+
($for:ident, fn get_set_del() {$($attr:ident: $type:ty),*}, impl {$($others:tt)*}) => {
1313
paste::paste! {
14-
$(
15-
pub fn [<get_ $attr>](&self) -> $type { self.$attr }
16-
pub fn [<set_ $attr>](&mut self, value: $type) { self.$attr = value; }
17-
pub fn [<del_ $attr>](&mut self) { self.$attr = None; }
18-
)*
14+
#[pymethods] impl $for {
15+
#[new]
16+
#[args(data = "None", kwargs = "**")]
17+
pub fn new(data: Option<&PyDict>, kwargs: Option<&PyDict>) -> PyResult<Self> {
18+
let mut slf = Self::default();
19+
if let Some(d) = data.or(kwargs) {
20+
Self::set_with_dict(&mut slf, d)?;
21+
}
22+
Ok(slf)
23+
}
24+
25+
pub fn getattr<'a>(slf: &'a PyCell<Self>, attr: &PyAny) -> PyResult<&'a PyAny> {
26+
slf.as_ref().getattr(attr)
27+
}
28+
29+
pub fn setattr<'a>(slf: &PyCell<Self>, attr: &PyAny, value: &PyAny) -> PyResult<()> {
30+
slf.as_ref().setattr(attr, value)
31+
}
32+
33+
$(
34+
pub fn [<r#get_ $attr>](&self) -> $type { self.$attr }
35+
pub fn [<r#set_ $attr>](&mut self, value: $type) { self.$attr = value; }
36+
pub fn [<r#del_ $attr>](&mut self) { self.$attr = None; }
37+
)*
38+
39+
$($others)*
40+
}
1941
}
2042
};
2143
}
@@ -47,10 +69,9 @@ pub struct Calculator {
4769
pub score: Option<u32>,
4870
}
4971
crate::pyo3_py_protocol!(Calculator);
50-
51-
#[pymethods]
52-
impl Calculator {
53-
generate_func!({
72+
create_py_methods!(
73+
Calculator,
74+
fn get_set_del() {
5475
mode: Option<u8>,
5576
mods: Option<u32>,
5677
n50: Option<usize>,
@@ -62,125 +83,108 @@ impl Calculator {
6283
combo: Option<usize>,
6384
miss: Option<usize>,
6485
score: Option<u32>
65-
});
66-
67-
#[new]
68-
#[args(data = "None", kwargs = "**")]
69-
pub fn new(data: Option<&PyDict>, kwargs: Option<&PyDict>) -> PyResult<Self> {
70-
let mut slf = Self::default();
71-
if let Some(d) = data.or(kwargs) {
72-
Self::set_with_dict(&mut slf, d)?;
86+
},
87+
impl {
88+
#[staticmethod]
89+
pub fn new_empty() -> Self {
90+
Self::default()
7391
}
74-
Ok(slf)
75-
}
76-
77-
#[staticmethod]
78-
pub fn new_empty() -> Self {
79-
Self::default()
80-
}
81-
82-
#[inline(always)]
83-
pub fn reset(&mut self) {
84-
self.mode = None;
85-
self.mods = None;
86-
self.n50 = None;
87-
self.n100 = None;
88-
self.n300 = None;
89-
self.katu = None;
90-
self.acc = None;
91-
self.passed_obj = None;
92-
self.combo = None;
93-
self.miss = None;
94-
self.score = None;
95-
}
9692

97-
#[inline(always)]
98-
pub fn calculate_raw(&self, beatmap: &Beatmap) -> CalcResult {
99-
CalcResult(self.calc(beatmap))
100-
}
101-
102-
pub fn getattr<'a>(slf: &'a PyCell<Self>, attr: &PyAny) -> PyResult<&'a PyAny> {
103-
slf.as_ref().getattr(attr)
104-
}
93+
#[inline(always)]
94+
pub fn reset(&mut self) {
95+
self.mode = None;
96+
self.mods = None;
97+
self.n50 = None;
98+
self.n100 = None;
99+
self.n300 = None;
100+
self.katu = None;
101+
self.acc = None;
102+
self.passed_obj = None;
103+
self.combo = None;
104+
self.miss = None;
105+
self.score = None;
106+
}
105107

106-
pub fn setattr<'a>(slf: &PyCell<Self>, attr: &PyAny, value: &PyAny) -> PyResult<()> {
107-
slf.as_ref().setattr(attr, value)
108-
}
108+
#[inline(always)]
109+
pub fn calculate_raw(&self, beatmap: &Beatmap) -> CalcResult {
110+
CalcResult(self.calc(beatmap))
111+
}
109112

110-
#[inline(always)]
111-
pub fn set_with_str(&mut self, attr: &str, value: &PyAny) -> PyResult<()> {
112-
crate::set_with_py_str!(self, attr, value; {
113-
mode,
114-
mods,
115-
n50,
116-
n100,
117-
n300,
118-
katu,
119-
acc,
120-
passed_obj,
121-
combo,
122-
miss,
123-
score
124-
});
125-
Ok(())
126-
}
113+
#[inline(always)]
114+
pub fn set_with_str(&mut self, attr: &str, value: &PyAny) -> PyResult<()> {
115+
crate::set_with_py_str!(self, attr, value; {
116+
mode,
117+
mods,
118+
n50,
119+
n100,
120+
n300,
121+
katu,
122+
acc,
123+
passed_obj,
124+
combo,
125+
miss,
126+
score
127+
});
128+
Ok(())
129+
}
127130

128-
#[inline(always)]
129-
pub fn set_with_dict(&mut self, data: &PyDict) -> PyResult<()> {
130-
for (k, v) in data.iter() {
131-
self.set_with_str(&k.extract::<String>()?, v)?;
131+
#[inline(always)]
132+
pub fn set_with_dict(&mut self, data: &PyDict) -> PyResult<()> {
133+
for (k, v) in data.iter() {
134+
self.set_with_str(&k.extract::<String>()?, v)?;
135+
}
136+
Ok(())
132137
}
133-
Ok(())
134-
}
135138

136-
#[getter]
137-
pub fn attrs(&self) -> String {
138-
self.as_string()
139-
}
139+
#[getter]
140+
pub fn attrs(&self) -> String {
141+
self.as_string()
142+
}
140143

141-
#[getter]
142-
pub fn attrs_dict<'a>(&self, py: Python<'a>) -> PyResult<&'a PyDict> {
143-
self.as_dict(py)
144-
}
144+
#[getter]
145+
pub fn attrs_dict<'a>(&self, py: Python<'a>) -> PyResult<&'a PyDict> {
146+
self.as_dict(py)
147+
}
145148

146-
#[getter]
147-
#[inline(always)]
148-
pub fn as_string(&self) -> String {
149-
format!(
150-
"mode: {:?}, mods: {:?}, n50: {:?}, n100: {:?}, n300: {:?}, katu: {:?}, acc: {:?}, passed_obj: {:?}, combo: {:?}, miss: {:?}, score: {:?}",
151-
self.mode,
152-
self.mods,
153-
self.n50,
154-
self.n100,
155-
self.n300,
156-
self.katu,
157-
self.acc,
158-
self.passed_obj,
159-
self.combo,
160-
self.miss,
161-
self.score,
162-
)
163-
}
149+
#[getter]
150+
#[inline(always)]
151+
pub fn as_string(&self) -> String {
152+
format!(
153+
"mode: {:?}, mods: {:?}, n50: {:?}, n100: {:?}, n300: {:?}, katu: {:?}, acc: {:?}, passed_obj: {:?}, combo: {:?}, miss: {:?}, score: {:?}",
154+
self.mode,
155+
self.mods,
156+
self.n50,
157+
self.n100,
158+
self.n300,
159+
self.katu,
160+
self.acc,
161+
self.passed_obj,
162+
self.combo,
163+
self.miss,
164+
self.score,
165+
)
166+
}
164167

165-
#[getter]
166-
#[inline(always)]
167-
pub fn as_dict<'a>(&self, py: Python<'a>) -> PyResult<&'a PyDict> {
168-
let d = crate::pyo3_py_dict!(py, self; {
169-
mode,
170-
mods,
171-
n50,
172-
n100,
173-
n300,
174-
katu,
175-
acc,
176-
passed_obj,
177-
combo,
178-
miss,
179-
score
180-
});
181-
Ok(d)
168+
#[getter]
169+
#[inline(always)]
170+
pub fn as_dict<'a>(&self, py: Python<'a>) -> PyResult<&'a PyDict> {
171+
let d = crate::pyo3_py_dict!(py, self; {
172+
mode,
173+
mods,
174+
n50,
175+
n100,
176+
n300,
177+
katu,
178+
acc,
179+
passed_obj,
180+
combo,
181+
miss,
182+
score
183+
});
184+
Ok(d)
185+
}
182186
}
183-
}
187+
);
184188

185189
impl Calculator {
186190
#[inline(always)]

tests/test_bench_pp_calculates.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ def wrap(): Calculator({'acc': 98.8, 'miss': 3}).calculate(beatmap)
5151
def test_calculate_5(benchmark) -> CalcResult:
5252
def wrap(): Calculator(acc=98.8, miss=3).calculate(beatmap)
5353
benchmark(wrap)
54+
55+
56+
@pytest.mark.benchmark(group="bench-pp-calc")
57+
def test_calculate_6(benchmark) -> CalcResult:
58+
def wrap():
59+
# --
60+
c = Calculator()
61+
c.set_acc(98.8)
62+
c.setattr('miss', 3)
63+
c.calculate(beatmap)
64+
benchmark(wrap)

0 commit comments

Comments
 (0)