コード例 #1
0
    def test_get_lhs_from_ast(self):
        s1 = "x, y = 123, 789 ##:"
        s2 = "C.x, y = 123, 789 ##:"

        ll1 = dt.get_logical_lines_of_cell(s1)[0]
        ll2 = dt.get_logical_lines_of_cell(s2)[0]

        ast1 = dt.ast.parse(s1).body[0]
        ast2 = dt.ast.parse(s2).body[0]

        lhs1 = dt.get_lhs_from_ast(ast1)
        lhs2 = dt.get_lhs_from_ast(ast2)

        self.assertEqual(lhs1, "x, y")
        self.assertEqual(lhs2, "C.x, y")
コード例 #2
0
    def test_demo2(self):
        raw_cell1 = """\
if 1:
    # van-der-Pol-Oszillator
    f = sp.Matrix([x2, (1-x1**2)*x2 - x1])
    y = h = x1


selector ##:
n = len(f)
f ##:
y ##:
"""

        eres1 = """\
if 1:
    # van-der-Pol-Oszillator
    f = sp.Matrix([x2, (1-x1**2)*x2 - x1])
    y = h = x1
custom_display("(selector)", (selector)); display({"text/plain": "---"}, raw=True)
n = len(f)
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)
        self.assertEqual(res1, eres1)
コード例 #3
0
    def test_demo1(self):
        raw_cell1 = """\
# numpy arrays and matrices are indented to take left-hand-side into account


np.random.seed(0)

A = np.random.random((5, 4)) ##:
np.matrix(A) ##:

# heuristic to insert a newline if indentation would be too long
A_with_long_name = A ##:

"""

        eres1 = """\
# numpy arrays and matrices are indented to take left-hand-side into account


np.random.seed(0)

A = np.random.random((5, 4)) ##:
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); display({"text/plain": "---"}, raw=True)

"""

        ll = dt.get_logical_lines_of_cell(raw_cell1)
        res1 = dt.insert_disp_lines(raw_cell1)
        self.assertEqual(res1, eres1)
コード例 #4
0
    def test_logical_lines1(self):
        raw_cell1 = """\
x = 0
# y = 1 ##:
ZZ = 0
"""
        ll_list = dt.get_logical_lines_of_cell(raw_cell1)
        self.assertEqual(len(ll_list), 3)

        # test whether the last logical line only consists of ENDMARKER and friends
        ignorable_tokens = [dt.tk.ENDMARKER, dt.tk.NL]
        self.assertTrue(
            all(elt.type in ignorable_tokens for elt in ll_list[-1].tokens))
コード例 #5
0
    def testLL3(self):
        raw_cell1 = """\
xx = sp.Matrix(sp.symbols('x1:11'))
yy = sp.Matrix(sp.symbols('y1:11'))

xx.shape, yy.shape ##:
"""
        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)); display({"text/plain": "---"}, raw=True)
"""

        ll = dt.get_logical_lines_of_cell(raw_cell1)
        res1 = dt.insert_disp_lines(raw_cell1)
        self.assertEqual(res1, eres1)
コード例 #6
0
    def testLL1(self):
        raw_cell1 = """\
x = 0
if 1:
    y ##:
C.xyz ##:
"""

        ll = dt.get_logical_lines_of_cell(raw_cell1)

        if 1:
            res = [dt.get_line_segments_from_logical_line(elt) for elt in ll]

            self.assertEqual(res[0], ("", "x", "0", ""))
            self.assertEqual(res[2], ("    ", None, "y", "##:"))
            self.assertEqual(res[3], ("", None, "C.xyz", "##:"))
コード例 #7
0
    def test_logical_lines2(self):

        raw_cell1 = """\
z1 = [ 1,
       2,
       3 ]



z2 = [ 1,
       2,
       3 ]  ##:

z3 = [ 1,  # some comment
       2,  ##:
       3 ] # more comments

# note the spaces in the  "empty" line below
       
z4 = [ 1,  
       2,  
       3 ] ##:
"""

        aa = dt.ast.parse(raw_cell1)
        ll = dt.get_logical_lines_of_cell(raw_cell1)

        self.assertEqual(ll[0].txt, 'z1 = [ 1,\n       2,\n       3 ]\n')
        self.assertEqual(ll[1].txt,
                         '\n\n\nz2 = [ 1,\n       2,\n       3 ]  ##:\n')
        self.assertEqual(
            ll[2].txt,
            '\nz3 = [ 1,  # some comment\n       2,  ##:\n       3 ] # more comments\n'
        )

        eres = '\n# note the spaces in the  "empty" line below\n       \nz4 = [ 1,  \n       2,  \n       3 ] ##:\n'
        self.assertEqual(ll[3].txt, eres)

        indent, lhs, rhs, comments = dt.get_line_segments_from_logical_line(
            ll[2])
        self.assertEqual(indent, "")
        self.assertEqual(lhs, "z3")
        self.assertEqual(rhs, "[ 1,\n       2,\n       3 ]")
コード例 #8
0
    def testLL2(self):

        raw_cell1 = """\
x = 0
if 1:
    # a = 0 
    # b = 1 ##:
    WW = 10
    XX = 0 # abc1
    if 1: 
        YY = 1
Z1 = 1
Z2 = 2# abc 
"""

        ll = dt.get_logical_lines_of_cell(raw_cell1)

        res = [dt.get_line_segments_from_logical_line(elt) for elt in ll]

        self.assertEqual(res[0], ("", "x", "0", ""))
        self.assertEqual(res[2], ("    ", "WW", "10", ""))
        self.assertEqual(res[3], ("    ", "XX", "0", "# abc1"))
コード例 #9
0
    def test_demo3(self):
        raw_cell1 = """\
if 1:
    # van-der-Pol-Oszillator
    f = sp.Matrix([x2, (1-x1**2)*x2 - x1])
    y = h = x1 ##:

a = 0
"""

        eres1 = """\
if 1:
    # van-der-Pol-Oszillator
    f = sp.Matrix([x2, (1-x1**2)*x2 - x1])
    y = h = x1 ##:
    custom_display("h", h); display({"text/plain": "---"}, raw=True)

a = 0
"""

        ll = dt.get_logical_lines_of_cell(raw_cell1)
        res1 = dt.insert_disp_lines(raw_cell1)
        self.assertEqual(res1, eres1)
コード例 #10
0
    def testLL4(self):

        raw_cell1 = """\
C.x = 123 ##:
x, y = 123, 789 ##:
C.x, C.y = 123, 789 ##:

C.x.y.z = 123 ##:
C.z.z.z = 123 ##:

if 1:
    C.x = 123 ##:
    C.x, ABC, C.y = 123, 789, 456 ##:
"""

        ll = dt.get_logical_lines_of_cell(raw_cell1)

        res = [dt.get_line_segments_from_logical_line(elt) for elt in ll]

        self.assertEqual(res[0], ("", "C.x", "123", "##:"))
        self.assertEqual(res[1], ("", "x, y", "123, 789", "##:"))
        self.assertEqual(res[2], ("", "C.x, C.y", "123, 789", "##:"))
        self.assertEqual(res[3], ("", "C.x.y.z", "123", "##:"))
        self.assertEqual(res[4], ("", "C.z.z.z", "123", "##:"))
コード例 #11
0
 def f(txt):
     return dt.get_logical_lines_of_cell(txt)[0]