Best Python code snippet using avocado_python
assets.py
Source:assets.py
...132 self.current_method = node.name133 self.asgmts[self.current_klass][self.current_method] = {}134 self.generic_visit(node)135 @staticmethod136 def _ast_list_to_list(node):137 result = []138 for item in node.value.elts:139 if hasattr(item, "value"):140 result.append(item.value)141 return result142 def visit_Assign(self, node): # pylint: disable=C0103143 """Visit Assign on AST and build assignments.144 This method will visit and build list of assignments that matches the145 pattern pattern `name = string`.146 :param node: AST node to be evaluated147 :type node: ast.*148 """149 if isinstance(node.value, (ast.Str, ast.List)):150 # make sure we are into a class method, we are not supporting151 # attributes and module constant assignments at this time152 if self.current_klass and self.current_method:153 # variables to make dictionary assignment line shorter154 cur_klass = self.current_klass155 cur_method = self.current_method156 # if it is a class attribute, save the attribute name157 # otherwise, save the local variable name158 if isinstance(node.targets[0], ast.Attribute):159 name = node.targets[0].attr160 else:161 name = node.targets[0].id162 if isinstance(node.value, ast.Str):163 self.asgmts[cur_klass][cur_method][name] = node.value.s164 elif isinstance(node.value, ast.List):165 self.asgmts[cur_klass][cur_method][name] = self._ast_list_to_list(166 node167 )168 self.generic_visit(node)169 def visit_Call(self, node): # pylint: disable=C0103170 """Visit Calls on AST and build list of calls that matches the pattern.171 :param node: AST node to be evaluated172 :type node: ast.*173 """174 # make sure we are into a class method175 if self.current_klass and self.current_method:176 if isinstance(node.func, ast.Attribute):177 if self.PATTERN in node.func.attr:178 call = self._parse_args(node)179 if call:...
collections.py
Source:collections.py
...13 )14 # nested nodes being handled in function above15 def visit_List(self, node):16 self.collections.append(17 _ast_list_to_list(node, convert_str_values=self.convert_str_values)18 )19 # nested nodes being handled in function above20def extract_collections_from_ast(ast_node: ast.AST, convert_str_values: bool = False) -> List[DictOrList]:21 """22 returns a list of dicts or lists. Goes through ast, converting23 ast.Dict to dict and ast.List to list, leaving the rest intact.24 Returns a list of these created dicts and lists25 Args:26 ast_node:27 Returns:28 """29 adlc = AstDictListConverter(convert_str_values=convert_str_values)30 adlc.visit(ast_node)31 return adlc.collections32def extract_collection_from_ast(ast_node: ast.AST, convert_str_values: bool = False) -> DictOrListOrNone:33 collections = extract_collections_from_ast(ast_node=ast_node, convert_str_values=convert_str_values)34 if len(collections) == 0:35 return None36 if len(collections) > 1:37 raise ValueError(f'expected to extract one assignment from ast. got {len(collections)} '38 f'assigns: {collections}')39 return collections[0]40def _ast_dict_or_list_to_dict_or_list(node: AstDictOrList, convert_str_values: bool = False) -> DictOrList:41 if isinstance(node, ast.Dict):42 return _ast_dict_to_dict(node, convert_str_values=convert_str_values)43 elif isinstance(node, ast.List):44 return _ast_list_to_list(node, convert_str_values=convert_str_values)45 else:46 raise ValueError(f'expected ast.Dict or ast.List. Got {node} of type {type(node)}')47def _ast_dict_to_dict(ast_dict: ast.Dict, convert_str_values: bool = False) -> dict:48 out_dict = {}49 for key, value in zip(ast_dict.keys, ast_dict.values):50 key = cast(ast.Str, key)51 key_string = key.s52 if isinstance(value, (ast.Dict, ast.List)):53 store_value = _ast_dict_or_list_to_dict_or_list(value, convert_str_values=convert_str_values)54 else:55 store_value = _convert_to_str_if_ast_str_and_desired(value, convert_desired=convert_str_values)56 out_dict[key_string] = store_value57 return out_dict58def _ast_list_to_list(ast_list: ast.List, convert_str_values: bool = False) -> list:59 out_list = []60 for item in ast_list.elts:61 if isinstance(item, (ast.Dict, ast.List)):62 store_item = _ast_dict_or_list_to_dict_or_list(item, convert_str_values=convert_str_values)63 else:64 store_item = _convert_to_str_if_ast_str_and_desired(item, convert_desired=convert_str_values)65 out_list.append(store_item)66 return out_list67def _convert_to_str_if_ast_str_and_desired(ast_node: ast.AST, convert_desired=False):68 if not convert_desired:69 return ast_node70 if isinstance(ast_node, ast.Str):71 return ast_node.s72 return ast_node
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!!