Skip to content

Commit

Permalink
Add a __next__ method to PyFastxReader
Browse files Browse the repository at this point in the history
  • Loading branch information
apcamargo authored Dec 19, 2024
1 parent 1a3cc5a commit 5862c64
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ pub struct PyFastxReader {
#[pymethods]
impl PyFastxReader {
fn __repr__(&self) -> PyResult<String> {
Ok("<FastxParser>".to_string())
Ok("<FastxReader>".to_string())
}

fn __iter__(slf: PyRefMut<Self>, py: Python<'_>) -> PyResult<FastxReaderIterator> {
Ok(FastxReaderIterator { t: slf.into_py(py) })
fn __iter__(slf: PyRefMut<Self>) -> PyRefMut<Self> {
slf
}

fn __next__(mut slf: PyRefMut<Self>) -> PyResult<Option<Record>> {
if let Some(rec) = slf.reader.next() {
let record = py_try!(rec);
Ok(Some(Record::from_sequence_record(&record)))
} else {
Ok(None)
}
}
}

Expand Down Expand Up @@ -74,24 +83,6 @@ impl Record {
}
}

#[pyclass]
pub struct FastxReaderIterator {
t: PyObject,
}

#[pymethods]
impl FastxReaderIterator {
fn __next__(slf: PyRef<Self>, py: Python<'_>) -> PyResult<Option<Record>> {
let mut parser: PyRefMut<PyFastxReader> = slf.t.extract(py)?;
if let Some(rec) = parser.reader.next() {
let record = py_try!(rec);
Ok(Some(Record::from_sequence_record(&record)))
} else {
Ok(None)
}
}
}

// TODO: what would be really nice is to detect the type of pyobject so it would on file object etc
// not for initial release though

Expand Down

0 comments on commit 5862c64

Please sign in to comment.