Best Python code snippet using ATX
test_treematcher.py
Source:test_treematcher.py  
...23        true_match = [1, 2, 3, 4, 5, 6]24        matches = []25        for num, tree in enumerate(self.trees):26            one_use = deepcopy(pattern)27            result = one_use.find_match(tree)28            try:29                res = next(result)30            except:31                res = None32            if res:33                matches += [num + 1]34        self.assertTrue(matches == true_match)35    def test_two_terminal_nodes(self):36        # The presense of leaves e, f as sister nodes.37        pattern = TreePattern(" (e, f)~; ")38        true_match = [4, 5, 6, 7, 8, 9, 10, 11]39        matches = []40        for num, tree in enumerate(self.trees):41            one_use = deepcopy(pattern)42            result = one_use.find_match(tree)43            try:44                res = next(result)45            except:46                res = None47            if res:48                matches += [num + 1]49        self.assertTrue(matches == true_match)50    def test_simple_paren_two_children(self):51        tree = Tree(" (((b, c)a, (b, c)a), (e, f)d) ;", format=1)52        pattern = TreePattern("(b,c)a ;")53        result = pattern.find_match(tree)54        self.assertTrue(len(list(result)) == 2)55    def test_simple_parent_two_children_false(self):56        tree = Tree(" (((b, c)a, (b, c)a), (e, f)d) ;", format=1)57        pattern = TreePattern("(b,c)qq ;")58        result = pattern.find_match(tree)59        self.assertTrue(len(list(result)) == 0)60    def test_simple_complete_topology(self):61        pattern = TreePattern("((e, i, f)d)~ ; ")62        true_match = [8, 9]63        match = []64        for num, tree in enumerate(self.trees):65            result = pattern.find_match(tree)66            if (len(list(result)) > 0):67                match += [num+1]68        self.assertTrue(true_match == match)69    # Waits for update70    # def test_incomplete_topology(self):71    #    pattern = TreePattern(" (e, i, f)d ;")72    #73    #    for i in pattern.find_match(self.trees[8]):74    #        print i75    #    self.assertTrue(True)76class Test_metacharacters_at_terminal_nodes(unittest.TestCase):77    def test_simple_plus(self):78        tree = Tree(" (((a, a, b, qq), (a, b, c, ww)), (b, b, a, ee));", format=8)79        pattern = TreePattern(" (qq, a+)~ ;")80        result = pattern.find_match(tree)81        expected = (tree&'qq').up82        self.assertTrue(len(list(result)) > 0 )83    def test_double_match(self):84        tree = Tree(" (((a, a, b), (c, c, d) ), (e, e, f), (g, h, i)) ; ")85        pattern = TreePattern( " ((a+, b)~, (e+, f)~);")86        result = (pattern.find_match(tree))87        #self.assertTrue(len(list(result)) > 0 )88        self.assertEqual(next(result), tree)89    def test_simple_zero_or_more(self):90        tree = Tree(" ((((a, a, b)), (c, d), (e, f)), (g, h, i)) ; ")91        pattern = TreePattern(" (a, a, b, d*) ;")92        result = (pattern.find_match(tree))93        self.assertTrue(len(list(result)) > 0)94    def test_skipped_zero_or_more(self):95        tree = Tree(" ((((a, a, b)), (c, d), (e, f)), (g, h, i)) ; ")96        pattern = TreePattern(" ( a, b, d*) ;")97        result = (pattern.find_match(tree))98        # false test99        self.assertTrue(len(list(result)) == 0)100    def test_constraintes_pattern(self):101        tree = Tree(" ((((a, b)), (c, d), (e, f)), (g, h, i)) ; ")102        (tree&'a').dist = 0.2103        (tree&'b').dist = 0.4104        (tree&'c').dist = 0.5105        (tree&'d').dist = 0.6106        (tree&'e').dist = 0.7107        (tree&'f').dist = 0.8108        (tree&'g').dist = 0.9109        pattern = TreePattern(" ('@.dist > 0.5+'); ", quoted_node_names=True)110        result = pattern.find_match(tree)111        expected = [(tree&'e').up, (tree&'g').up]112        found = True113        count = 0114        for node in result:115            count += 1116            found &= node in expected117        found &= count == 2118        self.assertTrue(found)119    def test_constraints_and_loose(self):120        tree = Tree(" (((((a, b), (c, d), (e, f)), (g, h, i)))) ; ")121        (tree&'a').dist = 0.2122        (tree&'b').dist = 0.4123        (tree&'c').dist = 0.5124        (tree&'d').dist = 0.6125        (tree&'e').dist = 0.7126        (tree&'f').dist = 0.7127        (tree&'g').dist = 0.9128        pattern = TreePattern(""" ('@.dist == 0.2', 'b')'~', ('@.dist > 0.5', '@.dist == 0.7+')'~' ; """, quoted_node_names=True)129        result = pattern.find_match(tree)130        res = next(result)131        #self.assertEqual(res, ((tree&'f').up).up)132        self.assertTrue( len(list(result)) > 0 )133    def test_star_and_logical_constraints(self):134        tree = Tree(" (((a, b), (c, d), (e, f)), (g, h, i)) ; ")135        (tree&'a').dist = 0.2136        (tree&'b').dist = 0.4137        (tree&'c').dist = 0.5138        (tree&'d').dist = 0.6139        (tree&'e').dist = 0.7140        (tree&'f').dist = 0.8141        (tree&'g').dist = 0.9142        pattern = TreePattern(""" ('g', '@.dist == 1+', 'fls_node*'); """, quoted_node_names=True)143        result = pattern.find_match(tree)144        self.assertEqual(next(result), (tree&'g').up)145    def test_exact_number_of_repeat(self):146        tree = Tree("((a, a, a, b, c), (d, d, qq), (e, e, e, ww, e, e, e, e, e)); ")147        p1 = TreePattern(" (b, c, 'a{1,3}') ;")148        p2 = TreePattern(" (b, c, 'a{2,3}') ;")149        p3 = TreePattern(" (b, c, 'a{3,3}') ;")150        p4 = TreePattern(" (b, c, 'a{4,5}') ;")151        p5 = TreePattern(" (ww, 'e{1,8}') ;")152        p6 = TreePattern(" (ww, 'e{7,9}') ;")153        p7 = TreePattern(" (ww, 'e{1,3}') ;")154        patterns = [p1, p2, p3, p4, p5, p6, p7]155        true_match = [True, True, True, False, True, True, False]156        match = True157        for num, pattern in enumerate(patterns):158            result = pattern.find_match(tree)159            found = len(list(result)) > 0160            match &= found == true_match[num]161        self.assertTrue(match)162    def test_exact_number_and_topology(self):163        tree = Tree(" ((a, a, b)p1, ((c, c, c, d)p2, (e, f, g)p3)p4)p5 ;", format=1)164        p1 = TreePattern(" ('a{2,2}', 'b')'p1' ;", quoted_node_names=True)165        p2 = TreePattern(" ('c{1,5}', 'd')'p2' ;",quoted_node_names=True)166        p3 = TreePattern(" ('c{2,3}', d, 'ww{0,3}')p2 ;")167        p4 = TreePattern(" ('c{3,3}', 'd{0,5}', 'ww{0,3}')p2;")168        p5 = TreePattern(" ('c{1,2}', 'd{0,1}', 'ww*')p2;")169        patterns = [p1, p2, p3, p4, p5]170        true_match = [True, True, True, True, False]171        match = True172        for num, pattern in enumerate(patterns):173            result = pattern.find_match(tree)174            found = len(list(result)) > 0175            match &= found == true_match[num]176        self.assertTrue(match)177class Test_basic_tests(unittest.TestCase):178    def test_all(self):179        test = True180        t1 = Tree(" ((a, a, b)p1, ((c, c, c, d)p2, (e, f, g)p3)p4)p5 ;", format=1)181        p1 = TreePattern(" ('c+', 'd')'p2' ;",quoted_node_names=True)182        test &= len(list(p1.find_match(t1))) > 0183        # Should  match184        t1 = Tree(" (((F, G)E, (C, D)B), A);", format=8)185        p1  = TreePattern("('@.support > 0', '@.support > 0')'B' ;")186        test &= len(list(p1.find_match(t1))) > 0187        # Should NOT match188        t1 = Tree(" (((F, G)E, (C, D)B), A);", format=8)189        p1  = TreePattern("('@.support > 0', '@.support > 0{2,3}')'B' ;")190        test &= len(list(p1.find_match(t1))) == 0191        # Should  match192        t1 = Tree(" (((F, G)E, (C, D)B), A);", format=8)193        p1  = TreePattern("('C', '@.support > 0')'B' ;")194        test &= len(list(p1.find_match(t1))) > 0195        # Should not match196        t1 = Tree("(((A, A, A), (B,C)), K);")197        p1 = TreePattern("(((A, A+, A, A), (B,C)), K);")198        test &= len(list(p1.find_match(t1))) == 0199        # Should match200        t1 = Tree("(((A, A, A), (B,C)), K);")201        p1 = TreePattern("(((A, A+, A), (B,C)), K);")202        test &= len(list(p1.find_match(t1))) > 0203        # Should match204        t1 = Tree("(((A, A, A), (B,C)), K);")205        p1 = TreePattern("(((A, A+), (B,C)), K);")206        test &= len(list(p1.find_match(t1))) > 0207        # ~ after a ) means that the two children of that node can be connected by208        # any number of internal up/down nodes209        t1 = Tree("(  ((B,Z), (D,F)), G);")210        p1 = TreePattern("( (B,Z), G)~;")211        test &= len(list(p1.find_match(t1))) > 0212        t1 = Tree("(  ((G, ((B,Z),A)), (D,G)), C);")213        p1 = TreePattern("(((B,Z)~,C), G)~;")214        test &= len(list(p1.find_match(t1))) == 0215        t1 = Tree("(  ((G, ((B,Z),A)), (D,G)), C);")216        p1 = TreePattern("(((B,Z)~,G), C)~;")217        test &= len(list(p1.find_match(t1))) > 0218        t1 = Tree("(((A, (B,C,D)), ((B,C), A)), F);")219        p1 = TreePattern("((C,B,D*), A);")220        test &= len(list(p1.find_match(t1))) > 0221        t1 = Tree("(((A, (B,C,D, D, D)), ((B,C), A)), F);")222        p1 = TreePattern("((C,B,'D{2,3}'), A);")223        test &= len(list(p1.find_match(t1))) > 0224        t1 = Tree("(a, b, b, a);")225        p1 = TreePattern("(a+, b+);")226        test &= len(list(p1.find_match(t1))) > 0227        t1 = Tree("((a, b), c);")228        p1 = TreePattern("((a, b, d*), c);")229        test &= len(list(p1.find_match(t1))) > 0230        t1 = Tree("(  (((B,H), (B,B,H), C), A), (K, J));")231        p1 = TreePattern("((C, (B+,H)+), A);")232        test &= len(list(p1.find_match(t1))) > 0233        self.assertTrue(test)234if __name__ == '__main__':...02_characterClass.py
Source:02_characterClass.py  
1import re2def find_match(pattern, string, index):3    x = re.search(pattern, string)4    if x:5        print(f"{index} - Match")6    else:7        print(f"{index} - No Match")8find_match('[aeiouAEIOU]', "a", 1) # Y9find_match('[aeiouAEIOU]', "ab", 2) # Y10find_match('[aeiouAEIOU]', "b", 3) # N11find_match('[a-zA-Z0-9_]', "b", 4) # Y12find_match('[a-zA-Z0-9_]', ",", 5) # N13find_match('[a-zA-Z0-9_]', "-", 6) # N14find_match('[a-zA-Z0-9_-]', "-", 7) # Y (search '-')15#NOT16find_match('[^0-9]', "0", 8) # N17find_match('[^0-9]', "0a", 9) # Y18find_match('[aeiouAEIOU][a-zA-Z0-9_][^0-9]', "a", 10) # N19find_match('[aeiouAEIOU][a-zA-Z0-9_][^0-9]', "am?", 11) # Y20find_match('[aeiouAEIOU][a-zA-Z0-9_][^0-9]', "-aaa??", 12) # Y (because of the aaa)...03_beginAndEnd.py
Source:03_beginAndEnd.py  
1import re2def find_match(pattern, string, index):3    x = re.search(pattern, string)4    if x:5        print(f"{index} - Match")6    else:7        print(f"{index} - No Match")8find_match('a', '0a', 1) # Y9find_match('^a', '0a', 2) # N10find_match('a', 'a0', 3) # Y11find_match('^a', 'a0', 4) # Y12find_match('a$', '0a', 5) # Y13find_match('a$', 'a0', 6) # N14find_match('^a$', 'a', 7) # Y15find_match('^a$', '0a', 8) # N16find_match('^a$', 'a0', 9) # N17#insdie the brackets the ^ is NOT18find_match('[^a]', 'a', 10) # N19find_match('[^a]', 'a0', 11) # Y20#starts at a21find_match('^[a]', 'a', 12) # Y...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
