Skip to content

Commit

Permalink
Adding clever screen render mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
lodagro authored and wesm committed Dec 8, 2011
1 parent b2138d4 commit 074e88b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ def set_printoptions(precision=None, column_space=None, max_rows=None,
max_columns : int
max_rows and max_columns are used in __repr__() methods to decide if
to_string() or info() is used to render an object to a string.
Either one, or both can be set to 0 (experimental). Pandas will figure
out how big the terminal is and will not display more rows or/and
columns that can fit on it.
"""
global _float_format, _column_space, _max_rows, _max_columns
if precision is not None:
Expand Down
34 changes: 27 additions & 7 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pandas.core.internals import BlockManager, make_block, form_blocks
from pandas.core.series import Series, _is_bool_indexer
from pandas.util import py3compat
from pandas.util.terminal import get_terminal_size
import pandas.core.nanops as nanops
import pandas.core.common as com
import pandas.core.datetools as datetools
Expand Down Expand Up @@ -317,14 +318,33 @@ def __repr__(self):
"""
Return a string representation for a particular DataFrame
"""
buf = StringIO()
if len(self.index) < com._max_rows and \
len(self.columns) <= com._max_columns:
self.to_string(buf=buf)
else:
self.info(buf=buf, verbose=self._verbose_info)
terminal_width, terminal_height = get_terminal_size()
max_rows = terminal_height if com._max_rows == 0 else com._max_rows
max_columns = com._max_columns

return buf.getvalue()
if max_columns > 0:
buf = StringIO()
if len(self.index) < max_rows and \
len(self.columns) <= max_columns:
self.to_string(buf=buf)
else:
self.info(buf=buf, verbose=self._verbose_info)
return buf.getvalue()
else:
if len(self.index) > max_rows:
buf = StringIO()
self.info(buf=buf, verbose=self._verbose_info)
return buf.getvalue()
else:
buf = StringIO()
self.to_string(buf=buf)
value = buf.getvalue()
if max([len(l) for l in value.split('\n')]) <= terminal_width:
return value
else:
buf = StringIO()
self.info(buf=buf, verbose=self._verbose_info)
return buf.getvalue()

def __iter__(self):
"""
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pandas.core.index import Index, MultiIndex, _ensure_index
from pandas.core.indexing import _SeriesIndexer, _maybe_droplevels
from pandas.util import py3compat
from pandas.util.terminal import get_terminal_size
import pandas.core.common as common
import pandas.core.datetools as datetools
import pandas.core.nanops as nanops
Expand Down Expand Up @@ -419,8 +420,10 @@ def __setslice__(self, i, j, value):

def __repr__(self):
"""Clean string representation of a Series"""
if len(self.index) > common._max_rows:
result = self._tidy_repr(min(30, common._max_rows))
width, height = get_terminal_size()
max_rows = height if common._max_rows == 0 else common._max_rows
if len(self.index) > max_rows:
result = self._tidy_repr(min(30, max_rows - 4))
elif len(self.index) > 0:
result = self._get_repr(print_header=True,
length=len(self) > 50,
Expand Down

0 comments on commit 074e88b

Please sign in to comment.