Skip to content

Commit

Permalink
implement pretty printing for IPython
Browse files Browse the repository at this point in the history
...by implementing _repr_pretty_() which is used by IPython.lib.pretty.
add IPython test dependency so that this can be tested.
  • Loading branch information
wbolster committed Jun 30, 2017
1 parent ba25379 commit 3c738bc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
flake8
IPython>=4.2.1
pytest>=3
pytest-cov
9 changes: 9 additions & 0 deletions src/sanest/sanest.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@ def _truncated_repr(self):
return '{}.{.__name__}({})'.format(
__package__, type(self), reprlib.repr(self._data))

def _repr_pretty_(self, p, cycle):
"""Helper for pretty-printing in IPython."""
opening = '{}.{.__name__}('.format(__package__, type(self))
if cycle: # pragma: no cover
p.text(opening + '...)')
else:
with p.group(len(opening), opening, ')'):
p.pretty(self._data)

def __copy__(self):
cls = type(self)
obj = cls.__new__(cls)
Expand Down
7 changes: 7 additions & 0 deletions test_sanest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import sys
import textwrap

import IPython.lib.pretty
import pytest

import sanest
Expand Down Expand Up @@ -1659,3 +1660,9 @@ def dedent(s):
def test_pretty_printing_pprint(input, expected):
actual = pprint.pformat(input)
assert actual == expected


@pytest.mark.parametrize(('input', 'expected'), PRETTY_PRINT_SAMPLES)
def test_pretty_printing_ipython(input, expected):
actual = IPython.lib.pretty.pretty(input)
assert actual == expected

0 comments on commit 3c738bc

Please sign in to comment.