Skip to content

Commit

Permalink
Make readers python iterators (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jul 31, 2024
1 parent ebadc3d commit b582f3b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions arro3-core/python/arro3/core/_core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Array:

class ArrayReader:
def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ...
def __iter__(self) -> ArrayReader: ...
def __next__(self) -> Array: ...
def __repr__(self) -> str: ...
@classmethod
def from_arrow(
Expand Down Expand Up @@ -725,6 +727,8 @@ class RecordBatch:

class RecordBatchReader:
def __arrow_c_stream__(self, requested_schema: object | None = None) -> object: ...
def __iter__(self) -> RecordBatchReader: ...
def __next__(self) -> RecordBatch: ...
def __repr__(self) -> str: ...
@classmethod
def from_arrow(
Expand Down
10 changes: 10 additions & 0 deletions pyo3-arrow/src/array_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ impl PyArrayReader {
to_stream_pycapsule(py, array_reader, requested_schema)
}

// Return self
// https://stackoverflow.com/a/52056290
fn __iter__(&mut self, py: Python) -> PyResult<PyObject> {
self.to_arro3(py)
}

fn __next__(&mut self, py: Python) -> PyArrowResult<PyObject> {
self.read_next_array(py)
}

pub fn __repr__(&self) -> String {
self.to_string()
}
Expand Down
10 changes: 10 additions & 0 deletions pyo3-arrow/src/record_batch_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ impl PyRecordBatchReader {
to_stream_pycapsule(py, array_reader, requested_schema)
}

// Return self
// https://stackoverflow.com/a/52056290
fn __iter__(&mut self, py: Python) -> PyResult<PyObject> {
self.to_arro3(py)
}

fn __next__(&mut self, py: Python) -> PyArrowResult<PyObject> {
self.read_next_batch(py)
}

pub fn __repr__(&self) -> String {
self.to_string()
}
Expand Down

0 comments on commit b582f3b

Please sign in to comment.