Best Python code snippet using tempest_python
test_get_pages.py
Source:test_get_pages.py
...15from MooseDocs.tree import pages16class TestBuildRegex(unittest.TestCase):17 def testBasic(self):18 path = '/one/**/four/five'19 self.assertEqual(_build_regex(path), r'^/one/(?:.*?)/four/five$')20 path = '/one/two/three/four/**'21 self.assertEqual(_build_regex(path), r'^/one/two/three/four/(?:.*?)$')22 path = '**/three/four/five'23 self.assertEqual(_build_regex(path), r'^(?:.*?)/three/four/five$')24 path = '**/three/**'25 self.assertEqual(_build_regex(path), r'^(?:.*?)/three/(?:.*?)$')26 path = '**/three/**/nine/**'27 self.assertEqual(_build_regex(path), r'^(?:.*?)/three/(?:.*?)/nine/(?:.*?)$')28 path = '/one/*/four/five'29 self.assertEqual(_build_regex(path), r'^/one/(?:[^/]*?)/four/five$')30 path = '/one/two/three/four/*'31 self.assertEqual(_build_regex(path), r'^/one/two/three/four/(?:[^/]*?)$')32 path = '*/three/four/five'33 self.assertEqual(_build_regex(path), r'^(?:[^/]*?)/three/four/five$')34 path = '*/three/*'35 self.assertEqual(_build_regex(path), r'^(?:[^/]*?)/three/(?:[^/]*?)$')36 path = '*/three/*/nine/*'37 self.assertEqual(_build_regex(path), r'^(?:[^/]*?)/three/(?:[^/]*?)/nine/(?:[^/]*?)$')38 path = '*/three/**/nine/*'39 self.assertEqual(_build_regex(path), r'^(?:[^/]*?)/three/(?:.*?)/nine/(?:[^/]*?)$')40 path = '**/three/*/nine/**'41 self.assertEqual(_build_regex(path), r'^(?:.*?)/three/(?:[^/]*?)/nine/(?:.*?)$')42 path = '**/three/*/nine/**/foo.md'43 self.assertEqual(_build_regex(path), r'^(?:.*?)/three/(?:[^/]*?)/nine/(?:.*?)/foo\.md$')44class TestFindFiles(unittest.TestCase):45 def testBasic(self):46 filenames = ['/one/two/three/four/a.md',47 '/one/two/three/four/b.md',48 '/one/two/three/four/c.md',49 '/one/two/three/four/d.md',50 '/one/two/not-three/four/a.md',51 '/one/two/not-three/four/b.md',52 '/one/two/three/four/five/a.md',53 '/one/two/three/four/five/b.md',54 '/one/two/three/four/five/c.md',55 '/one/two/three/four/five/d.md']56 pattern = '/one/two/three/four/*'57 files = _find_files(filenames, pattern)...
solution.py
Source:solution.py
1#%% Part 12from data import rules, messages3from data import test_rules, test_messages4def build_regex(rules: dict, rule_id: str):5 def _build_regex(rule_id: str):6 rule = rules[rule_id]7 if isinstance(rule, str):8 return rule9 else:10 regexes = [11 "".join(12 _build_regex(term)13 for term in subrule14 )15 for subrule in rule16 ]17 return "(" + "|".join(18 f"({regex})"19 for regex in regexes20 ) + ")"21 return r"^(" + _build_regex(rule_id) + r")$"22import re23RULE_ZERO_REGEX = re.compile(build_regex(rules, '0'))24TEST_RULE_ZERO_REGEX = re.compile(build_regex(test_rules, '0'))25print("Part 1 Test:", len(list(filter(TEST_RULE_ZERO_REGEX.match, test_messages))))26print("Part 1:", len(list(filter(RULE_ZERO_REGEX.match, messages))))27#%% Part 228from data import parse_rule29part_2_rules = rules.copy()30parse_rule(part_2_rules, "8: 42 | 42 8")31parse_rule(part_2_rules, "11: 42 31 | 42 11 31")32def build_regex_part_2(rules: dict, rule_id: str, max_rule_11_depth: int=12):33 def _build_regex(rule_id: str, rule_11_depth: int):34 if rule_id == "8":35 return "{}+".format(_build_regex("42", rule_11_depth))36 if rule_id == "11":37 return f"{_build_regex('42', rule_11_depth)}{{{rule_11_depth}}}{_build_regex('31', rule_11_depth)}{{{rule_11_depth}}}"38 rule = rules[rule_id]39 if isinstance(rule, str):40 return rule41 else:42 regexes = [43 "".join(44 _build_regex(term, rule_11_depth)45 for term in subrule46 )47 for subrule in rule48 ]49 return "(" + "|".join(50 f"({regex})"51 for regex in regexes52 ) + ")"53 return [54 r"^(" + _build_regex(rule_id, rule_11_depth) + r")$"55 for rule_11_depth in range(1, max_rule_11_depth + 2)56 ]57RULE_ZERO_REGEXES = [58 re.compile(regex)59 for regex in60 build_regex_part_2(part_2_rules, '0')61]62part_2_messages = [63 message64 for message in messages65 if any(66 regex.match(message)67 for regex in RULE_ZERO_REGEXES68 )...
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!!