Best Python code snippet using fMBT_python
recb.py
Source:recb.py  
...121    return P_name(name, **kwargs)122def fast_forward(text):123    """returns a pattern that matches everything until the next instance of text"""124    return P_ff(text)125def parser_debugger(match=True, nomatch=True, interactive=False, print_func=sys.stdout.write, print_maxlen=80):126    """returns configured parser debugger. Add returned function to any pattern with pattern.set_pa(f).127    Parameters:128      match (bool):129              if True, print/debug if a pattern matches. The default is True.130      nomatch (bool):131              if True, print/debug if a pattern does not match. The132              default is True.133      interactive (bool or function(pattern, string, match) -> bool):134              if True, stop in interactive debugger (pdb prompt) on135              match. The default is False. If function, go interactive136              if the function returns True.137      print_func (function(str)):138             function to handle debug print messages.139             The default is sys.stdout.write.140      print_maxlen (int):141             maximum string length for printing.142    """143    def _parser_debugger(p, s, m):144        if callable(interactive):145            go_interacive = interactive(p, s, m)146        else:147            go_interacive = interactive148        if not match and m:149            # there is a match, but match debugging is set off150            return151        if not nomatch and m is None:152            # there is no match, but nomatch debugging is set off153            return154        print_msg = []155        print_msg.append("\npattern: %s" % (p,))156        if len(s) > print_maxlen:157            show_s = s[:print_maxlen] + "...[%s B]" % (len(s),)158        else:159            show_s = s160        print_msg.append("parsing: %r" % (show_s,))161        if type(m).__name__ == "SRE_Match":162            print_msg.append("          " + ("^" * (len(repr(show_s[:m.end()]))-2)))163            d = m.groupdict()164            for k in sorted(m.groupdict().keys()):165                print_msg.append("         %s = %r" % (k, d[k][:print_maxlen] if d.get(k, None) != None else None))166        print_msg.append("match:   %s" % (m,))167        if print_func:168            print_func("\n".join(print_msg) + "\n")169        if go_interacive:170            import pdb171            pdb.set_trace()172    return _parser_debugger173class P_Base(object):174    def __init__(self, parent=None, ca=None, cb=None, children=None,175                 pb=None, pa=None):176        self._parent = parent177        self._cb = cb # callback for "all parsed", called before children178        self._ca = ca # callback for "all parsed", called after children179        self._pb = pb # (debug) callback during parsing, called before parse180        self._pa = pa # (debug) callback during parsing, called after parse181        self._children = children182        self._parse_stack = []183        if self._children:184            for child in self._children:185                child.set_parent(self)186    def __add__(self, other_p):187        return P_then_P(children=[self, other_p])188    def __or__(self, other_p):189        return P_or_P(children=[self, other_p])190    def __sub__(self, other_p):191        return P_but_not_P(children=[self, other_p])192    def __repr__(self):193        return "%s(%r)" % (194            self.__class__.__name__,195            self._children)196    def many(self, **kwargs):197        return P_many(self, **kwargs)198    def parse(self, s, lineno=1):199        if self._parse_stack and self._parse_stack[-1] == s:200            # never ending recursion, refuse to parse201            return (None, s)202        self._parse_stack.append(s)203        results, unparsed = self._parse(s)204        self._parse_stack.pop()205        if not (results is None) and not self._parent and len(self._parse_stack) == 0:206            # This is the root node, no recursion depth left:207            # s was parsed successfully208            self.parsed(results, lineno)209        return results, unparsed210    def parsed(self, results, lineno):211        if self._cb:212            self._cb(self, results, lineno)213        self._parsed(results, lineno)214        if self._ca:215            self._ca(self, results, lineno)216    def _parsed(self, results, lineno):217        """override on derived classes if they have children. They need to218        call children's parsed() (not _parsed()) for correct children."""219        pass220    def set_ca(self, ca):221        """set callback function f(pattern, result, lineno) to be called222        on full match (everything parsed successfully).223        This is called after callbacks of child nodes in syntax tree."""224        self._ca = ca225        return self226    def set_cb(self, cb):227        """set callback function f(pattern, result, lineno) to be called228        on full match (everything parsed successfully).229        This is called before callbacks of child nodes in syntax tree."""230        self._cb = cb231        return self232    def set_parent(self, parent):233        self._parent = parent234        return self235    def set_patterns(self, name_to_pattern):236        if self._children:237            for child in self._children:238                child.set_patterns(name_to_pattern)239        return self240    def set_pa(self, pa):241        """set callback function f(pattern, s) to be called242        before parsing s"""243        if self._parse_stack:244            # recursive call detected, node already handled245            return self246        if isinstance(self, P_re) or isinstance(self, P_ff):247            self._pa = pa248        self._parse_stack.append(None)249        if self._children:250            for child in self._children:251                child.set_pa(pa)252        self._parse_stack.pop()253        return self254    def set_pb(self, pb):255        """set callback function f(pattern, s, result) to be called256        after parsing s, with parsing result"""257        if isinstance(self, P_re) or isinstance(self, P_ff):258            self._pb = pb259        if self._children:260            for child in self._children:261                child.set_pb(pb)262        return self263    def debug(self, **kwargs):264        """run parser in debug mode. overwrites pa callback.265        see parser_debugger() for parameters."""266        self.set_pa(parser_debugger(**kwargs))267        return self268class P_re(P_Base):269    def __init__(self, regexp, **kwargs):270        P_Base.__init__(self, **kwargs)271        self._r = re.compile(regexp)272    def regexp(self):273        return self._r274    def _parse(self, s):275        if self._pb:276            self._pb(self, s)277        m = self._r.match(s)278        if self._pa:279            self._pa(self, s, m)280        if m:...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!!
