shr-parser 0.1.5__tar.gz → 0.1.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -292,7 +292,7 @@ dependencies = [
292
292
 
293
293
  [[package]]
294
294
  name = "shr_parser_py"
295
- version = "0.1.5"
295
+ version = "0.1.6"
296
296
  dependencies = [
297
297
  "pyo3",
298
298
  "shr_parser",
@@ -1,7 +1,7 @@
1
1
  [package]
2
2
  name = "shr_parser_py"
3
3
  description = "Python bindings for the shr_parser Rust crate"
4
- version = "0.1.5"
4
+ version = "0.1.6"
5
5
  edition = "2021"
6
6
  license = "GPL-3.0-only"
7
7
  authors = ["Lasse Nielsen <lasse@xerrion.dk>"]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: shr_parser
3
- Version: 0.1.5
3
+ Version: 0.1.6
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -9,7 +9,7 @@ Summary: Parses sweeps from a Signal Hound Spike file.
9
9
  Author: Lasse Nielsen
10
10
  Author-email: Lasse Nielsen <lasse@xerrion.dk>
11
11
  License: GPL-3.0-only
12
- Requires-Python: >=3.8, <=3.12
12
+ Requires-Python: >=3.8, <=3.13
13
13
  Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
14
14
  Project-URL: Source Code, https://github.com/Xerrion/shr_parser_py
15
15
 
@@ -4,7 +4,7 @@ build-backend = "maturin"
4
4
 
5
5
  [project]
6
6
  name = "shr_parser"
7
- requires-python = ">=3.8,<=3.12"
7
+ requires-python = ">=3.8,<=3.13"
8
8
  classifiers = [
9
9
  "Programming Language :: Rust",
10
10
  "Programming Language :: Python :: Implementation :: CPython",
@@ -0,0 +1,154 @@
1
+ use fmt::Display;
2
+ use std::fmt;
3
+ use pyo3::prelude::*;
4
+ 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
+ }
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
+ }
94
+ }
95
+
96
+ #[pymethods]
97
+ impl PySHRParser {
98
+ #[new]
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| {
101
+ pyo3::exceptions::PyIOError::new_err(format!("Failed to parse SHR file: {:?}", e))
102
+ })?;
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)
108
+ }
109
+
110
+ 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
+ })
114
+ }
115
+
116
+ fn get_sweeps(&self) -> PyResult<Vec<PySHRSweep>> {
117
+ let sweeps = self.parser.get_sweeps();
118
+ Ok(sweeps
119
+ .into_iter()
120
+ .map(|sweep| PySHRSweep {
121
+ sweep_number: sweep.sweep_number,
122
+ timestamp: sweep.timestamp,
123
+ frequency: sweep.frequency,
124
+ amplitude: sweep.amplitude,
125
+ })
126
+ .collect())
127
+ }
128
+
129
+ fn get_file_header(&self) -> PyResult<String> {
130
+ let header = self.parser.get_file_header();
131
+ Ok(format!("{:?}", header))
132
+ }
133
+
134
+ fn get_file_path(&self) -> PyResult<String> {
135
+ Ok(self.parser.get_file_path().to_string_lossy().to_string())
136
+ }
137
+ }
138
+
139
+
140
+ /// Create a new SHRParser instance.
141
+ #[pyfunction]
142
+ fn create_parser(file_path: &str, parsing_type: i32) -> PyResult<PySHRParser> {
143
+ PySHRParser::new(file_path, parsing_type.try_into().unwrap())
144
+ }
145
+
146
+ /// 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)?)?;
153
+ Ok(())
154
+ }
@@ -1,61 +0,0 @@
1
- use pyo3::prelude::*;
2
- use pyo3::wrap_pyfunction;
3
- use std::path::PathBuf;
4
- use ::shr_parser::{SHRParser, SHRParsingType};
5
-
6
- /// A wrapper around the SHRParser for Python
7
- #[pyclass]
8
- struct PySHRParser {
9
- parser: SHRParser,
10
- }
11
-
12
- #[pymethods]
13
- impl PySHRParser {
14
- #[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| {
20
- pyo3::exceptions::PyIOError::new_err(format!("Failed to parse SHR file: {:?}", e))
21
- })?;
22
- Ok(PySHRParser { parser })
23
- }
24
-
25
- fn to_csv(&self, path: String) -> PyResult<()> {
26
- self.parser.to_csv(PathBuf::from(path)).map_err(|e| {
27
- pyo3::exceptions::PyIOError::new_err(format!("Failed to write to CSV: {:?}", e))
28
- })
29
- }
30
-
31
- fn get_sweeps(&self) -> PyResult<Vec<(i32, u64, f64, f64)>> {
32
- let sweeps = self.parser.get_sweeps();
33
- Ok(sweeps
34
- .into_iter()
35
- .map(|sweep| (sweep.sweep_number, sweep.timestamp, sweep.frequency, sweep.amplitude))
36
- .collect())
37
- }
38
-
39
- fn get_file_header(&self) -> PyResult<String> {
40
- let header = self.parser.get_file_header();
41
- Ok(format!("{:?}", header))
42
- }
43
-
44
- fn get_file_path(&self) -> PyResult<String> {
45
- Ok(self.parser.get_file_path().to_string_lossy().to_string())
46
- }
47
- }
48
-
49
- /// Create a new SHRParser instance.
50
- #[pyfunction]
51
- fn create_parser(file_path: String, parsing_type: i32) -> PyResult<PySHRParser> {
52
- PySHRParser::new(file_path, parsing_type)
53
- }
54
-
55
- /// A Python module implemented in Rust.
56
- #[pymodule]
57
- fn shr_parser(module: &Bound<'_, PyModule>) -> PyResult<()> {
58
- module.add_class::<PySHRParser>()?;
59
- module.add_function(wrap_pyfunction!(create_parser, module)?)?;
60
- Ok(())
61
- }
File without changes
File without changes
File without changes