本文整理汇总了Python中types.ModuleType方法的典型用法代码示例。如果您正苦于以下问题:Python types.ModuleType方法的具体用法?Python types.ModuleType怎么用?Python types.ModuleType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types
的用法示例。
在下文中一共展示了types.ModuleType方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: iterate_module_attributes
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def iterate_module_attributes(module):
"""
Iterate over the attributes in a module.
This is a generator function.
Returns (name, value) tuples.
"""
for name in dir(module):
# Ignore fields marked as private or builtin.
if name.startswith('_'):
continue
value = getattr(module, name)
# Ignore callable objects (functions) and loaded modules.
if callable(value) or isinstance(value, types.ModuleType):
continue
yield (name, value)
示例2: read_table
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def read_table(self,module):
if isinstance(module,types.ModuleType):
parsetab = module
else:
if sys.version_info[0] < 3:
exec("import %s as parsetab" % module)
else:
env = { }
exec("import %s as parsetab" % module, env, env)
parsetab = env['parsetab']
if parsetab._tabversion != __tabversion__:
raise VersionError("yacc table file version is out of date")
self.lr_action = parsetab._lr_action
self.lr_goto = parsetab._lr_goto
self.lr_productions = []
for p in parsetab._lr_productions:
self.lr_productions.append(MiniProduction(*p))
self.lr_method = parsetab._lr_method
return parsetab._lr_signature
示例3: test_iter_builders_side_effect
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_iter_builders_side_effect(self):
# inject dummy module and add cleanup
mod = ModuleType('calmjs_testing_dummy')
mod.complete = generic_builder
self.addCleanup(sys.modules.pop, 'calmjs_testing_dummy')
sys.modules['calmjs_testing_dummy'] = mod
working_dir = utils.mkdtemp(self)
utils.make_dummy_dist(self, (
('entry_points.txt', '\n'.join([
'[calmjs.artifacts]',
'artifact.js = calmjs_testing_dummy:complete',
])),
), 'app', '1.0', working_dir=working_dir)
mock_ws = WorkingSet([working_dir])
registry = ArtifactRegistry('calmjs.artifacts', _working_set=mock_ws)
registry.update_artifact_metadata('app', {})
root = join(working_dir, 'app-1.0.egg-info', 'calmjs_artifacts')
self.assertFalse(exists(root))
ep, toolchain, spec = next(registry.iter_builders_for('app'))
self.assertFalse(exists(root))
# directory only created after the toolchain is executed
toolchain(spec)
self.assertTrue(exists(root))
示例4: test_get_modpath_pkg_resources_missing_path
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_get_modpath_pkg_resources_missing_path(self):
with pretty_logging(stream=StringIO()) as fd:
self.assertEqual([], indexer.modpath_pkg_resources(
None, calmjs_ep))
self.assertIn(
"None does not appear to be a valid module", fd.getvalue())
with pretty_logging(stream=StringIO()) as fd:
module = ModuleType('nothing')
self.assertEqual([], indexer.modpath_pkg_resources(
module, calmjs_ep))
err = fd.getvalue()
self.assertIn(
"module 'nothing' and entry_point 'demo = demo'", err)
# the input is fetched using a working entry_point, after all
self.assertIn("resource path resolved to be '" + calmjs_dist_dir, err)
self.assertIn("but it does not exist", fd.getvalue())
示例5: from_pyfile
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def from_pyfile(self, filename):
"""Update the values in the config from a Python file.
Only the uppercase variables in that module are stored in the config.
:param filename: an absolute path to the config file
"""
module = types.ModuleType("config")
module.__file__ = filename
try:
with open(filename) as config_file:
exec( # nosec
compile(config_file.read(), filename, "exec"),
module.__dict__,
)
except IOError as e:
e.strerror = "Unable to load configuration file (%s)" % e.strerror
raise
except Exception as e:
raise PyFileError(filename) from e
self.from_object(module)
return True
示例6: test_loadTestsFromModule__TestCase_subclass
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromModule__TestCase_subclass(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
def test(self):
pass
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)
expected = [loader.suiteClass([MyTestCase('test')])]
self.assertEqual(list(suite), expected)
# "This method searches `module` for classes derived from TestCase"
#
# What happens if no tests are found (no TestCase instances)?
示例7: test_loadTestsFromModule__no_TestCase_tests
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromModule__no_TestCase_tests(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
pass
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [loader.suiteClass()])
# "This method searches `module` for classes derived from TestCase"s
#
# What happens if loadTestsFromModule() is given something other
# than a module?
#
# XXX Currently, it succeeds anyway. This flexibility
# should either be documented or loadTestsFromModule() should
# raise a TypeError
#
# XXX Certain people are using this behaviour. We'll add a test for it
示例8: test_loadTestsFromModule__load_tests
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromModule__load_tests(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
def test(self):
pass
m.testcase_1 = MyTestCase
load_tests_args = []
def load_tests(loader, tests, pattern):
self.assertIsInstance(tests, unittest.TestSuite)
load_tests_args.extend((loader, tests, pattern))
return tests
m.load_tests = load_tests
loader = unittest.TestLoader()
suite = loader.loadTestsFromModule(m)
self.assertIsInstance(suite, unittest.TestSuite)
self.assertEqual(load_tests_args, [loader, suite, None])
load_tests_args = []
suite = loader.loadTestsFromModule(m, use_load_tests=False)
self.assertEqual(load_tests_args, [])
示例9: test_loadTestsFromName__relative_TestCase_subclass
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__relative_TestCase_subclass(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
def test(self):
pass
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
suite = loader.loadTestsFromName('testcase_1', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
示例10: test_loadTestsFromName__relative_testmethod
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__relative_testmethod(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
def test(self):
pass
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
suite = loader.loadTestsFromName('testcase_1.test', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve either to
# a module, a test case class, a TestSuite instance, a test method
# within a test case class, or a callable object which returns a
# TestCase or TestSuite instance."
#
# Does loadTestsFromName() raise the proper exception when trying to
# resolve "a test method within a test case class" that doesn't exist
# for the given name (relative to a provided module)?
示例11: test_loadTestsFromName__relative_invalid_testmethod
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__relative_invalid_testmethod(self):
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
def test(self):
pass
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
try:
loader.loadTestsFromName('testcase_1.testfoo', m)
except AttributeError as e:
self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'")
else:
self.fail("Failed to raise AttributeError")
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a ... TestSuite instance"
示例12: test_loadTestsFromName__callable__TestCase_instance
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__callable__TestCase_instance(self):
m = types.ModuleType('m')
testcase_1 = unittest.FunctionTestCase(lambda: None)
def return_TestCase():
return testcase_1
m.return_TestCase = return_TestCase
loader = unittest.TestLoader()
suite = loader.loadTestsFromName('return_TestCase', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [testcase_1])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase ... instance"
#*****************************************************************
#Override the suiteClass attribute to ensure that the suiteClass
#attribute is used
示例13: test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
class SubTestSuite(unittest.TestSuite):
pass
m = types.ModuleType('m')
testcase_1 = unittest.FunctionTestCase(lambda: None)
def return_TestCase():
return testcase_1
m.return_TestCase = return_TestCase
loader = unittest.TestLoader()
loader.suiteClass = SubTestSuite
suite = loader.loadTestsFromName('return_TestCase', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [testcase_1])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a test method within a test case class"
#*****************************************************************
#Override the suiteClass attribute to ensure that the suiteClass
#attribute is used
示例14: test_loadTestsFromName__relative_testmethod_ProperSuiteClass
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
class SubTestSuite(unittest.TestSuite):
pass
m = types.ModuleType('m')
class MyTestCase(unittest.TestCase):
def test(self):
pass
m.testcase_1 = MyTestCase
loader = unittest.TestLoader()
loader.suiteClass=SubTestSuite
suite = loader.loadTestsFromName('testcase_1.test', m)
self.assertIsInstance(suite, loader.suiteClass)
self.assertEqual(list(suite), [MyTestCase('test')])
# "The specifier name is a ``dotted name'' that may resolve ... to
# ... a callable object which returns a TestCase or TestSuite instance"
#
# What happens if the callable returns something else?
示例15: test_loadTestsFromName__callable__wrong_type
# 需要导入模块: import types [as 别名]
# 或者: from types import ModuleType [as 别名]
def test_loadTestsFromName__callable__wrong_type(self):
m = types.ModuleType('m')
def return_wrong():
return 6
m.return_wrong = return_wrong
loader = unittest.TestLoader()
try:
suite = loader.loadTestsFromName('return_wrong', m)
except TypeError:
pass
else:
self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
# "The specifier can refer to modules and packages which have not been
# imported; they will be imported as a side-effect"