Best Python code snippet using avocado_python
python3s_backend.py
Source:python3s_backend.py  
...78					self       .clear_break(f,l)79				elif mr:80					self.parent.clear_break(f,l)81					self       .clear_break(f,l)82					self.parent.set_break(f,l,{"range": mr, "hits" : 0})83					self       .set_break(f,l,{"range": mr, "hits" : 0})84				else :85					self.parent.clear_break(f,l)86					self       .clear_break(f,l)87					self.parent.set_break(f,l,{"cond":args[1]})88					self       .set_break(f,l,{"cond":args[1]})89			else:90				self.parent.clear_break(f,l)91				self       .clear_break(f,l)92				self.parent.set_break(f,l,{})93				self       .set_break(f,l,{})94			# self.parent.toggle_break(f,l)95			# self.toggle_break(f,l)96			self.wait_cmd(frame)97		elif s in ['s']: self.set_step()98		elif s in ['q']: self.set_quit()99		elif s in ['r']: self.set_return(frame)100		elif s in ['u']: self.set_until(frame, int(args[0]) if args else None)101		elif s in ['o']:102			self.curidx = self.curidx-1103			self.wait_cmd(self.stack[self.curidx][0])104		elif s in ['i']:105			self.curidx = self.curidx+1106			self.wait_cmd(self.stack[self.curidx][0])107		elif s in ['h']:108			self.show_help()109			self.wait_cmd(frame)110		else           : self.wait_cmd(frame)111	def show_help(self):112		self.parent.show_help("""113			Commands               Description114			c                      Continue execution, only stop when a breakpoint is encountered.115			n                      Continue execution until the next line in the current function is reached or116			                       it returns.117			b LINE[ COND|RANGE|c]  Set break at LINE in the current file. If a COND expression is supplied, the118			                       debugger stops at LINE only when COND evaluates to True. If a RANGE 119			                       expression (a expression matching the syntax of Python slices) is supplied,120			                       the debugger stops at LINE only when the hit count of the breakpoint is one121			                       of the numbers generated by RANGE. If letter c appears after LINE, the122			                       breakpoint is cleared.123			s                      Execute the current line, stop at the first possible occasion (either in a124			                       function that is called or in the current function).125			q                      Quit the debugger.126			r                      Continue execution until the current function returns.127			u [LINE]               Without argument, continue execution until the line with a number greater128			                       than the current one is reached.  With a line number, continue execution129			                       until a line with a number greater or equal than LINE is reached. In both130			                       cases, also stop when the current frame returns.131			o                      Move the current frame one level up in the stack trace (to an older frame).132			i                      Move the current frame one level down in the stack trace (to a newer frame).133			h                      Show this help.134			If no command is given, the previous command is repeated.135			""")136	def runscript(self,filename):137		# The script has to run in __main__ namespace (or imports from138		# __main__ will break).139		#140		# So we clear up the __main__ and set several special variables141		# (this gets rid of pdb's globals and cleans old variables on restarts).142		import __main__143		__main__.__dict__144		main_copy = __main__.__dict__.copy()145		__main__.__dict__.clear()146		__main__.__dict__.update({	"__name__"    : "__main__",147									"__file__"    : filename,148									"__builtins__": __builtins__,149								})150		# When bdb sets tracing, a number of call and line events happens151		# BEFORE debugger even reaches user's code (and the exact sequence of152		# events depends on python version). So we take special measures to153		# avoid stopping before we reach the main script (see user_line and154		# user_call for details).155		self.mainpyfile = os.path.realpath(filename)156		self._user_requested_quit = False157		with open(filename, "rb") as fp:158			statement = "exec(compile(%r, %r, 'exec'))" % \159						(fp.read(), self.mainpyfile)160		self.clear_all_breaks()161		for filenam,lines in self.breakpoints.items():162			for l,bpinfo in lines.items():163				self.set_break(filenam, l,bpinfo)164		# Replace pdb's dir with script's dir in front of module search path.165		sys.path[0] = os.path.dirname(self.mainpyfile)166		try :167			self._wait_for_mainpyfile = True168			self.run(statement)169		except SyntaxError:170			print ("SyntaxError")171			traceback.print_exc()172			self.parent.show_exception("syntax error")173		except:174			traceback.print_exc()175			print ("Uncaught exception. Entering post mortem debugging")176			typ, val, t = sys.exc_info()177			self.parent.show_exception(str(val))178			self.stack, self.curidx = self.get_stack(None, t)179			self.wait_cmd(self.stack[self.curidx][0])			180		for filenam,lines in self.breakpoints.items():181			for l,bpinfo in lines.items():182				if "hits" in bpinfo:183					bpinfo["hits"]=0184		self.parent.finished()185		__main__.__dict__.clear()186		__main__.__dict__.update(main_copy)187	def tryeval(self,expr):188		try:189			return eval(expr, self.curframe.f_globals, self.curframe.f_locals)190		except Exception as e:191			return e192	def toggle_break(self,filename,line):193		if not filename in self.breakpoints: self.breakpoints.update({filename:{}})194		bps = self.breakpoints[filename]195		bps.pop(line)   if line in bps else bps.update({line:{}})196		(self.set_break if line in bps else self.clear_break)(filename, line)197	def break_here(self,frame):198		if not bdb.Bdb.break_here(self,frame): return False199		f, l = filename(frame), line(frame)200		bp = self.breakpoints[f][l]201		if not "range" in bp: return True202		bp["hits"] += 1203		return n_in_range(bp["hits"]-1,bp['range'])204	def set_break(self,filename,line,bpinfo={},**kwargs):205		bdb.Bdb.set_break(self,filename,line,**(bpinfo if "cond" in bpinfo else {}))206		if not filename in self.breakpoints: self.breakpoints.update({filename:{}})207		bps = self.breakpoints[filename]208		if not line in bps: bps.update({line:{}})209		bps[line]=bpinfo210	def clear_break(self,filename,line):211		bdb.Bdb.clear_break(self,filename,line)212		if not filename in self.breakpoints: self.breakpoints.update({filename:{}})213		bps = self.breakpoints[filename]214		if line in bps: bps.pop(line)215	def filter_vars(self, d):216		# 		sf = os.path.dirname(os.path.abspath(inspect.getsourcefile(v)))217		# try:218		# 	d.pop("__builtins__") # this messes up things (not eval defined): copy d first219		# except:...debug_calls.py
Source:debug_calls.py  
1import inspect2import pdb3from functools import wraps4from typing import Callable5def create_arglist(args, kwargs):6    return ', '.join((*(repr(x) for x in args), *(f'{x[0]}={x[1]!r}'7                                                  for x in kwargs.items())))8def debug_calls(*db_args, **db_kwargs):9    def _debug_calls(func: Callable):10        @wraps(func)11        def wrapper(*args, **kwargs):12            sig = inspect.signature(func)13            caller = inspect.stack()[1]14            arglist = create_arglist(args, kwargs)15            print(f'{func.__name__}({arglist}) '16                  f'called by {caller.function} '17                  f'in file "{caller.filename}" '18                  f'on line {caller.lineno}')19            if set_break:20                # return func(*args, **kwargs)21                if not wrapper.condition:22                    wrapper.condition = input(f'Debug {func.__name__} when?')23                # NOTE: eval() is insecure and should not be used in live systems24                if eval(wrapper.condition, {},25                        sig.bind(*args, **kwargs).arguments):26                    print(f'Condition met: {wrapper.condition}')27                    return pdb.runcall(func, *args, **kwargs)28                print(f'Condition not met: {wrapper.condition}')29            return func(*args, **kwargs)30        wrapper.condition = None31        return wrapper32    if db_kwargs and 'set_break' in db_kwargs:33        set_break = db_kwargs['set_break']34    else:35        set_break = False36    if len(db_args) == 1 and callable(db_args[0]):37        return _debug_calls(db_args[0])...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!!
