Skip to content

Commit

Permalink
displaytools: replace print("---") by display(...) (workarround for i…
Browse files Browse the repository at this point in the history
…ssue #4)
  • Loading branch information
cknoll committed Feb 12, 2020
1 parent 81b8af8 commit 84368ce
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
14 changes: 8 additions & 6 deletions ipydex/displaytools.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,19 @@ def process_line(line, line_flags, expr_to_disp, indent):
if line_flags.transpose:
expr_to_disp = "{}.T".format(expr_to_disp)

print_delim = 'display({{"text/plain": "{}"}}, raw=True)'.format(delim)

if line_flags.lhs:
if line_flags.shape:
new_line = '{}custom_display("{}.shape", {}.shape); print("{}")'
new_line = new_line.format(indent, expr_to_disp, expr_to_disp, delim)
new_line = '{}custom_display("{}.shape", {}.shape); {}'
new_line = new_line.format(indent, expr_to_disp, expr_to_disp, print_delim)
elif line_flags.info:
new_line = '{}custom_display("info({})", _ipydex__info({})); print("{}")'
new_line = new_line.format(indent, expr_to_disp, expr_to_disp, delim)
new_line = '{}custom_display("info({})", _ipydex__info({})); {}'
new_line = new_line.format(indent, expr_to_disp, expr_to_disp, print_delim)
else:
new_line = '{}custom_display("{}", {}); print("{}")'.format(indent, expr_to_disp, expr_to_disp, delim)
new_line = '{}custom_display("{}", {}); {}'.format(indent, expr_to_disp, expr_to_disp, print_delim)
else:
new_line = '{}display({}); print("{}")'.format(indent, expr_to_disp, delim)
new_line = '{}display({}); {}'.format(indent, expr_to_disp, print_delim)

return new_line

Expand Down
2 changes: 1 addition & 1 deletion ipydex/release.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.11.0"
__version__ = "0.11.1"
44 changes: 22 additions & 22 deletions ipydex/test/test_displaytools.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def test_insert_disp_lines1(self):
eres1 = """\
x = 0
y = 1 ##:
custom_display("y", y); print("---")
custom_display("y", y); display({"text/plain": "---"}, raw=True)
z = 0
"""

Expand All @@ -183,7 +183,7 @@ def test_insert_disp_lines1(self):
eres1 = """\
x = 0
y = 1 # more comments ##: more comments
custom_display("y", y); print("---")
custom_display("y", y); display({"text/plain": "---"}, raw=True)
z = 0
"""

Expand Down Expand Up @@ -216,7 +216,7 @@ def test_insert_disp_lines1(self):
x = 0
if 1:
y = 1 ##:
custom_display("y", y); print("---")
custom_display("y", y); display({"text/plain": "---"}, raw=True)
z = 0
"""

Expand All @@ -235,8 +235,8 @@ def test_insert_disp_lines1(self):
eres1 = """\
x = 0
if 1:
custom_display("(y)", (y)); print("---")
custom_display("(C.xyz)", (C.xyz)); print("---")
custom_display("(y)", (y)); display({"text/plain": "---"}, raw=True)
custom_display("(C.xyz)", (C.xyz)); display({"text/plain": "---"}, raw=True)
"""

res1 = dt.insert_disp_lines(raw_cell1)
Expand All @@ -260,16 +260,16 @@ def test_insert_disp_lines1(self):
x = 0
if 1:
y1, y2 = 1, 2 ##:
custom_display("(y1, y2)", (y1, y2)); print("---")
custom_display("(y1, y2)", (y1, y2)); display({"text/plain": "---"}, raw=True)
y1, y2 = yy = 1, 2 ##:
custom_display("yy", yy); print("---")
custom_display("yy", yy); display({"text/plain": "---"}, raw=True)
yy = y1, y2 = 1, 2 ##:
custom_display("(y1, y2)", (y1, y2)); print("---")
custom_display("(y1, y2)", (y1, y2)); display({"text/plain": "---"}, raw=True)
y1, y2 = yy = 1, 2 ##:T
custom_display("yy.T", yy.T); print("---")
custom_display("yy.T", yy.T); display({"text/plain": "---"}, raw=True)
z = 0
"""

Expand Down Expand Up @@ -301,10 +301,10 @@ def func(a, b):

eres1 = """\
x = func1(a=a, b = b) ##:
custom_display("x", x); print("---")
custom_display("x", x); display({"text/plain": "---"}, raw=True)
if 1:
y = func2(a=a, b = b) ##:T
custom_display("y.T", y.T); print("---")
custom_display("y.T", y.T); display({"text/plain": "---"}, raw=True)
"""

res1 = dt.insert_disp_lines(raw_cell1)
Expand All @@ -322,7 +322,7 @@ def test_insert_disp_lines2(self):

eres1 = """\
y = x ##:S
custom_display("y.shape", y.shape); print("---")
custom_display("y.shape", y.shape); display({"text/plain": "---"}, raw=True)
z = 0
"""

Expand All @@ -338,7 +338,7 @@ def test_insert_disp_lines2(self):

eres1 = """\
y = x ##:i
custom_display("info(y)", _ipydex__info(y)); print("---")
custom_display("info(y)", _ipydex__info(y)); display({"text/plain": "---"}, raw=True)
z = 0
"""

Expand All @@ -355,9 +355,9 @@ def test_insert_disp_lines3(self):
"""
eres1 = """\
y=x ##:
custom_display("y", y); print("---")
custom_display("y", y); display({"text/plain": "---"}, raw=True)
y=x ##:i
custom_display("info(y)", _ipydex__info(y)); print("---")
custom_display("info(y)", _ipydex__info(y)); display({"text/plain": "---"}, raw=True)
z = 0
"""
res1 = dt.insert_disp_lines(raw_cell1)
Expand Down Expand Up @@ -512,12 +512,12 @@ def test_demo5(self):
np.random.seed(0)
A = np.random.random((5, 4)) ##:
custom_display("A", A); print("---")
custom_display("(np.matrix(A))", (np.matrix(A))); print("---")
custom_display("A", A); display({"text/plain": "---"}, raw=True)
custom_display("(np.matrix(A))", (np.matrix(A))); display({"text/plain": "---"}, raw=True)
# heuristic to insert a newline if indentation would be too long
A_with_long_name = A ##:
custom_display("A_with_long_name", A_with_long_name); print("---")
custom_display("A_with_long_name", A_with_long_name); display({"text/plain": "---"}, raw=True)
"""

Expand All @@ -544,10 +544,10 @@ def test_demo4(self):
# van-der-Pol-Oszillator
f = sp.Matrix([x2, (1-x1**2)*x2 - x1])
y = h = x1
custom_display("(selector)", (selector)); print("---")
custom_display("(selector)", (selector)); display({"text/plain": "---"}, raw=True)
n = len(f)
custom_display("(f)", (f)); print("---")
custom_display("(y)", (y)); print("---")
custom_display("(f)", (f)); display({"text/plain": "---"}, raw=True)
custom_display("(y)", (y)); display({"text/plain": "---"}, raw=True)
"""
ll = dt.get_logical_lines_of_cell(raw_cell1)
res1 = dt.insert_disp_lines(raw_cell1)
Expand All @@ -563,7 +563,7 @@ def testLL3(self):
eres1 = """\
xx = sp.Matrix(sp.symbols('x1:11'))
yy = sp.Matrix(sp.symbols('y1:11'))
custom_display("(xx.shape, yy.shape)", (xx.shape, yy.shape)); print("---")
custom_display("(xx.shape, yy.shape)", (xx.shape, yy.shape)); display({"text/plain": "---"}, raw=True)
"""

ll = dt.get_logical_lines_of_cell(raw_cell1)
Expand Down

0 comments on commit 84368ce

Please sign in to comment.