Skip to content

Commit 7005352

Browse files
author
Lasse Nielsen
committed
Huge overhaul
1 parent 2308299 commit 7005352

File tree

4 files changed

+107
-14
lines changed

4 files changed

+107
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
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
@@ -1,7 +1,7 @@
11
[package]
22
name = "shr_parser_py"
33
description = "Python bindings for the shr_parser Rust crate"
4-
version = "0.1.5"
4+
version = "0.1.6"
55
edition = "2021"
66
license = "GPL-3.0-only"
77
authors = ["Lasse Nielsen <lasse@xerrion.dk>"]

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "maturin"
44

55
[project]
66
name = "shr_parser"
7-
requires-python = ">=3.8,<=3.12"
7+
requires-python = ">=3.8,<=3.13"
88
classifiers = [
99
"Programming Language :: Rust",
1010
"Programming Language :: Python :: Implementation :: CPython",

src/lib.rs

Lines changed: 104 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,110 @@
1+
use fmt::Display;
2+
use std::fmt;
13
use pyo3::prelude::*;
24
use pyo3::wrap_pyfunction;
35
use std::path::PathBuf;
46
use ::shr_parser::{SHRParser, SHRParsingType};
57

68
/// A wrapper around the SHRParser for Python
7-
#[pyclass]
9+
#[pyclass(name="SHRParser", subclass)]
810
struct PySHRParser {
911
parser: SHRParser,
12+
parsing_type: PySHRParsingType,
13+
}
14+
15+
#[pyclass(name="SHRSweep")]
16+
struct PySHRSweep {
17+
sweep_number: i32,
18+
timestamp: u64,
19+
frequency: f64,
20+
amplitude: f64,
21+
}
22+
23+
#[pyclass(name="SHRParsingType", eq, eq_int)]
24+
#[derive(Clone, Copy, Debug, PartialEq)]
25+
enum PySHRParsingType {
26+
PEAK = 0,
27+
MEAN = 1,
28+
LOW = 2,
29+
}
30+
31+
impl TryFrom<i32> for PySHRParsingType {
32+
type Error = &'static str;
33+
34+
fn try_from(value: i32) -> Result<Self, Self::Error> {
35+
match value {
36+
0 => Ok(PySHRParsingType::PEAK),
37+
1 => Ok(PySHRParsingType::MEAN),
38+
2 => Ok(PySHRParsingType::LOW),
39+
_ => Err("Invalid value for SHRParsingType"),
40+
}
41+
}
42+
}
43+
44+
impl TryFrom<PySHRParsingType> for SHRParsingType {
45+
type Error = &'static str;
46+
47+
fn try_from(value: PySHRParsingType) -> Result<Self, Self::Error> {
48+
match value {
49+
PySHRParsingType::PEAK => Ok(SHRParsingType::Peak),
50+
PySHRParsingType::MEAN => Ok(SHRParsingType::Mean),
51+
PySHRParsingType::LOW => Ok(SHRParsingType::Low),
52+
}
53+
}
54+
}
55+
56+
impl Display for PySHRParsingType {
57+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58+
match self {
59+
PySHRParsingType::PEAK => write!(f, "SHRParsingType.PEAK"),
60+
PySHRParsingType::MEAN => write!(f, "SHRParsingType.MEAN"),
61+
PySHRParsingType::LOW => write!(f, "SHRParsingType.LOW"),
62+
}
63+
}
64+
}
65+
66+
#[pymethods]
67+
impl PySHRSweep {
68+
fn __repr__(&self) -> String {
69+
format!(
70+
"SHRSweep(sweep_number={}, timestamp={}, frequency={}, amplitude={})",
71+
self.sweep_number, self.timestamp, self.frequency, self.amplitude
72+
)
73+
}
74+
75+
#[getter]
76+
fn sweep_number(&self) -> i32 {
77+
self.sweep_number
78+
}
79+
80+
#[getter]
81+
fn timestamp(&self) -> u64 {
82+
self.timestamp
83+
}
84+
85+
#[getter]
86+
fn frequency(&self) -> f64 {
87+
self.frequency
88+
}
89+
90+
#[getter]
91+
fn amplitude(&self) -> f64 {
92+
self.amplitude
93+
}
1094
}
1195

1296
#[pymethods]
1397
impl PySHRParser {
1498
#[new]
15-
fn new(file_path: String, parsing_type: i32) -> PyResult<Self> {
16-
let parsing_type = SHRParsingType::try_from(parsing_type).map_err(|e| {
17-
pyo3::exceptions::PyValueError::new_err(format!("Invalid parsing type: {}", e))
18-
})?;
19-
let parser = SHRParser::new(PathBuf::from(file_path), parsing_type).map_err(|e| {
99+
fn new(file_path: &str, parsing_type: PySHRParsingType) -> PyResult<Self> {
100+
let parser = SHRParser::new(PathBuf::from(file_path), SHRParsingType::try_from(parsing_type).unwrap()).map_err(|e| {
20101
pyo3::exceptions::PyIOError::new_err(format!("Failed to parse SHR file: {:?}", e))
21102
})?;
22-
Ok(PySHRParser { parser })
103+
Ok(PySHRParser { parser, parsing_type })
104+
}
105+
106+
fn __repr__(&self) -> String {
107+
format!("SHRParser(file_path='{}', parsing_type={})", self.parser.get_file_path().to_string_lossy(), self.parsing_type)
23108
}
24109

25110
fn to_csv(&self, path: String) -> PyResult<()> {
@@ -28,11 +113,16 @@ impl PySHRParser {
28113
})
29114
}
30115

31-
fn get_sweeps(&self) -> PyResult<Vec<(i32, u64, f64, f64)>> {
116+
fn get_sweeps(&self) -> PyResult<Vec<PySHRSweep>> {
32117
let sweeps = self.parser.get_sweeps();
33118
Ok(sweeps
34119
.into_iter()
35-
.map(|sweep| (sweep.sweep_number, sweep.timestamp, sweep.frequency, sweep.amplitude))
120+
.map(|sweep| PySHRSweep {
121+
sweep_number: sweep.sweep_number,
122+
timestamp: sweep.timestamp,
123+
frequency: sweep.frequency,
124+
amplitude: sweep.amplitude,
125+
})
36126
.collect())
37127
}
38128

@@ -46,16 +136,19 @@ impl PySHRParser {
46136
}
47137
}
48138

139+
49140
/// Create a new SHRParser instance.
50141
#[pyfunction]
51-
fn create_parser(file_path: String, parsing_type: i32) -> PyResult<PySHRParser> {
52-
PySHRParser::new(file_path, parsing_type)
142+
fn create_parser(file_path: &str, parsing_type: i32) -> PyResult<PySHRParser> {
143+
PySHRParser::new(file_path, parsing_type.try_into().unwrap())
53144
}
54145

55146
/// A Python module implemented in Rust.
56147
#[pymodule]
57148
fn shr_parser(module: &Bound<'_, PyModule>) -> PyResult<()> {
58149
module.add_class::<PySHRParser>()?;
150+
module.add_class::<PySHRSweep>()?;
151+
module.add_class::<PySHRParsingType>()?;
59152
module.add_function(wrap_pyfunction!(create_parser, module)?)?;
60153
Ok(())
61154
}

0 commit comments

Comments
 (0)