Best Python code snippet using avocado_python
reference_ast_node_visitor.py
Source:reference_ast_node_visitor.py  
...105    elif isinstance(node, _ast.Import):106      self._handle_import(node)107      return108    elif isinstance(node, _ast.ImportFrom):109      self._handle_import_from(node)110      return111    elif isinstance(node, _ast.Assign):112      self._handle_assign(node)113      return114    elif isinstance(node, (_ast.Name, _ast.Attribute)):115      # Note: This will miss Names & Attributes contained in Imports and116      # Assign[ment]s since the children of those nodes are handled directly.117      if isinstance(node, _ast.Attribute):118        name = _get_complete_attribute_path(node)119      else:120        name = node.id121      self.references.append((name, self._find_matching_name(node), self.current_context, lineno))122    elif isinstance(node, _ast.Module):123      old_global = self.current_global_names124      module_names = []125      for module_name in module_members:126        module_names.insert(0, (module_name, join_names(name, module_name), 'Unknown'))127      self.current_global_names = module_names + old_global128    #Figure out remainder of logic aroung LEBG +_find_matching_name129    old_parent = self.parent130    self.parent = node131    old_context = self.current_context132    self.current_context = complete_name133    super(ReferenceAstNodeVisitor, self).generic_visit(node)134    self.parent = old_parent135    self.current_context = old_context136    if isinstance(node, _ast.Module):137      self.current_global_names = old_global138    elif isinstance(node, (_ast.ClassDef, _ast.FunctionDef)):139      # In case this is a nested class, restore old vals.140      # self.current_instance_names = outter_instance_vals141      # TODO### Add this to142      self.current_enclosing_names = old_enclosing143      if isinstance(node, _ast.ClassDef):144        # If it's a class, keep the things around but with full names.145        for local_name, local_complete_name, local_type_name in self.current_local_names[::-1]:146          old_local.insert(0, (join_names(name, local_name), local_complete_name, local_type_name))147      self.current_local_names = old_local148  def _find_matching_name(self, node):149    if isinstance(node, _ast.Attribute):150      name = _get_complete_attribute_path(node)151    else:152      assert isinstance(node, _ast.Name), type_name(node)153      name = node.id154    for name_list in (self.current_local_names, self.current_enclosing_names, self.current_instance_names,155                      self.current_global_names):156      # TODO: For instance names, prepend 'self.' to each for matching here.157      for node_name_tuple in name_list:158        if node_name_tuple[0] == name:159          return node_name_tuple[1]  # complete_name160    # Note that this must come below the above due to LEGB ordering.161    if name in builtins:162      return name163    return None164  def _handle_assign(self, node):165    self.generic_visit(node.value)166    for target in node.targets:167      # assert isinstance(target, _ast.Name)168      if isinstance(target, _ast.Name):169        self.current_local_names.insert(170            0, (target.id, join_names(self.current_context, target.id), type_name(node)))171      else:172        assert isinstance(target, _ast.Attribute), f'{type(target)}'173        name = _get_complete_attribute_path(target)174        self.current_local_names.append((name, name, type_name(node)))175  def _handle_import(self, node):176    for name_alias in node.names:  # Each name is an _ast.alias.177      assert isinstance(name_alias, _ast.alias), f'{type(name_alias)}'178      name = name_alias.asname if name_alias.asname is not None else name_alias.name179      self._process_module(name_alias.name)180      self.current_local_names.append((name, name_alias.name, type_name(node)))181  def _handle_import_from(self, node):182    for name_alias in node.names:  # Each name is an _ast.alias.183      assert isinstance(name_alias, _ast.alias), f'{type(name_alias)}'184      name = name_alias.asname if name_alias.asname is not None else name_alias.name185      if name is '*':186        traveler = self._process_module(node.module)187        # Blegh.188        self.current_local_names += traveler.current_local_names189        # for module_local_name in traveler.current_local_names:190        #   self.current_local_names.append()191      else:192        self.current_local_names.append((name, join_names(node.module, name_alias.name), type_name(node)))193  def _process_module(self, module_name):194    module_spec = importlib.util.find_spec(module_name)195    if module_spec.origin is None:...ImportAnalyzer.py
Source:ImportAnalyzer.py  
...132            return accepted_list133    134    def _dfs(self, node):135        if node.type == 'import_from':136            self._handle_import_from(node)137        elif node.type == 'import_name':138            self._handle_import_name(node)139        elif hasattr(node, 'children'):140            for child in node.children:141                self._dfs(child)142        else:143            pass144    def _count(self, value):145        if value in self.accepted_list:146            if len(self.counter[value]) == 0 or self.counter[value][-1] != self.entry:147                # Count by file, so do not count the same value twice148                self.counter[value].append(self.entry)149    150    def _handle_import_from(self, node):151        def get_imports(node):152            if isinstance(node.children[3], parso.python.tree.Operator) and node.children[3].value == "(":153                return node.children[4]154            else:155                return node.children[3]156        module = node.children[1]157        imports = get_imports(node)158        if module.type == 'dotted_name' and module.children[0].value == self.package:159            # Case 1: from Library.B import C160            for dotted_name in module.children:161                if dotted_name.type == 'name':162                    self._count(dotted_name.value)163        if ((module.type == 'name' and module.value == self.package) or164            (module.type == 'dotted_name' and module.children[0].value == self.package)):...module.py
Source:module.py  
...102    @staticmethod103    def _get_name_from_alias_statement(alias):104        """Returns the aliased name or original one."""105        return alias.asname if alias.asname else alias.name106    def _handle_import_from(self, statement, interesting_klass):107        self.add_imported_symbol(statement)108        if interesting_klass in [name.name for name in statement.names]:109            self.interesting_klass_found = True110        if statement.module != self.module:111            return112        name = get_statement_import_as(statement).get(self.klass, None)113        if name is not None:114            self.klass_imports.add(name)115    @staticmethod116    def _all_module_level_names(full_module_name):117        result = []118        components = full_module_name.split(".")[:-1]119        for topmost in range(len(components)):120            result.append(".".join(components[-topmost:]))121            components.pop()122        return result123    def _handle_import(self, statement):124        self.add_imported_symbol(statement)125        imported_as = get_statement_import_as(statement)126        name = imported_as.get(self.module, None)127        if name is not None:128            self.mod_imports.add(name)129        for as_name in imported_as.values():130            for mod_name in self._all_module_level_names(as_name):131                if mod_name == self.module:132                    self.mod_imports.add(mod_name)133    def iter_classes(self, interesting_klass=None):134        """135        Iterate through classes and keep track of imported avocado statements136        """137        for statement in self.mod.body:138            # Looking for a 'from <module> import <klass>'139            if isinstance(statement, ast.ImportFrom):140                self._handle_import_from(statement, interesting_klass)141            # Looking for a 'import <module>'142            elif isinstance(statement, ast.Import):143                self._handle_import(statement)144            # Looking for a 'class Anything(anything):'145            elif isinstance(statement, ast.ClassDef):...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!!
