当前位置: 首页>>代码示例>>Python>>正文


Python random.choose方法代码示例

本文整理汇总了Python中random.choose方法的典型用法代码示例。如果您正苦于以下问题:Python random.choose方法的具体用法?Python random.choose怎么用?Python random.choose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在random的用法示例。


在下文中一共展示了random.choose方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def __init__(self, *args, **kw):
        self.package_name = 'PACKAGE_'
        while self.package_name in sys.modules:
            self.package_name += random.choose(string.letters)
        self.module_name = self.package_name + '.foo'
        unittest.TestCase.__init__(self, *args, **kw) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:8,代码来源:test_pkgimport.py

示例2: test_package_import__semantics

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def test_package_import__semantics(self):

        # Generate a couple of broken modules to try importing.

        # ...try loading the module when there's a SyntaxError
        self.rewrite_file('for')
        try: __import__(self.module_name)
        except SyntaxError: pass
        else: raise RuntimeError, 'Failed to induce SyntaxError'
        self.assertNotIn(self.module_name, sys.modules)
        self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))

        # ...make up a variable name that isn't bound in __builtins__
        var = 'a'
        while var in dir(__builtins__):
            var += random.choose(string.letters)

        # ...make a module that just contains that
        self.rewrite_file(var)

        try: __import__(self.module_name)
        except NameError: pass
        else: raise RuntimeError, 'Failed to induce NameError.'

        # ...now  change  the module  so  that  the NameError  doesn't
        # happen
        self.rewrite_file('%s = 1' % var)
        module = __import__(self.module_name).foo
        self.assertEqual(getattr(module, var), 1) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:31,代码来源:test_pkgimport.py

示例3: __init__

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def __init__(self, *args, **kw):
        self.package_name = 'PACKAGE_'
        while self.package_name in sys.modules:
            self.package_name += random.choose(string.ascii_letters)
        self.module_name = self.package_name + '.foo'
        unittest.TestCase.__init__(self, *args, **kw) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:8,代码来源:test_pkgimport.py

示例4: test_package_import__semantics

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def test_package_import__semantics(self):

        # Generate a couple of broken modules to try importing.

        # ...try loading the module when there's a SyntaxError
        self.rewrite_file('for')
        try: __import__(self.module_name)
        except SyntaxError: pass
        else: raise RuntimeError('Failed to induce SyntaxError') # self.fail()?
        self.assertNotIn(self.module_name, sys.modules)
        self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))

        # ...make up a variable name that isn't bound in __builtins__
        var = 'a'
        while var in dir(__builtins__):
            var += random.choose(string.ascii_letters)

        # ...make a module that just contains that
        self.rewrite_file(var)

        try: __import__(self.module_name)
        except NameError: pass
        else: raise RuntimeError('Failed to induce NameError.')

        # ...now  change  the module  so  that  the NameError  doesn't
        # happen
        self.rewrite_file('%s = 1' % var)
        module = __import__(self.module_name).foo
        self.assertEqual(getattr(module, var), 1) 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:31,代码来源:test_pkgimport.py

示例5: windex

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def windex(lst):
	'''an attempt to make a random.choose() function that makes weighted choices
	
	accepts a list of tuples with the item and probability as a pair
	like: >>> x = [('one', 0.25), ('two', 0.25), ('three', 0.5)]
	>>> y=windex(x)'''
	n = random.uniform(0, 1)
	for item, weight in lst:
		if n < weight:
			break
		n = n - weight
	return item 
开发者ID:ActiveState,项目名称:code,代码行数:14,代码来源:recipe-117241.py

示例6: __init__

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def __init__(self, *args, **kw):
        self.package_name = 'PACKAGE_'
        while sys.modules.has_key(self.package_name):
            self.package_name += random.choose(string.letters)
        self.module_name = self.package_name + '.foo'
        unittest.TestCase.__init__(self, *args, **kw) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:8,代码来源:test_pkgimport.py

示例7: test_package_import__semantics

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def test_package_import__semantics(self):

        # Generate a couple of broken modules to try importing.

        # ...try loading the module when there's a SyntaxError
        self.rewrite_file('for')
        try: __import__(self.module_name)
        except SyntaxError: pass
        else: raise RuntimeError, 'Failed to induce SyntaxError'
        self.assert_(not sys.modules.has_key(self.module_name) and
                     not hasattr(sys.modules[self.package_name], 'foo'))

        # ...make up a variable name that isn't bound in __builtins__
        import __builtin__
        var = 'a'
        while var in dir(__builtin__):
            var += random.choose(string.letters)

        # ...make a module that just contains that
        self.rewrite_file(var)

        try: __import__(self.module_name)
        except NameError: pass
        else: raise RuntimeError, 'Failed to induce NameError.'

        # ...now  change  the module  so  that  the NameError  doesn't
        # happen
        self.rewrite_file('%s = 1' % var)
        module = __import__(self.module_name).foo
        self.assertEqual(getattr(module, var), 1) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:32,代码来源:test_pkgimport.py

示例8: test_package_import__semantics

# 需要导入模块: import random [as 别名]
# 或者: from random import choose [as 别名]
def test_package_import__semantics(self):

        # Generate a couple of broken modules to try importing.

        # ...try loading the module when there's a SyntaxError
        self.rewrite_file('for')
        try: __import__(self.module_name)
        except SyntaxError: pass
        else: raise RuntimeError, 'Failed to induce SyntaxError'
        self.assertTrue(self.module_name not in sys.modules)
        self.assertFalse(hasattr(sys.modules[self.package_name], 'foo'))

        # ...make up a variable name that isn't bound in __builtins__
        import __builtin__
        var = 'a'
        while var in dir(__builtin__):
            var += random.choose(string.letters)

        # ...make a module that just contains that
        self.rewrite_file(var)

        try: __import__(self.module_name)
        except NameError: pass
        else: raise RuntimeError, 'Failed to induce NameError.'

        # ...now  change  the module  so  that  the NameError  doesn't
        # happen
        self.rewrite_file('%s = 1' % var)
        module = __import__(self.module_name).foo
        self.assertEqual(getattr(module, var), 1) 
开发者ID:Acmesec,项目名称:CTFCrackTools-V2,代码行数:32,代码来源:test_pkgimport.py


注:本文中的random.choose方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。