本文整理汇总了Python中astroid.manager.AstroidManager.ast_from_class方法的典型用法代码示例。如果您正苦于以下问题:Python AstroidManager.ast_from_class方法的具体用法?Python AstroidManager.ast_from_class怎么用?Python AstroidManager.ast_from_class使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类astroid.manager.AstroidManager
的用法示例。
在下文中一共展示了AstroidManager.ast_from_class方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AstroidManagerTC
# 需要导入模块: from astroid.manager import AstroidManager [as 别名]
# 或者: from astroid.manager.AstroidManager import ast_from_class [as 别名]
class AstroidManagerTC(TestCase):
def setUp(self):
self.manager = AstroidManager()
self.manager.astroid_cache.clear()
def test_ast_from_module(self):
import unittest
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.pure_python, True)
import time
astroid = self.manager.ast_from_module(time)
self.assertEqual(astroid.pure_python, False)
def test_ast_from_class(self):
astroid = self.manager.ast_from_class(int)
self.assertEqual(astroid.name, "int")
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object)
self.assertEqual(astroid.name, "object")
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn("__setattr__", astroid)
def _test_ast_from_zip(self, archive):
origpath = sys.path[:]
sys.modules.pop("mypypa", None)
archive_path = join(DATA, archive)
sys.path.insert(0, archive_path)
try:
module = self.manager.ast_from_module_name("mypypa")
self.assertEqual(module.name, "mypypa")
self.assertTrue(module.file.endswith("%s/mypypa" % archive), module.file)
finally:
# remove the module, else after importing egg, we don't get the zip
if "mypypa" in self.manager.astroid_cache:
del self.manager.astroid_cache["mypypa"]
del self.manager._mod_file_cache[("mypypa", None)]
if archive_path in sys.path_importer_cache:
del sys.path_importer_cache[archive_path]
sys.path = origpath
def test_ast_from_module_name_egg(self):
self._test_ast_from_zip("MyPyPa-0.1.0-py2.5.egg")
def test_ast_from_module_name_zip(self):
self._test_ast_from_zip("MyPyPa-0.1.0-py2.5.zip")
def test_from_directory(self):
obj = self.manager.project_from_files([DATA], _silent_no_wrap, "data")
self.assertEqual(obj.name, "data")
self.assertEqual(obj.path, join(DATA, "__init__.py"))
def test_project_node(self):
obj = self.manager.project_from_files([DATA], _silent_no_wrap, "data")
expected = set(
["SSL1", "__init__", "all", "appl", "format", "module", "module2", "noendingnewline", "nonregr", "notall"]
)
expected = [
"data",
"data.SSL1",
"data.SSL1.Connection1",
"data.absimport",
"data.all",
"data.appl",
"data.appl.myConnection",
"data.email",
"data.format",
"data.module",
"data.module2",
"data.noendingnewline",
"data.nonregr",
"data.notall",
]
self.assertListEqual(sorted(k for k in list(obj.keys())), expected)
def test_do_not_expose_main(self):
obj = self.manager.ast_from_module_name("__main__")
self.assertEqual(obj.name, "__main__")
self.assertEqual(list(obj.items()), [])
示例2: AstroidManagerTC
# 需要导入模块: from astroid.manager import AstroidManager [as 别名]
# 或者: from astroid.manager.AstroidManager import ast_from_class [as 别名]
#.........这里部分代码省略.........
if archive_path in sys.path_importer_cache:
del sys.path_importer_cache[archive_path]
sys.path = origpath
def test_ast_from_module_name_egg(self):
self._test_ast_from_zip('MyPyPa-0.1.0-py2.5.egg')
def test_ast_from_module_name_zip(self):
self._test_ast_from_zip('MyPyPa-0.1.0-py2.5.zip')
def test_zip_import_data(self):
"""check if zip_import_data works"""
filepath = self.datapath('MyPyPa-0.1.0-py2.5.zip/mypypa')
astroid = self.manager.zip_import_data(filepath)
self.assertEqual(astroid.name, 'mypypa')
def test_zip_import_data_without_zipimport(self):
"""check if zip_import_data return None without zipimport"""
self.assertEqual(self.manager.zip_import_data('path'), None)
def test_file_from_module(self):
"""check if the unittest filepath is equals to the result of the method"""
import unittest
if PY3K:
unittest_file = unittest.__file__
else:
unittest_file = unittest.__file__[:-1]
self.assertEqual(unittest_file,
self.manager.file_from_module_name('unittest', None))
def test_file_from_module_name_astro_building_exception(self):
"""check if the method launch a exception with a wrong module name"""
self.assertRaises(AstroidBuildingException, self.manager.file_from_module_name, 'unhandledModule', None)
def test_ast_from_module(self):
import unittest
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.pure_python, True)
import time
astroid = self.manager.ast_from_module(time)
self.assertEqual(astroid.pure_python, False)
def test_ast_from_module_cache(self):
"""check if the module is in the cache manager"""
import unittest
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.name, 'unittest')
self.assertIn('unittest', self.manager.astroid_cache)
def test_ast_from_class(self):
astroid = self.manager.ast_from_class(int)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def test_ast_from_class_with_module(self):
"""check if the method works with the module name"""
astroid = self.manager.ast_from_class(int, int.__module__)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object, object.__module__)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def test_ast_from_class_attr_error(self):
"""give a wrong class at the ast_from_class method"""
self.assertRaises(AstroidBuildingException, self.manager.ast_from_class, None)
def test_from_directory(self):
obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
self.assertEqual(obj.name, 'data')
self.assertEqual(obj.path, join(DATA, '__init__.py'))
def test_project_node(self):
obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
expected = set(['SSL1', '__init__', 'all', 'appl', 'format', 'module',
'module2', 'noendingnewline', 'nonregr', 'notall'])
expected = [
'data', 'data.SSL1', 'data.SSL1.Connection1',
'data.absimport', 'data.all',
'data.appl', 'data.appl.myConnection', 'data.email',
'data.find_test',
'data.find_test.module',
'data.find_test.module2',
'data.find_test.noendingnewline',
'data.find_test.nonregr',
'data.format',
'data.lmfp',
'data.lmfp.foo',
'data.module',
'data.module1abs', 'data.module1abs.core',
'data.module2', 'data.noendingnewline',
'data.nonregr', 'data.notall']
self.assertListEqual(sorted(k for k in list(obj.keys())), expected)
示例3: AstroidManagerTC
# 需要导入模块: from astroid.manager import AstroidManager [as 别名]
# 或者: from astroid.manager.AstroidManager import ast_from_class [as 别名]
class AstroidManagerTC(TestCase):
def setUp(self):
self.manager = AstroidManager()
self.manager.astroid_cache.clear()
def test_ast_from_module(self):
import unittest
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.pure_python, True)
import time
astroid = self.manager.ast_from_module(time)
self.assertEqual(astroid.pure_python, False)
def test_ast_from_class(self):
astroid = self.manager.ast_from_class(int)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def _test_ast_from_zip(self, archive):
origpath = sys.path[:]
sys.modules.pop('mypypa', None)
archive_path = join(DATA, archive)
sys.path.insert(0, archive_path)
try:
module = self.manager.ast_from_module_name('mypypa')
self.assertEqual(module.name, 'mypypa')
self.assertTrue(module.file.endswith('%s/mypypa' % archive),
module.file)
finally:
# remove the module, else after importing egg, we don't get the zip
if 'mypypa' in self.manager.astroid_cache:
del self.manager.astroid_cache['mypypa']
del self.manager._mod_file_cache[('mypypa', None)]
if archive_path in sys.path_importer_cache:
del sys.path_importer_cache[archive_path]
sys.path = origpath
def test_ast_from_module_name_egg(self):
self._test_ast_from_zip('MyPyPa-0.1.0-py2.5.egg')
def test_ast_from_module_name_zip(self):
self._test_ast_from_zip('MyPyPa-0.1.0-py2.5.zip')
def test_from_directory(self):
obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
self.assertEqual(obj.name, 'data')
self.assertEqual(obj.path, join(DATA, '__init__.py'))
def test_project_node(self):
obj = self.manager.project_from_files([DATA], _silent_no_wrap, 'data')
expected = set(['SSL1', '__init__', 'all', 'appl', 'format', 'module',
'module2', 'noendingnewline', 'nonregr', 'notall'])
expected = ['data', 'data.SSL1', 'data.SSL1.Connection1',
'data.absimport', 'data.all',
'data.appl', 'data.appl.myConnection', 'data.email', 'data.format',
'data.module', 'data.module2', 'data.noendingnewline',
'data.nonregr', 'data.notall']
self.assertListEqual(sorted(k for k in obj.keys()), expected)
def test_do_not_expose_main(self):
obj = self.manager.ast_from_module_name('__main__')
self.assertEqual(obj.name, '__main__')
self.assertEqual(obj.items(), [])
示例4: AstroidManagerTest
# 需要导入模块: from astroid.manager import AstroidManager [as 别名]
# 或者: from astroid.manager.AstroidManager import ast_from_class [as 别名]
#.........这里部分代码省略.........
astroid = self.manager.zip_import_data(filepath)
self.assertEqual(astroid.name, 'mypypa')
def test_zip_import_data_without_zipimport(self):
"""check if zip_import_data return None without zipimport"""
self.assertEqual(self.manager.zip_import_data('path'), None)
def test_file_from_module(self):
"""check if the unittest filepath is equals to the result of the method"""
if sys.version_info > (3, 0):
unittest_file = unittest.__file__
else:
unittest_file = unittest.__file__[:-1]
self.assertEqual(unittest_file,
self.manager.file_from_module_name('unittest', None)[0])
def test_file_from_module_name_astro_building_exception(self):
"""check if the method launch a exception with a wrong module name"""
self.assertRaises(AstroidBuildingException, self.manager.file_from_module_name, 'unhandledModule', None)
def test_ast_from_module(self):
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.pure_python, True)
import time
astroid = self.manager.ast_from_module(time)
self.assertEqual(astroid.pure_python, False)
def test_ast_from_module_cache(self):
"""check if the module is in the cache manager"""
astroid = self.manager.ast_from_module(unittest)
self.assertEqual(astroid.name, 'unittest')
self.assertIn('unittest', self.manager.astroid_cache)
def test_ast_from_class(self):
astroid = self.manager.ast_from_class(int)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def test_ast_from_class_with_module(self):
"""check if the method works with the module name"""
astroid = self.manager.ast_from_class(int, int.__module__)
self.assertEqual(astroid.name, 'int')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
astroid = self.manager.ast_from_class(object, object.__module__)
self.assertEqual(astroid.name, 'object')
self.assertEqual(astroid.parent.frame().name, BUILTINS)
self.assertIn('__setattr__', astroid)
def test_ast_from_class_attr_error(self):
"""give a wrong class at the ast_from_class method"""
self.assertRaises(AstroidBuildingException, self.manager.ast_from_class, None)
def test_from_directory(self):
self.assertEqual(self.project.name, 'data')
self.assertEqual(self.project.path,
os.path.abspath(resources.find('data/__init__.py')))
def test_project_node(self):
expected = [
'data',