Best Python code snippet using avocado_python
imported.py
Source:imported.py
...81 an importable spec82 :type symbol_is_module: bool83 """84 modules_paths = sys.path85 modules_paths.insert(0, self.get_relative_module_fs_path())86 spec = None87 for component, previous in self._walk_importable_components(symbol_is_module):88 if previous:89 modules_paths = [90 os.path.join(mod, previous) for mod in modules_paths[:]91 ]92 spec = PathFinder.find_spec(component, modules_paths)93 if spec is None:94 break95 return spec96 def is_importable(self, symbol_is_module=False):97 """Checks whether this imported symbol seems to be importable.98 This is a check based on the limitations that we do not99 actually perform an import, and assumes a directory structure100 with modules.101 :param symbol_is_module: if it's known that the symbol is also102 a module, include it in the search for103 an importable spec104 :type symbol_is_module: bool105 """106 return self.get_importable_spec(symbol_is_module) is not None107 @staticmethod108 def _split_last_module_path_component(module_path):109 """Splits a module path into a lower and topmost components.110 It also discards any information about relative location.111 :param module_path: a module path, such as "os" or "os.path"112 or "..selftests.utils"113 :type module_path: str114 :returns: the lower and topmost components115 :rtype: tuple116 """117 non_relative = module_path.strip(".")118 if "." in non_relative:119 module_components = non_relative.rsplit(".", 1)120 if len(module_components) == 2:121 return (module_components[0], module_components[1])122 return ("", non_relative)123 @staticmethod124 def _get_relative_prefix(statement):125 """Returns the string that represents to the relative import level.126 :param statement: an "import from" ast statement127 :type statement: :class:`ast.ImportFrom`128 :returns: the string that represents the relative import level.129 :rtype: str130 """131 relative_level = getattr(statement, "level", 0) or 0132 return "".join(["." for _ in range(relative_level)])133 @staticmethod134 def get_symbol_from_statement(statement):135 return ImportedSymbol.get_symbol_module_path_from_statement(statement)[0]136 @staticmethod137 def get_module_path_from_statement(statement):138 return ImportedSymbol.get_symbol_module_path_from_statement(statement)[1]139 @staticmethod140 def get_symbol_module_path_from_statement(statement, name_index=0):141 symbol = ""142 module_path = ""143 module_alias = ""144 symbol_alias = ""145 import_as = get_statement_import_as(statement)146 names = list(import_as.keys())147 as_names = list(import_as.values())148 if isinstance(statement, ast.Import):149 # On an Import statement, it's impossible to import a symbol150 # so everything is the module_path151 module_path = names[name_index]152 module_alias = as_names[name_index]153 elif isinstance(statement, ast.ImportFrom):154 symbol = names[name_index]155 relative = ImportedSymbol._get_relative_prefix(statement)156 module_name = statement.module or ""157 module_path = relative + module_name158 symbol_alias = as_names[name_index]159 return symbol, module_path, module_alias, symbol_alias160 @property161 def module_name(self):162 """The final name of the module from its importer perspective.163 If a alias exists, it will be the alias name. If not, it will164 be the original name.165 """166 if self.module_alias:167 return self.module_alias168 return self.module_path169 @property170 def symbol_name(self):171 """The final name of the symbol from its importer perspective.172 If a alias exists, it will be the alias name. If not, it will173 be the original name.174 """175 if self.symbol_alias:176 return self.symbol_alias177 return self.symbol178 @classmethod179 def from_statement(cls, statement, importer_fs_path=None, index=0):180 (181 symbol,182 module_path,183 module_alias,184 symbol_alias,185 ) = cls.get_symbol_module_path_from_statement(statement, index)186 return cls(module_path, symbol, importer_fs_path, module_alias, symbol_alias)187 def to_str(self):188 """Returns a string representation of the plausible statement used."""189 if not self.symbol:190 return f"import {self.module_path}"191 return f"from {self.module_path} import {self.symbol}"192 def is_relative(self):193 """Returns whether the imported symbol is on a relative path."""194 return self.module_path.startswith(".")195 def get_relative_module_fs_path(self):196 """Returns the module base dir, based on its relative path197 The base dir for the module is the directory where one is198 expected to find the first module of the module path. For a199 module path of "..foo.bar", and its importer being at200 "/abs/path/test.py", the base dir where "foo" is supposed to201 be found would be "/abs". And as a consequence, "bar" would202 be found at "/abs/foo/bar".203 This assumes that the module path is indeed related to the location204 of its importer. This may not be true if the namespaces match, but205 are distributed across different filesystem paths.206 """207 path = os.path.dirname(self.importer_fs_path)208 for char in self.module_path[1:]:209 if char != ".":210 break211 path = os.path.dirname(path)212 return path213 def get_parent_fs_path(self):214 if self.is_relative():215 return self.get_relative_module_fs_path()216 parent_path = os.path.dirname(self.importer_fs_path)217 if self.module_path:218 return os.path.join(parent_path, self.module_path)219 return parent_path220 def __repr__(self):221 return (222 f'<ImportedSymbol module_path="{self.module_path}"'223 f'symbol="{self.symbol}"'224 f'importer_fs_path="{self.importer_fs_path}">'225 )226 def __eq__(self, other):227 return (228 (self.module_path == other.module_path)229 and (self.symbol == other.symbol)...
test_safeloader_imported.py
Source:test_safeloader_imported.py
...101 imported_symbol = ImportedSymbol(102 ".module", "symbol", "/abs/fs/location/test.py"103 )104 self.assertEqual(105 imported_symbol.get_relative_module_fs_path(), "/abs/fs/location"106 )107 def test_upper(self):108 imported_symbol = ImportedSymbol(109 "..module", "symbol", "/abs/fs/location/test.py"110 )111 self.assertEqual(imported_symbol.get_relative_module_fs_path(), "/abs/fs")112 def test_upper_from_statement(self):113 statement = ast.parse("from ..utils import utility").body[0]114 importer = "/abs/fs/location/of/selftests/unit/test_foo.py"115 symbol = ImportedSymbol.from_statement(statement, importer)116 self.assertEqual(117 symbol.get_relative_module_fs_path(), "/abs/fs/location/of/selftests"118 )119 def test_same_from_statement(self):120 statement = ast.parse("from .test_bar import symbol").body[0]121 importer = "/abs/fs/location/of/selftests/unit/test_foo.py"122 symbol = ImportedSymbol.from_statement(statement, importer)123 self.assertEqual(124 symbol.get_relative_module_fs_path(), "/abs/fs/location/of/selftests/unit"125 )126class ParentPath(unittest.TestCase):127 def test_compound(self):128 statement = ast.parse("from path import parent3").body[0]129 importer = "/abs/fs/location/of/imports.py"130 symbol = ImportedSymbol.from_statement(statement, importer)131 self.assertEqual(symbol.get_parent_fs_path(), "/abs/fs/location/of/path")132 def test_compound_levels(self):133 statement = ast.parse("from .path.parent8 import Class8").body[0]134 importer = "/abs/fs/location/of/imports.py"135 symbol = ImportedSymbol.from_statement(statement, importer)136 self.assertEqual(symbol.get_parent_fs_path(), "/abs/fs/location/of")137class Importable(unittest.TestCase):138 def test_single(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!!