Skip to content

Commit

Permalink
formatter limit sequence length
Browse files Browse the repository at this point in the history
  • Loading branch information
idanpa committed Mar 8, 2024
1 parent f6921a8 commit 954e9b6
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions calcpy/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from sympy.concrete.expr_with_limits import ExprWithLimits
import IPython.lib.pretty

MAX_SEQ_LENGTH = 100

def _bin_pad(bin_string, pad_every=4):
return ' '.join(bin_string[i:i+pad_every] for i in range(0, len(bin_string), pad_every))

Expand Down Expand Up @@ -137,7 +139,10 @@ def evalf(expr:sympy.Expr):

def evalf_iterable(iterable):
evalu = []
for el in iterable:
for idx, el in enumerate(iterable):
if idx > MAX_SEQ_LENGTH:
evalu.append('...')
break
if isinstance(el, sympy.Expr):
evalu.append(evalf(el))
elif isinstance(el, (list, tuple)):
Expand All @@ -158,12 +163,18 @@ def pretty(obj, n_col=None, n_row=None):
except:
sympy_pretty = str(obj)
if sympy_pretty.count('\n') >= n_row*1.5:
return IPython.lib.pretty.pretty(obj, max_width=n_col)
ipython_pretty = IPython.lib.pretty.pretty(obj, max_width=n_col)
if ipython_pretty.count('\n') < n_row*1.5:
return ipython_pretty
return sympy_pretty

def iterable_formatter(iterable, printer, cycle):
n_col, n_row = shutil.get_terminal_size()

if len(iterable) > MAX_SEQ_LENGTH+1:
iterable = iterable[:MAX_SEQ_LENGTH+1]
iterable[MAX_SEQ_LENGTH] = '...'

pretty_s = pretty(iterable, n_col, n_row)
out = pretty_s

Expand Down

0 comments on commit 954e9b6

Please sign in to comment.