Best Python code snippet using slash
check_funcs.py
Source:check_funcs.py
1import re2from functools import partial, wraps3from protowhat.Feedback import Feedback4MSG_CHECK_FALLBACK = "Your submission is incorrect. Try again!"5DEFAULT_MISSING_MSG = "Could not find the {index}{node_name}."6DEFAULT_APPEND_MSG = "Check the {index}{node_name}. "7def requires_ast(f):8 @wraps(f)9 def wrapper(*args, **kwargs):10 state = kwargs.get("state", args[0] if len(args) else None)11 state_ast = [state.student_ast, state.solution_ast]12 # fail if no ast parser in use13 if any(ast is None for ast in state_ast):14 raise TypeError(15 "Trying to use ast, but it is None. Are you using a parser? {} {}".format(16 args, kwargs17 )18 )19 # check whether the parser passed or failed for some code20 # if safe_parsing is enabled in the Dispatcher (otherwise an exception would be raised earlier)21 ParseError = state.ast_dispatcher.ParseError22 parse_fail = any(isinstance(ast, ParseError) for ast in state_ast)23 if parse_fail:24 return state # skip test25 else:26 return f(*args, **kwargs) # proceed with test27 return wrapper28@requires_ast29def check_node(30 state, name, index=0, missing_msg=None, priority=None31):32 """Select a node from abstract syntax tree (AST), using its name and index position.33 Args:34 state: State instance describing student and solution code. Can be omitted if used with Ex().35 name : the name of the abstract syntax tree node to find.36 index: the position of that node (see below for details).37 missing_msg: feedback message if node is not in student AST.38 priority: the priority level of the node being searched for. This determines whether to39 descend into other AST nodes during the search. Higher priority nodes descend40 into lower priority. Currently, the only important part of priority is that41 setting a very high priority (e.g. 99) will search every node.42 :Example:43 If both the student and solution code are.. ::44 SELECT a FROM b; SELECT x FROM y;45 then we can focus on the first select with::46 # approach 1: with manually created State instance47 state = State(*args, **kwargs)48 new_state = check_node(state, 'SelectStmt', 0)49 # approach 2: with Ex and chaining50 new_state = Ex().check_node('SelectStmt', 0)51 """52 has_custom_message = bool(missing_msg)53 if missing_msg is None:54 missing_msg = DEFAULT_MISSING_MSG55 df = partial(state.ast_dispatcher.find, name, priority=priority)56 sol_stmt_list = df(state.solution_ast)57 try:58 sol_stmt = sol_stmt_list[index]59 except IndexError:60 raise IndexError("Can't get %s statement at index %s" % (name, index))61 stu_stmt_list = df(state.student_ast)62 try:63 stu_stmt = stu_stmt_list[index]64 except IndexError:65 # use speaker on ast dialect module to get message, or fall back to generic66 ast_path = state.get_ast_path() or "highlighted code"67 msg = state.ast_dispatcher.describe(68 sol_stmt, missing_msg, index=index, ast_path=ast_path69 )70 if msg is None:71 msg = MSG_CHECK_FALLBACK72 if has_custom_message:73 state.report(msg, append=False)74 state.report(msg)75 append_message = state.ast_dispatcher.describe(76 sol_stmt, DEFAULT_APPEND_MSG, index=index77 )78 return state.to_child(79 student_ast=stu_stmt, solution_ast=sol_stmt, append_message=append_message80 )81@requires_ast82def check_edge(state, name, index=0, missing_msg=None):83 """Select an attribute from an abstract syntax tree (AST) node, using the attribute name.84 Args:85 state: State instance describing student and solution code. Can be omitted if used with Ex().86 name: the name of the attribute to select from current AST node.87 index: entry to get from a list field. If too few entires, will fail with missing_msg.88 missing_msg: feedback message if attribute is not in student AST.89 :Example:90 If both the student and solution code are.. ::91 SELECT a FROM b; SELECT x FROM y;92 then we can get the from_clause using ::93 # approach 1: with manually created State instance -----94 state = State(*args, **kwargs)95 select = check_node(state, 'SelectStmt', 0)96 clause = check_edge(select, 'from_clause')97 # approach 2: with Ex and chaining ---------------------98 select = Ex().check_node('SelectStmt', 0) # get first select statement99 clause = select.check_edge('from_clause', None) # get from_clause (a list)100 clause2 = select.check_edge('from_clause', 0) # get first entry in from_clause101 """102 has_custom_message = bool(missing_msg)103 if missing_msg is None:104 missing_msg = DEFAULT_MISSING_MSG105 def select(node_name, node):106 attr = state.ast_dispatcher.select(node_name, node)107 if attr and isinstance(attr, list) and index is not None:108 attr = attr[index]109 return attr110 try:111 sol_attr = select(name, state.solution_ast)112 except IndexError:113 raise IndexError("Can't get %s attribute" % name)114 # use speaker on ast dialect module to get message, or fall back to generic115 ast_path = state.get_ast_path() or "highlighted code"116 _msg = state.ast_dispatcher.describe(117 state.student_ast, missing_msg, field=name, index=index, ast_path=ast_path118 )119 if _msg is None:120 _msg = MSG_CHECK_FALLBACK121 try:122 stu_attr = select(name, state.student_ast)123 except:124 if has_custom_message:125 state.report(_msg, append=False)126 state.report(_msg)127 # fail if attribute exists, but is none only for student128 if stu_attr is None and sol_attr is not None:129 if has_custom_message:130 state.report(_msg, append=False)131 state.report(_msg)132 append_message = state.ast_dispatcher.describe(133 state.student_ast, "Check the {field_name}. ", index=index, field=name134 )135 return state.to_child(136 student_ast=stu_attr, solution_ast=sol_attr, append_message=append_message137 )138def has_code(139 state,140 text,141 incorrect_msg="Check the {ast_path}. The checker expected to find {text}.",142 fixed=False,143):144 """Test whether the student code contains text.145 Args:146 state: State instance describing student and solution code. Can be omitted if used with Ex().147 text : text that student code must contain. Can be a regex pattern or a simple string.148 incorrect_msg: feedback message if text is not in student code.149 fixed: whether to match text exactly, rather than using regular expressions.150 Note:151 Functions like ``check_node`` focus on certain parts of code.152 Using these functions followed by ``has_code`` will only look153 in the code being focused on.154 :Example:155 If the student code is.. ::156 SELECT a FROM b WHERE id < 100157 Then the first test below would (unfortunately) pass, but the second would fail..::158 # contained in student code159 Ex().has_code(text="id < 10")160 # the $ means that you are matching the end of a line161 Ex().has_code(text="id < 10$")162 By setting ``fixed = True``, you can search for fixed strings::163 # without fixed = True, '*' matches any character164 Ex().has_code(text="SELECT * FROM b") # passes165 Ex().has_code(text="SELECT \\\\* FROM b") # fails166 Ex().has_code(text="SELECT * FROM b", fixed=True) # fails167 You can check only the code corresponding to the WHERE clause, using ::168 where = Ex().check_node('SelectStmt', 0).check_edge('where_clause')169 where.has_code(text = "id < 10)170 """171 stu_ast = state.student_ast172 stu_code = state.student_code173 # fallback on using complete student code if no ast174 ParseError = state.ast_dispatcher.ParseError175 def get_text(ast, code):176 if isinstance(ast, ParseError):177 return code178 try:179 return ast.get_text(code) or ""180 except:181 return code182 stu_text = get_text(stu_ast, stu_code)183 _msg = incorrect_msg.format(184 ast_path=state.get_ast_path() or "highlighted code", text=text185 )186 # either simple text matching or regex test187 res = text in stu_text if fixed else re.search(text, stu_text)188 if not res:189 state.report(_msg)190 return state191@requires_ast192def has_equal_ast(193 state,194 incorrect_msg=None,195 sql=None,196 start=["expression", "subquery", "sql_script"][0],197 exact=None,198 should_append_msg=False,199):200 """Test whether the student and solution code have identical AST representations201 Args:202 state: State instance describing student and solution code. Can be omitted if used with Ex().203 incorrect_msg: feedback message if student and solution ASTs don't match204 sql : optional code to use instead of the solution ast that is zoomed in on.205 start: if ``sql`` arg is used, the parser rule to parse the sql code.206 One of 'expression' (the default), 'subquery', or 'sql_script'.207 exact: whether to require an exact match (True), or only that the208 student AST contains the solution AST. If not specified, this209 defaults to ``True`` if ``sql`` is not specified, and to ``False``210 if ``sql`` is specified. You can always specify it manually.211 should_append_msg: prepend the auto generated incorrect_msg with the previous append_messages.212 :Example:213 Example 1 - Suppose the solution code is ::214 SELECT * FROM cities215 and you want to verify whether the `FROM` part is correct: ::216 Ex().check_node('SelectStmt').from_clause().has_equal_ast()217 Example 2 - Suppose the solution code is ::218 SELECT * FROM b WHERE id > 1 AND name = 'filip'219 Then the following SCT makes sure ``id > 1`` was used somewhere in the WHERE clause.::220 Ex().check_node('SelectStmt') \\/221 .check_edge('where_clause') \\/222 .has_equal_ast(sql = 'id > 1')223 """224 has_custom_message = bool(incorrect_msg)225 if not has_custom_message:226 if should_append_msg: # Remove the ast_path mention because we are prepending.227 incorrect_msg = "{extra}"228 else:229 incorrect_msg = "Check the {ast_path}. {extra}"230 ast = state.ast_dispatcher.ast_mod231 sol_ast = state.solution_ast if sql is None else ast.parse(sql, start)232 # if sql is set, exact defaults to False.233 # if sql not set, exact defaults to True.234 if exact is None:235 exact = sql is None236 stu_rep = repr(state.student_ast)237 sol_rep = repr(sol_ast)238 def get_str(ast, code, sql):239 if sql:240 return sql241 if isinstance(ast, str):242 return ast243 try:244 return ast.get_text(code)245 except:246 return None247 sol_str = get_str(state.solution_ast, state.solution_code, sql)248 _msg = incorrect_msg.format(249 ast_path=state.get_ast_path()250 or ("code" if state.highlighting_disabled else "highlighted code"),251 extra="The checker expected to find `{}` in there.".format(sol_str)252 if sol_str253 else "Something is missing.",254 )255 if (exact and (sol_rep != stu_rep)) or (not exact and (sol_rep not in stu_rep)):256 if should_append_msg:257 state.report(_msg)258 state.report(_msg, append=False)259 return state260def has_parsed_ast(state):261 asts = [state.student_ast, state.solution_ast]262 if any(isinstance(c, state.ast_dispatcher.ParseError) for c in asts):263 state.report("AST did not parse")...
error.py
Source:error.py
...35 assert hasattr(self, 'exc_info')36 self.exc_info = None37 for frame in self.traceback.frames:38 frame.forget_python_frame()39 def has_custom_message(self):40 return self._has_custom_message41 def mark_as_failure(self):42 self._is_failure = True43 def is_fatal(self):44 return self._fatal45 @property46 @deprecated('Use error.exception_str', what='error.exception', since='1.2.3')47 def exception(self):48 return self.exception_str49 def mark_fatal(self):50 """Marks this error as fatal, causing session termination51 """52 self._fatal = True53 return self...
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!!