Skip to content

Commit

Permalink
implement basic __repr__ on classes (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron authored Jul 10, 2024
1 parent 235476b commit 137ed29
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pyo3-arrow/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;
use std::sync::Arc;

use arrow::ffi::{FFI_ArrowArray, FFI_ArrowSchema};
Expand Down Expand Up @@ -95,6 +96,15 @@ impl AsRef<ArrayRef> for PyArray {
}
}

impl Display for PyArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "arro3.Array<")?;
self.array.data_type().fmt(f)?;
writeln!(f, ">")?;
Ok(())
}
}

#[pymethods]
impl PyArray {
/// An implementation of the Array interface, for interoperability with numpy and other
Expand Down Expand Up @@ -138,6 +148,10 @@ impl PyArray {
self.array.len()
}

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

/// Construct this object from an existing Arrow object.
///
/// It can be called on anything that exports the Arrow data interface
Expand Down
14 changes: 14 additions & 0 deletions pyo3-arrow/src/chunked.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;
use std::sync::Arc;

use arrow_array::{make_array, Array, ArrayRef};
Expand Down Expand Up @@ -114,6 +115,15 @@ impl AsRef<[ArrayRef]> for PyChunkedArray {
}
}

impl Display for PyChunkedArray {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "arro3.ChunkedArray<")?;
self.field.data_type().fmt(f)?;
writeln!(f, ">")?;
Ok(())
}
}

#[pymethods]
impl PyChunkedArray {
/// An implementation of the Array interface, for interoperability with numpy and other
Expand Down Expand Up @@ -158,6 +168,10 @@ impl PyChunkedArray {
self.chunks.iter().fold(0, |acc, x| acc + x.len())
}

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

/// Copy this array to a `numpy` NDArray
pub fn to_numpy(&self, py: Python) -> PyResult<PyObject> {
self.__array__(py)
Expand Down
19 changes: 19 additions & 0 deletions pyo3-arrow/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;
use std::sync::Arc;

use arrow::ffi::FFI_ArrowSchema;
Expand Down Expand Up @@ -68,6 +69,20 @@ impl AsRef<Field> for PyField {
}
}

impl Display for PyField {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "arro3.Field<")?;
f.write_str(self.0.name().as_str())?;
write!(f, ": ")?;
self.0.data_type().fmt(f)?;
if !self.0.is_nullable() {
write!(f, " not null")?;
}
writeln!(f, ">")?;
Ok(())
}
}

#[pymethods]
impl PyField {
/// An implementation of the [Arrow PyCapsule
Expand All @@ -91,6 +106,10 @@ impl PyField {
self.0 == other.0
}

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

/// Construct this from an existing Arrow object.
///
/// It can be called on anything that exports the Arrow schema interface
Expand Down
14 changes: 14 additions & 0 deletions pyo3-arrow/src/record_batch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;
use std::sync::Arc;

use arrow::array::AsArray;
Expand All @@ -13,6 +14,7 @@ use pyo3::types::{PyCapsule, PyTuple, PyType};
use crate::error::PyArrowResult;
use crate::ffi::from_python::utils::import_array_pycapsules;
use crate::ffi::to_python::nanoarrow::to_nanoarrow_array;
use crate::schema::display_schema;

/// A Python-facing Arrow record batch.
///
Expand Down Expand Up @@ -77,6 +79,14 @@ impl AsRef<RecordBatch> for PyRecordBatch {
}
}

impl Display for PyRecordBatch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "arro3.RecordBatch")?;
writeln!(f, "-----------------")?;
display_schema(&self.0.schema(), f)
}
}

