Best Python code snippet using slash
load_grammar.py
Source:load_grammar.py  
...258            ] + [259                ST('expansion', [target]*a + [atom]*i) for i in range(b)260            ])261            return self._add_rule(key, new_name, tree)262    def _generate_repeats(self, rule, mn, mx):263        """Generates a rule tree that repeats ``rule`` exactly between ``mn`` to ``mx`` times.264        """265        # For a small number of repeats, we can take the naive approach266        if mx < REPEAT_BREAK_THRESHOLD:267            return ST('expansions', [ST('expansion', [rule] * n) for n in range(mn, mx + 1)])268        # For large repeat values, we break the repetition into sub-rules. 269        # We treat ``rule~mn..mx`` as ``rule~mn rule~0..(diff=mx-mn)``.270        # We then use small_factors to split up mn and diff up into values [(a, b), ...]271        # This values are used with the help of _add_repeat_rule and _add_repeat_rule_opt272        # to generate a complete rule/expression that matches the corresponding number of repeats273        mn_target = rule274        for a, b in small_factors(mn, SMALL_FACTOR_THRESHOLD):275            mn_target = self._add_repeat_rule(a, b, mn_target, rule)276        if mx == mn:277            return mn_target278        diff = mx - mn + 1  # We add one because _add_repeat_opt_rule generates rules that match one less279        diff_factors = small_factors(diff, SMALL_FACTOR_THRESHOLD)280        diff_target = rule  # Match rule 1 times281        diff_opt_target = ST('expansion', [])  # match rule 0 times (e.g. up to 1 -1 times)282        for a, b in diff_factors[:-1]:283            diff_opt_target = self._add_repeat_opt_rule(a, b, diff_target, diff_opt_target, rule)284            diff_target = self._add_repeat_rule(a, b, diff_target, rule)285        a, b = diff_factors[-1]286        diff_opt_target = self._add_repeat_opt_rule(a, b, diff_target, diff_opt_target, rule)287        return ST('expansions', [ST('expansion', [mn_target] + [diff_opt_target])])288    def expr(self, rule, op, *args):289        if op.value == '?':290            empty = ST('expansion', [])291            return ST('expansions', [rule, empty])292        elif op.value == '+':293            # a : b c+ d294            #   -->295            # a : b _c d296            # _c : _c c | c;297            return self._add_recurse_rule('plus', rule)298        elif op.value == '*':299            # a : b c* d300            #   -->301            # a : b _c? d302            # _c : _c c | c;303            new_name = self._add_recurse_rule('star', rule)304            return ST('expansions', [new_name, ST('expansion', [])])305        elif op.value == '~':306            if len(args) == 1:307                mn = mx = int(args[0])308            else:309                mn, mx = map(int, args)310                if mx < mn or mn < 0:311                    raise GrammarError("Bad Range for %s (%d..%d isn't allowed)" % (rule, mn, mx))312            return self._generate_repeats(rule, mn, mx)313        assert False, op314    def maybe(self, rule):315        keep_all_tokens = self.rule_options and self.rule_options.keep_all_tokens316        rule_size = FindRuleSize(keep_all_tokens).transform(rule)317        empty = ST('expansion', [_EMPTY] * rule_size)318        return ST('expansions', [rule, empty])319class SimplifyRule_Visitor(Visitor):320    @staticmethod321    def _flatten(tree):322        while tree.expand_kids_by_data(tree.data):323            pass324    def expansion(self, tree):325        # rules_list unpacking326        # a : b (c|d) e...loader.py
Source:loader.py  
...41                self._cached_matchers = None42        return self._cached_matchers43    def get_runnables(self, paths, prepend_interactive=False):44        assert context.session is not None45        sources = self._generate_repeats(self._generate_test_sources(paths))46        returned = self._collect(sources)47        self._duplicate_funcs |= self._local_config.duplicate_funcs48        for (path, name, line) in sorted(self._duplicate_funcs):49            _logger.warning('Duplicate function definition, File: {}, Name: {}, Line: {}', path, name, line)50        if prepend_interactive:51            returned.insert(0, generate_interactive_test())52        for index, test in enumerate(returned):53            test.__slash__.parallel_index = index54        hooks.tests_loaded(tests=returned) # pylint: disable=no-member55        returned.sort(key=lambda test: (56            test.__slash__.repeat_all_index, test.__slash__.get_sort_key()57        ))58        for test in returned:59            if test.__slash__.id is not None:60                raise SlashInternalError('Slash ID of {!r} should be None, but is {}'.format(test, test.__slash__.id))61            test.__slash__.allocate_id()62        return returned63    def _generate_repeats(self, tests):64        returned = []65        repeat_each = config.root.run.repeat_each66        for test in tests:67            for i in range(repeat_each * repeat_marker.get_value(test.get_test_function(), 1)):68                returned.append(test.clone() if i else test)69        num_tests = len(returned)70        for i in range(config.root.run.repeat_all - 1):71            for test in itertools.islice(returned, 0, num_tests):72                clone = test.clone()73                clone.__slash__.repeat_all_index = i + 174                returned.append(clone)75        return returned76    def _collect(self, iterator):77        returned = []...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!!
