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


Python pytree.WildcardPattern方法代碼示例

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


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

示例1: test_wildcard

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import WildcardPattern [as 別名]
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertIs(r["pl"], l3)
        r = {} 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:32,代碼來源:test_pytree.py

示例2: test_generate_matches

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import WildcardPattern [as 別名]
def test_generate_matches(self):
        la = pytree.Leaf(1, "a")
        lb = pytree.Leaf(1, "b")
        lc = pytree.Leaf(1, "c")
        ld = pytree.Leaf(1, "d")
        le = pytree.Leaf(1, "e")
        lf = pytree.Leaf(1, "f")
        leaves = [la, lb, lc, ld, le, lf]
        root = pytree.Node(1000, leaves)
        pa = pytree.LeafPattern(1, "a", "pa")
        pb = pytree.LeafPattern(1, "b", "pb")
        pc = pytree.LeafPattern(1, "c", "pc")
        pd = pytree.LeafPattern(1, "d", "pd")
        pe = pytree.LeafPattern(1, "e", "pe")
        pf = pytree.LeafPattern(1, "f", "pf")
        pw = pytree.WildcardPattern([[pa, pb, pc], [pd, pe],
                                     [pa, pb], [pc, pd], [pe, pf]],
                                    min=1, max=4, name="pw")
        self.assertEqual([x[0] for x in pw.generate_matches(leaves)],
                         [3, 5, 2, 4, 6])
        pr = pytree.NodePattern(type=1000, content=[pw], name="pr")
        matches = list(pytree.generate_matches([pr], [root]))
        self.assertEqual(len(matches), 1)
        c, r = matches[0]
        self.assertEqual(c, 1)
        self.assertEqual(str(r["pr"]), "abcdef")
        self.assertEqual(r["pw"], [la, lb, lc, ld, le, lf])
        for c in "abcdef":
            self.assertEqual(r["p" + c], pytree.Leaf(1, c)) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:31,代碼來源:test_pytree.py

示例3: test_has_key_example

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import WildcardPattern [as 別名]
def test_has_key_example(self):
        pattern = pytree.NodePattern(331,
                                     (pytree.LeafPattern(7),
                                      pytree.WildcardPattern(name="args"),
                                      pytree.LeafPattern(8)))
        l1 = pytree.Leaf(7, "(")
        l2 = pytree.Leaf(3, "x")
        l3 = pytree.Leaf(8, ")")
        node = pytree.Node(331, [l1, l2, l3])
        r = {}
        self.assertTrue(pattern.match(node, r))
        self.assertEqual(r["args"], [l2]) 
開發者ID:remg427,項目名稱:misp42splunk,代碼行數:14,代碼來源:test_pytree.py

示例4: test_wildcard

# 需要導入模塊: from lib2to3 import pytree [as 別名]
# 或者: from lib2to3.pytree import WildcardPattern [as 別名]
def test_wildcard(self):
        # Build a tree for testing
        l1 = pytree.Leaf(100, "foo")
        l2 = pytree.Leaf(100, "bar")
        l3 = pytree.Leaf(100, "foo")
        n1 = pytree.Node(1000, [l1, l2])
        n2 = pytree.Node(1000, [l3])
        root = pytree.Node(1000, [n1, n2])
        # Build a pattern
        pl = pytree.LeafPattern(100, "foo", name="pl")
        pn = pytree.NodePattern(1000, [pl], name="pn")
        pw = pytree.WildcardPattern([[pn], [pl, pl]], name="pw")
        r = {}
        self.assertFalse(pw.match_seq([root], r))
        self.assertEqual(r, {})
        self.assertFalse(pw.match_seq([n1], r))
        self.assertEqual(r, {})
        self.assertTrue(pw.match_seq([n2], r))
        # These are easier to debug
        self.assertEqual(sorted(r.keys()), ["pl", "pn", "pw"])
        self.assertEqual(r["pl"], l1)
        self.assertEqual(r["pn"], n2)
        self.assertEqual(r["pw"], [n2])
        # But this is equivalent
        self.assertEqual(r, {"pl": l1, "pn": n2, "pw": [n2]})
        r = {}
        self.assertTrue(pw.match_seq([l1, l3], r))
        self.assertEqual(r, {"pl": l3, "pw": [l1, l3]})
        self.assertTrue(r["pl"] is l3)
        r = {} 
開發者ID:ktraunmueller,項目名稱:Computable,代碼行數:32,代碼來源:test_pytree.py


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