Skip to content

Commit 82ddd28

Browse files
committed
refactor: rename and restructure formatter functions for clarity and maintainability
1 parent 620d25e commit 82ddd28

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

src/dataframe.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,37 +94,31 @@ impl Default for FormatterConfig {
9494
}
9595

9696
/// Get the Python formatter from the datafusion.html_formatter module
97-
fn get_python_formatter(py: Python) -> PyResult<Bound<'_, PyAny>> {
97+
fn import_python_formatter(py: Python) -> PyResult<Bound<'_, PyAny>> {
9898
let formatter_module = py.import("datafusion.html_formatter")?;
9999
let get_formatter = formatter_module.getattr("get_formatter")?;
100100
get_formatter.call0()
101101
}
102+
// Helper function to extract attributes with fallback to default
103+
fn get_attr<'a>(py_object: &'a Bound<'a, PyAny>, attr_name: &str, default_value: usize) -> usize {
104+
py_object
105+
.getattr(attr_name)
106+
.and_then(|v| v.extract::<usize>())
107+
.unwrap_or(default_value)
108+
}
102109

103-
fn get_formatter_config(py: Python) -> PyResult<FormatterConfig> {
104-
let formatter = get_python_formatter(py)?;
105-
106-
// Helper function to extract attributes with fallback to default
107-
fn get_attr<'a>(
108-
formatter: &'a Bound<'a, PyAny>,
109-
attr_name: &str,
110-
default_value: usize,
111-
) -> usize {
112-
formatter
113-
.getattr(attr_name)
114-
.and_then(|v| v.extract::<usize>())
115-
.unwrap_or(default_value)
116-
}
117-
110+
/// Helper function to create a FormatterConfig from a Python formatter object
111+
fn build_formatter_config_from_python(formatter: &Bound<'_, PyAny>) -> FormatterConfig {
118112
let default_config = FormatterConfig::default();
119-
let max_bytes = get_attr(&formatter, "max_memory_bytes", default_config.max_bytes);
120-
let min_rows = get_attr(&formatter, "min_rows_display", default_config.min_rows);
121-
let repr_rows = get_attr(&formatter, "repr_rows", default_config.repr_rows);
113+
let max_bytes = get_attr(formatter, "max_memory_bytes", default_config.max_bytes);
114+
let min_rows = get_attr(formatter, "min_rows_display", default_config.min_rows);
115+
let repr_rows = get_attr(formatter, "repr_rows", default_config.repr_rows);
122116

123-
Ok(FormatterConfig {
117+
FormatterConfig {
124118
max_bytes,
125119
min_rows,
126120
repr_rows,
127-
})
121+
}
128122
}
129123

130124
/// A PyDataFrame is a representation of a logical plan and an API to compose statements.
@@ -167,7 +161,9 @@ impl PyDataFrame {
167161
}
168162

169163
fn __repr__(&self, py: Python) -> PyDataFusionResult<String> {
170-
let config = get_formatter_config(py)?;
164+
// Get the Python formatter module and call format_html
165+
let formatter = import_python_formatter(py)?;
166+
let config = build_formatter_config_from_python(&formatter);
171167
let (batches, has_more) = wait_for_future(
172168
py,
173169
collect_record_batches_to_display(self.df.as_ref().clone(), config),
@@ -189,7 +185,10 @@ impl PyDataFrame {
189185
}
190186

191187
fn _repr_html_(&self, py: Python) -> PyDataFusionResult<String> {
192-
let config = get_formatter_config(py)?;
188+
// Get the Python formatter module and call format_html
189+
let formatter = import_python_formatter(py)?;
190+
let config = build_formatter_config_from_python(&formatter);
191+
193192
let (batches, has_more) = wait_for_future(
194193
py,
195194
collect_record_batches_to_display(self.df.as_ref().clone(), config),
@@ -209,9 +208,6 @@ impl PyDataFrame {
209208

210209
let py_schema = self.schema().into_pyobject(py)?;
211210

212-
// Get the Python formatter module and call format_html
213-
let formatter = get_python_formatter(py)?;
214-
215211
// Call format_html method on the formatter
216212
let kwargs = pyo3::types::PyDict::new(py);
217213
let py_batches_list = PyList::new(py, py_batches.as_slice())?;

0 commit comments

Comments
 (0)