Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a __next__ method to PyFastxReader #92

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading