當前位置: 首頁>>代碼示例>>Python>>正文


Python pytree.Leaf方法代碼示例

本文整理匯總了Python中lib2to3.pytree.Leaf方法的典型用法代碼示例。如果您正苦於以下問題:Python pytree.Leaf方法的具體用法?Python pytree.Leaf怎麽用?Python pytree.Leaf使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在lib2to3.pytree的用法示例。


在下文中一共展示了pytree.Leaf方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_changed

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_changed(self):
        l1 = pytree.Leaf(100, "f")
        self.assertFalse(l1.was_changed)
        l1.changed()
        self.assertTrue(l1.was_changed)

        l1 = pytree.Leaf(100, "f")
        n1 = pytree.Node(1000, [l1])
        self.assertFalse(n1.was_changed)
        n1.changed()
        self.assertTrue(n1.was_changed)

        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "+")
        l3 = pytree.Leaf(100, "bar")
        n1 = pytree.Node(1000, [l1, l2, l3])
        n2 = pytree.Node(1000, [n1])
        self.assertFalse(l1.was_changed)
        self.assertFalse(n1.was_changed)
        self.assertFalse(n2.was_changed)

        n1.changed()
        self.assertTrue(n1.was_changed)
        self.assertTrue(n2.was_changed)
        self.assertFalse(l1.was_changed) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:27,代碼來源:test_pytree.py

示例2: test_node_set_child

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_node_set_child(self):
        l1 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1])

        l2 = pytree.Leaf(100, "bar")
        n1.set_child(0, l2)
        self.assertEqual(l1.parent, None)
        self.assertEqual(l2.parent, n1)
        self.assertEqual(n1.children, [l2])

        n2 = pytree.Node(1000, [l1])
        n2.set_child(0, n1)
        self.assertEqual(l1.parent, None)
        self.assertEqual(n1.parent, n2)
        self.assertEqual(n2.parent, None)
        self.assertEqual(n2.children, [n1])

        self.assertRaises(IndexError, n1.set_child, 4, l2)
        # I don't care what it raises, so long as it's an exception
        self.assertRaises(Exception, n1.set_child, 0, list) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:22,代碼來源:test_pytree.py

示例3: suitify

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def suitify(parent):
    """
    Turn the stuff after the first colon in parent's children
    into a suite, if it wasn't already
    """
    for node in parent.children:
        if node.type == syms.suite:
            # already in the prefered format, do nothing
            return

    # One-liners have no suite node, we have to fake one up
    for i, node in enumerate(parent.children):
        if node.type == token.COLON:
            break
    else:
        raise ValueError(u"No class suite and no ':'!")
    # Move everything into a suite node
    suite = Node(syms.suite, [Newline(), Leaf(token.INDENT, indentation(node) + indentation_step(node))])
    one_node = parent.children[i+1]
    one_node.remove()
    one_node.prefix = u''
    suite.append_child(one_node)
    parent.append_child(suite) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:25,代碼來源:fixer_util.py

示例4: test_remove

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_remove(self):
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [n1])

        self.assertEqual(n1.remove(), 0)
        self.assertEqual(n2.children, [])
        self.assertEqual(l1.parent, n1)
        self.assertEqual(n1.parent, None)
        self.assertEqual(n2.parent, None)
        self.assertFalse(n1.was_changed)
        self.assertTrue(n2.was_changed)

        self.assertEqual(l2.remove(), 1)
        self.assertEqual(l1.remove(), 0)
        self.assertEqual(n1.children, [])
        self.assertEqual(l1.parent, None)
        self.assertEqual(n1.parent, None)
        self.assertEqual(n2.parent, None)
        self.assertTrue(n1.was_changed)
        self.assertTrue(n2.was_changed) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:24,代碼來源:test_pytree.py

示例5: transform

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def transform(self, node, results):
        single = results.get("single")
        if single:
            # Make a fake listmaker
            fake = pytree.Node(syms.listmaker, [single.clone()])
            single.replace(fake)
            items = fake
        else:
            items = results["items"]

        # Build the contents of the literal
        literal = [pytree.Leaf(token.LBRACE, "{")]
        literal.extend(n.clone() for n in items.children)
        literal.append(pytree.Leaf(token.RBRACE, "}"))
        # Set the prefix of the right brace to that of the ')' or ']'
        literal[-1].prefix = items.next_sibling.prefix
        maker = pytree.Node(syms.dictsetmaker, literal)
        maker.prefix = node.prefix

        # If the original was a one tuple, we need to remove the extra comma.
        if len(maker.children) == 4:
            n = maker.children[2]
            n.remove()
            maker.children[-1].prefix = n.prefix

        # Finally, replace the set call with our shiny new literal.
        return maker 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:29,代碼來源:fix_set_literal.py

