Skip to content

Commit

Permalink
series too
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoGorelli committed Jan 1, 2025
1 parent 9d1907c commit 7ecb08f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 54 deletions.
45 changes: 3 additions & 42 deletions narwhals/dataframe.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import os
from typing import TYPE_CHECKING
from typing import Any
from typing import Callable
Expand All @@ -20,6 +19,7 @@
from narwhals.translate import to_native
from narwhals.utils import find_stacklevel
from narwhals.utils import flatten
from narwhals.utils import generate_repr
from narwhals.utils import is_sequence_but_not_str
from narwhals.utils import parse_version

Expand Down Expand Up @@ -415,35 +415,7 @@ def __array__(self, dtype: Any = None, copy: bool | None = None) -> np.ndarray:
return self._compliant_frame.__array__(dtype, copy=copy)

def __repr__(self) -> str: # pragma: no cover
header = "Narwhals DataFrame"
terminal_width = os.get_terminal_size().columns
native_repr = self.to_native().__repr__()
native_lines = native_repr.splitlines()
max_native_width = max(len(line) for line in native_lines)
if max_native_width + 2 < terminal_width:
length = max(max_native_width, len(header))
output = f"┌{'─'*length}\n"
header_extra = length - len(header)
output += f"|{' '*(header_extra//2)}{header}{' '*(header_extra//2 + header_extra%2)}|\n"
output += f"|{'-'*(length)}|\n"
start_extra = (max_native_width - length) // 2
end_extra = (max_native_width - length) // 2 + max_native_width % 2
for line in native_lines:
output += f"|{' '*(start_extra)}{line}{' '*(end_extra + max_native_width - len(line))}|\n"
output += f"└{'─' * length}┘"
return output

length = len(header)
return (
"┌"
+ "─" * length
+ "┐\n"
+ f"|{header}|\n"
+ "| Use `.to_native` to see native output |\n"
+ "└"
+ "─" * length
+ "┘"
)
return generate_repr("Narwhals DataFrame", self.to_native().__repr__())

def __arrow_c_stream__(self, requested_schema: object | None = None) -> object:
"""Export a DataFrame via the Arrow PyCapsule Interface.
Expand Down Expand Up @@ -3599,18 +3571,7 @@ def __init__(
raise AssertionError(msg)

def __repr__(self) -> str: # pragma: no cover
header = " Narwhals LazyFrame "
length = len(header)
return (
"┌"
+ "─" * length
+ "┐\n"
+ f"|{header}|\n"
+ "| Use `.to_native` to see native output |\n"
+ "└"
+ "─" * length
+ "┘"
)
return generate_repr("Narwhals LazyFrame", self.to_native().__repr__())

@property
def implementation(self) -> Implementation:
Expand Down
14 changes: 2 additions & 12 deletions narwhals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from narwhals.dtypes import _validate_dtype
from narwhals.typing import IntoSeriesT
from narwhals.utils import _validate_rolling_arguments
from narwhals.utils import generate_repr
from narwhals.utils import parse_version

if TYPE_CHECKING:
Expand Down Expand Up @@ -404,18 +405,7 @@ def pipe(self, function: Callable[[Any], Self], *args: Any, **kwargs: Any) -> Se
return function(self, *args, **kwargs)

def __repr__(self) -> str: # pragma: no cover
header = " Narwhals Series "
length = len(header)
return (
"┌"
+ "─" * length
+ "┐\n"
+ f"|{header}|\n"
+ "| Use `.to_native()` to see native output |\n"
+ "└"
+ "─" * length
+ "┘"
)
return generate_repr("Narwhals Series", self.to_native().__repr__())

def __len__(self) -> int:
return len(self._compliant_series)
Expand Down
34 changes: 34 additions & 0 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import re
from enum import Enum
from enum import auto
Expand Down Expand Up @@ -960,3 +961,36 @@ def _validate_rolling_arguments(
min_periods = window_size

return window_size, min_periods


def generate_repr(header: str, native_repr: str) -> str:
terminal_width = os.get_terminal_size().columns
native_lines = native_repr.splitlines()
max_native_width = max(len(line) for line in native_lines)

if max_native_width + 2 < terminal_width:
length = max(max_native_width, len(header))
output = f"┌{'─'*length}\n"
header_extra = length - len(header)
output += (
f"|{' '*(header_extra//2)}{header}{' '*(header_extra//2 + header_extra%2)}|\n"
)
output += f"|{'-'*(length)}|\n"
start_extra = (max_native_width - length) // 2
end_extra = (max_native_width - length) // 2 + (max_native_width - length) % 2
for line in native_lines:
output += f"|{' '*(start_extra)}{line}{' '*(end_extra + max_native_width - len(line))}|\n"
output += f"└{'─' * length}┘"
return output

length = len(header)
return (
"┌"
+ "─" * length
+ "┐\n"
+ f"|{header}|\n"
+ "| Use `.to_native` to see native output |\n"
+ "└"
+ "─" * length
+ "┘"
)

0 comments on commit 7ecb08f

Please sign in to comment.