Skip to content

Commit 3c3a41d

Browse files
committed
Did stuff
1 parent 531f856 commit 3c3a41d

File tree

4 files changed

+76
-137
lines changed

4 files changed

+76
-137
lines changed

Cargo.lock

Lines changed: 14 additions & 71 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
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.6"
4+
version = "0.1.7"
55
edition = "2021"
66
license = "GPL-3.0-only"
77
authors = ["Lasse Nielsen <lasse@xerrion.dk>"]
@@ -15,5 +15,5 @@ crate-type = ["cdylib"]
1515
name = "shr_parser"
1616

1717
[dependencies]
18-
pyo3 = { version = "0.22.1", features = ["extension-module"] }
19-
shr_parser = "1.0.5"
18+
pyo3 = { version = "0.23.4", features = ["extension-module"] }
19+
shr_parser = "1.0.8"

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.13"
7+
requires-python = ">=3.8,<4"
88
classifiers = [
99
"Programming Language :: Rust",
1010
"Programming Language :: Python :: Implementation :: CPython",

src/lib.rs

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
1-
use fmt::Display;
2-
use std::fmt;
1+
use std::fmt::{self, Display};
2+
use std::path::PathBuf;
33
use pyo3::prelude::*;
44
use pyo3::wrap_pyfunction;
5-
use std::path::PathBuf;
6-
use ::shr_parser::{SHRParser, SHRParsingType};
7-
8-
/// A wrapper around the SHRParser for Python
9-
#[pyclass(name="SHRParser", subclass)]
10-
struct PySHRParser {
11-
parser: SHRParser,
12-
parsing_type: PySHRParsingType,
13-
}
5+
use shr_parser::{SHRParser, SHRParsingType};
146

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)]
7+
/// An enum mirroring SHRParsingType for Python.
8+
#[pyclass(name = "SHRParsingType", eq, eq_int)]
249
#[derive(Clone, Copy, Debug, PartialEq)]
2510
enum PySHRParsingType {
2611
PEAK = 0,
@@ -30,7 +15,6 @@ enum PySHRParsingType {
3015

3116
impl TryFrom<i32> for PySHRParsingType {
3217
type Error = &'static str;
33-
3418
fn try_from(value: i32) -> Result<Self, Self::Error> {
3519
match value {
3620
0 => Ok(PySHRParsingType::PEAK),
@@ -43,12 +27,11 @@ impl TryFrom<i32> for PySHRParsingType {
4327

4428
impl TryFrom<PySHRParsingType> for SHRParsingType {
4529
type Error = &'static str;
46-
4730
fn try_from(value: PySHRParsingType) -> Result<Self, Self::Error> {
4831
match value {
4932
PySHRParsingType::PEAK => Ok(SHRParsingType::Peak),
5033
PySHRParsingType::MEAN => Ok(SHRParsingType::Mean),
51-
PySHRParsingType::LOW => Ok(SHRParsingType::Low),
34+
PySHRParsingType::LOW => Ok(SHRParsingType::Low),
5235
}
5336
}
5437
}
@@ -58,11 +41,28 @@ impl Display for PySHRParsingType {
5841
match self {
5942
PySHRParsingType::PEAK => write!(f, "SHRParsingType.PEAK"),
6043
PySHRParsingType::MEAN => write!(f, "SHRParsingType.MEAN"),
61-
PySHRParsingType::LOW => write!(f, "SHRParsingType.LOW"),
44+
PySHRParsingType::LOW => write!(f, "SHRParsingType.LOW"),
6245
}
6346
}
6447
}
6548

49+
/// A Python wrapper for a sweep.
50+
#[pyclass(name = "SHRSweep")]
51+
struct PySHRSweep {
52+
#[pyo3(get)]
53+
sweep_number: i32,
54+
#[pyo3(get)]
55+
timestamp: u64,
56+
#[pyo3(get)]
57+
frequency: f64,
58+
#[pyo3(get)]
59+
amplitude: f64,
60+
#[pyo3(get)]
61+
latitude: f64,
62+
#[pyo3(get)]
63+
longitude: f64,
64+
}
65+
6666
#[pymethods]
6767
impl PySHRSweep {
6868
fn __repr__(&self) -> String {
@@ -71,57 +71,51 @@ impl PySHRSweep {
7171
self.sweep_number, self.timestamp, self.frequency, self.amplitude
7272
)
7373
}
74+
}
7475

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-
}
76+
/// A Python wrapper around SHRParser.
77+
#[pyclass(name = "SHRParser", subclass)]
78+
struct PySHRParser {
79+
parser: SHRParser,
80+
parsing_type: PySHRParsingType,
9481
}
9582

9683
#[pymethods]
9784
impl PySHRParser {
9885
#[new]
9986
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| {
101-
pyo3::exceptions::PyIOError::new_err(format!("Failed to parse SHR file: {:?}", e))
102-
})?;
87+
let shr_parsing = SHRParsingType::try_from(parsing_type)
88+
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e))?;
89+
let parser = SHRParser::new(PathBuf::from(file_path), shr_parsing)
90+
.map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to parse SHR file: {:?}", e)))?;
10391
Ok(PySHRParser { parser, parsing_type })
10492
}
10593

10694
fn __repr__(&self) -> String {
107-
format!("SHRParser(file_path='{}', parsing_type={})", self.parser.get_file_path().to_string_lossy(), self.parsing_type)
95+
format!(
96+
"SHRParser(file_path='{}', parsing_type={})",
97+
self.parser.get_file_path().to_string_lossy(),
98+
self.parsing_type
99+
)
108100
}
109101

110102
fn to_csv(&self, path: String) -> PyResult<()> {
111-
self.parser.to_csv(PathBuf::from(path)).map_err(|e| {
112-
pyo3::exceptions::PyIOError::new_err(format!("Failed to write to CSV: {:?}", e))
113-
})
103+
self.parser
104+
.to_csv(PathBuf::from(path))
105+
.map_err(|e| pyo3::exceptions::PyIOError::new_err(format!("Failed to write to CSV: {:?}", e)))
114106
}
115107

116108
fn get_sweeps(&self) -> PyResult<Vec<PySHRSweep>> {
117109
let sweeps = self.parser.get_sweeps();
118110
Ok(sweeps
119111
.into_iter()
120112
.map(|sweep| PySHRSweep {
121-
sweep_number: sweep.sweep_number,
122-
timestamp: sweep.timestamp,
113+
sweep_number: sweep.number,
114+
timestamp: sweep.header.timestamp,
123115
frequency: sweep.frequency,
124116
amplitude: sweep.amplitude,
117+
latitude: sweep.header.latitude,
118+
longitude: sweep.header.longitude,
125119
})
126120
.collect())
127121
}
@@ -132,23 +126,25 @@ impl PySHRParser {
132126
}
133127

134128
fn get_file_path(&self) -> PyResult<String> {
135-
Ok(self.parser.get_file_path().to_string_lossy().to_string())
129+
Ok(self.parser.get_file_path().to_string_lossy().into_owned())
136130
}
137131
}
138132