示例6: test

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test(self):
        kids = [None,
                [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 2),
                 Leaf(token.NUMBER, 3)],
                [Leaf(token.NUMBER, 1), Leaf(token.NUMBER, 3),
                 Leaf(token.NUMBER, 2), Leaf(token.NUMBER, 4)],
                [Leaf(token.STRING, "b"), Leaf(token.STRING, "j", prefix=" ")]
                ]
        self.assertStr(self._Call("A"), "A()")
        self.assertStr(self._Call("b", kids[1]), "b(1,2,3)")
        self.assertStr(self._Call("a.b().c", kids[2]), "a.b().c(1,3,2,4)")
        self.assertStr(self._Call("d", kids[3], prefix=" "), " d(b, j)") 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:14,代碼來源:test_util.py

示例7: test_leaf

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_leaf(self):
        l1 = pytree.Leaf(100, "foo")
        self.assertEqual(l1.type, 100)
        self.assertEqual(l1.value, "foo") 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:6,代碼來源:test_pytree.py

示例8: test_leaf_repr

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_leaf_repr(self):
        l1 = pytree.Leaf(100, "foo")
        self.assertEqual(repr(l1), "Leaf(100, 'foo')") 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:5,代碼來源:test_pytree.py

示例9: test_leaf_str

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_leaf_str(self):
        l1 = pytree.Leaf(100, "foo")
        self.assertEqual(str(l1), "foo")
        l2 = pytree.Leaf(100, "foo", context=(" ", (10, 1)))
        self.assertEqual(str(l2), " foo") 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:7,代碼來源:test_pytree.py

示例10: test_leaf_equality

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_leaf_equality(self):
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "foo", context=(" ", (1, 0)))
        self.assertEqual(l1, l2)
        l3 = pytree.Leaf(101, "foo")
        l4 = pytree.Leaf(100, "bar")
        self.assertNotEqual(l1, l3)
        self.assertNotEqual(l1, l4) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:10,代碼來源:test_pytree.py

示例11: test_leaf_prefix

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_leaf_prefix(self):
        l1 = pytree.Leaf(100, "foo")
        self.assertEqual(l1.prefix, "")
        self.assertFalse(l1.was_changed)
        l1.prefix = "  ##\n\n"
        self.assertEqual(l1.prefix, "  ##\n\n")
        self.assertTrue(l1.was_changed) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:9,代碼來源:test_pytree.py

示例12: test_node

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_node(self):
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(200, "bar")
        n1 = pytree.Node(1000, [l1, l2])
        self.assertEqual(n1.type, 1000)
        self.assertEqual(n1.children, [l1, l2]) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:8,代碼來源:test_pytree.py

示例13: test_node_repr

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_node_repr(self):
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
        n1 = pytree.Node(1000, [l1, l2])
        self.assertEqual(repr(n1),
                         "Node(1000, [%s, %s])" % (repr(l1), repr(l2))) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:8,代碼來源:test_pytree.py

示例14: test_node_str

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_node_str(self):
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar", context=(" ", (1, 0)))
        n1 = pytree.Node(1000, [l1, l2])
        self.assertEqual(str(n1), "foo bar") 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:7,代碼來源:test_pytree.py

示例15: test_get_suffix

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import Leaf [as 別名]
def test_get_suffix(self):
        l1 = pytree.Leaf(100, "foo", prefix="a")
        l2 = pytree.Leaf(100, "bar", prefix="b")
        n1 = pytree.Node(1000, [l1, l2])

        self.assertEqual(l1.get_suffix(), l2.prefix)
        self.assertEqual(l2.get_suffix(), "")
        self.assertEqual(n1.get_suffix(), "")

        l3 = pytree.Leaf(100, "bar", prefix="c")
        n2 = pytree.Node(1000, [n1, l3])

        self.assertEqual(n1.get_suffix(), l3.prefix)
        self.assertEqual(l3.get_suffix(), "")
        self.assertEqual(n2.get_suffix(), "") 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:17,代碼來源:test_pytree.py


注:本文中的lib2to3.pytree.Leaf方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。