#[pymethods]
impl PyRecordBatch {
/// An implementation of the [Arrow PyCapsule
Expand Down Expand Up @@ -109,6 +119,10 @@ impl PyRecordBatch {
self.0 == other.0
}

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

/// Construct this from an existing Arrow RecordBatch.
///
/// It can be called on anything that exports the Arrow data interface
Expand Down
18 changes: 18 additions & 0 deletions pyo3-arrow/src/record_batch_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;

use arrow::ffi_stream::FFI_ArrowArrayStream;
use arrow_array::RecordBatchReader;
Expand All @@ -11,6 +12,7 @@ use pyo3::types::{PyCapsule, PyTuple, PyType};
use crate::error::PyArrowResult;
use crate::ffi::from_python::utils::import_stream_pycapsule;
use crate::ffi::to_python::nanoarrow::to_nanoarrow_array_stream;
use crate::schema::display_schema;
use crate::{PySchema, PyTable};

/// A Python-facing Arrow record batch reader.
Expand Down Expand Up @@ -97,6 +99,18 @@ impl From<Box<dyn RecordBatchReader + Send>> for PyRecordBatchReader {
}
}

impl Display for PyRecordBatchReader {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "arro3.RecordBatchReader")?;
writeln!(f, "-----------------------")?;
if let Ok(schema) = self.schema_ref() {
display_schema(&schema, f)
} else {
writeln!(f, "Closed stream")
}
}
}

#[pymethods]
impl PyRecordBatchReader {
/// An implementation of the [Arrow PyCapsule
Expand All @@ -122,6 +136,10 @@ impl PyRecordBatchReader {
PyCapsule::new_bound(py, ffi_stream, Some(stream_capsule_name))
}

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

/// Returns `true` if this reader has already been consumed.
pub fn closed(&self) -> bool {
self.0.is_none()
Expand Down
24 changes: 24 additions & 0 deletions pyo3-arrow/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;
use std::sync::Arc;

use arrow::ffi::FFI_ArrowSchema;
Expand Down Expand Up @@ -68,6 +69,25 @@ impl AsRef<Schema> for PySchema {
}
}

impl Display for PySchema {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "arro3.Schema")?;
writeln!(f, "------------")?;
display_schema(&self.0, f)
}
}

pub(crate) fn display_schema(schema: &Schema, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
schema.fields().iter().try_for_each(|field| {
f.write_str(field.name().as_str())?;
write!(f, ": ")?;
field.data_type().fmt(f)?;
writeln!(f)?;
Ok::<_, std::fmt::Error>(())
})?;
Ok(())
}

#[pymethods]
impl PySchema {
/// An implementation of the [Arrow PyCapsule
Expand All @@ -84,6 +104,10 @@ impl PySchema {
Ok(schema_capsule)
}

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

/// Construct this object from an existing Arrow object
///
/// It can be called on anything that exports the Arrow data interface
Expand Down
14 changes: 14 additions & 0 deletions pyo3-arrow/src/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::ffi::CString;
use std::fmt::Display;

use arrow::ffi_stream::ArrowArrayStreamReader as ArrowRecordBatchStreamReader;
use arrow::ffi_stream::FFI_ArrowArrayStream;
Expand All @@ -12,6 +13,7 @@ use pyo3::types::{PyCapsule, PyTuple, PyType};

use crate::ffi::from_python::utils::import_stream_pycapsule;
use crate::ffi::to_python::nanoarrow::to_nanoarrow_array_stream;
use crate::schema::display_schema;
use crate::PySchema;

/// A Python-facing Arrow table.
Expand Down Expand Up @@ -64,6 +66,14 @@ impl PyTable {
}
}

impl Display for PyTable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "arro3.Table")?;
writeln!(f, "-----------")?;
display_schema(&self.schema, f)
}
}

#[pymethods]
impl PyTable {
/// An implementation of the [Arrow PyCapsule
Expand Down Expand Up @@ -99,6 +109,10 @@ impl PyTable {
self.batches.iter().fold(0, |acc, x| acc + x.num_rows())
}

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

/// Construct this object from an existing Arrow object.
///
/// It can be called on anything that exports the Arrow stream interface
Expand Down

0 comments on commit 137ed29

Please sign in to comment.