139-
140133
/// Create a new SHRParser instance.
141134
#[pyfunction]
142135
fn create_parser(file_path: &str, parsing_type: i32) -> PyResult<PySHRParser> {
143-
PySHRParser::new(file_path, parsing_type.try_into().unwrap())
136+
let py_parsing_type: PySHRParsingType = parsing_type
137+
.try_into()
138+
.map_err(|e| pyo3::exceptions::PyValueError::new_err(e))?;
139+
PySHRParser::new(file_path, py_parsing_type)
144140
}
145141

146142
/// A Python module implemented in Rust.
147-
#[pymodule]
148-
fn shr_parser(module: &Bound<'_, PyModule>) -> PyResult<()> {
149-
module.add_class::<PySHRParser>()?;
150-
module.add_class::<PySHRSweep>()?;
151-
module.add_class::<PySHRParsingType>()?;
152-
module.add_function(wrap_pyfunction!(create_parser, module)?)?;
143+
#[pymodule(name="shr_parser")]
144+
fn shr_parser_py(m: &Bound<PyModule>) -> PyResult<()> {
145+
m.add_class::<PySHRParser>()?;
146+
m.add_class::<PySHRSweep>()?;
147+
m.add_class::<PySHRParsingType>()?;
148+
m.add_function(wrap_pyfunction!(create_parser, m)?)?;
153149
Ok(())
154-
}
150+
}

0 commit comments

Comments
 (0)