本文整理汇总了Python中__builtin__.xrange方法的典型用法代码示例。如果您正苦于以下问题:Python __builtin__.xrange方法的具体用法?Python __builtin__.xrange怎么用?Python __builtin__.xrange使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类__builtin__
的用法示例。
在下文中一共展示了__builtin__.xrange方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xrange
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def xrange(*args):
"""Coconut uses Python 3 "range" instead of Python 2 "xrange"."""
raise _coconut.NameError('Coconut uses Python 3 "range" instead of Python 2 "xrange"')
示例2: test_init_only_two_tiles
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def test_init_only_two_tiles(self):
t = 0
for x in xrange(Board.SIZE):
for y in xrange(Board.SIZE):
c = self.b.cells[y][x]
if not c == 0:
t += 1
else:
self.assertEqual(c, 0, 'board[%d][%d] should be 0' % (y, x))
self.assertEqual(t, 2)
示例3: test_filled
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def test_filled(self):
self.b.cells = [[1]*Board.SIZE for _ in xrange(Board.SIZE)]
self.assertTrue(self.b.filled())
# == .addTile == #
示例4: test_getCol
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def test_getCol(self):
s = 4
b = Board(size=s)
l = [42, 17, 12, 3]
b.cells = [[l[i], 4, 1, 2] for i in xrange(s)]
self.assertSequenceEqual(b.getCol(0), l)
# == .setLine == #
示例5: setUp
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def setUp(self):
try:
self.xr = __builtin__.xrange
delattr(__builtin__, 'xrange')
except AttributeError:
self.xr = None
helpers.reload(board)
示例6: tearDown
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def tearDown(self):
__builtin__.xrange = self.xr
示例7: xrange
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def xrange(*args, **kwargs):
major_version = sys.version_info.major
if major_version == 2:
import __builtin__
return __builtin__.xrange(*args, **kwargs)
elif major_version >= 3:
import builtins
return builtins.range(*args, **kwargs)
else:
raise RuntimeError("Unsupported version of Python.")
示例8: test_pickle_python2_python3
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def test_pickle_python2_python3():
# Test that loading object arrays saved on Python 2 works both on
# Python 2 and Python 3 and vice versa
data_dir = os.path.join(os.path.dirname(__file__), 'data')
if sys.version_info[0] >= 3:
xrange = range
else:
import __builtin__
xrange = __builtin__.xrange
expected = np.array([None, xrange, u'\u512a\u826f',
b'\xe4\xb8\x8d\xe8\x89\xaf'],
dtype=object)
for fname in ['py2-objarr.npy', 'py2-objarr.npz',
'py3-objarr.npy', 'py3-objarr.npz']:
path = os.path.join(data_dir, fname)
for encoding in ['bytes', 'latin1']:
data_f = np.load(path, encoding=encoding)
if fname.endswith('.npz'):
data = data_f['x']
data_f.close()
else:
data = data_f
if sys.version_info[0] >= 3:
if encoding == 'latin1' and fname.startswith('py2'):
assert_(isinstance(data[3], str))
assert_array_equal(data[:-1], expected[:-1])
# mojibake occurs
assert_array_equal(data[-1].encode(encoding), expected[-1])
else:
assert_(isinstance(data[3], bytes))
assert_array_equal(data, expected)
else:
assert_array_equal(data, expected)
if sys.version_info[0] >= 3:
if fname.startswith('py2'):
if fname.endswith('.npz'):
data = np.load(path)
assert_raises(UnicodeError, data.__getitem__, 'x')
data.close()
data = np.load(path, fix_imports=False, encoding='latin1')
assert_raises(ImportError, data.__getitem__, 'x')
data.close()
else:
assert_raises(UnicodeError, np.load, path)
assert_raises(ImportError, np.load, path,
encoding='latin1', fix_imports=False)
示例9: test_pickle_python2_python3
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def test_pickle_python2_python3():
# Test that loading object arrays saved on Python 2 works both on
# Python 2 and Python 3 and vice versa
data_dir = os.path.join(os.path.dirname(__file__), 'data')
if sys.version_info[0] >= 3:
xrange = range
else:
import __builtin__
xrange = __builtin__.xrange
expected = np.array([None, xrange, u'\u512a\u826f',
b'\xe4\xb8\x8d\xe8\x89\xaf'],
dtype=object)
for fname in ['py2-objarr.npy', 'py2-objarr.npz',
'py3-objarr.npy', 'py3-objarr.npz']:
path = os.path.join(data_dir, fname)
for encoding in ['bytes', 'latin1']:
data_f = np.load(path, allow_pickle=True, encoding=encoding)
if fname.endswith('.npz'):
data = data_f['x']
data_f.close()
else:
data = data_f
if sys.version_info[0] >= 3:
if encoding == 'latin1' and fname.startswith('py2'):
assert_(isinstance(data[3], str))
assert_array_equal(data[:-1], expected[:-1])
# mojibake occurs
assert_array_equal(data[-1].encode(encoding), expected[-1])
else:
assert_(isinstance(data[3], bytes))
assert_array_equal(data, expected)
else:
assert_array_equal(data, expected)
if sys.version_info[0] >= 3:
if fname.startswith('py2'):
if fname.endswith('.npz'):
data = np.load(path, allow_pickle=True)
assert_raises(UnicodeError, data.__getitem__, 'x')
data.close()
data = np.load(path, allow_pickle=True, fix_imports=False,
encoding='latin1')
assert_raises(ImportError, data.__getitem__, 'x')
data.close()
else:
assert_raises(UnicodeError, np.load, path,
allow_pickle=True)
assert_raises(ImportError, np.load, path,
allow_pickle=True, fix_imports=False,
encoding='latin1')
示例10: iwait
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import xrange [as 别名]
def iwait(objects, timeout=None, count=None):
"""
Iteratively yield *objects* as they are ready, until all (or *count*) are ready
or *timeout* expired.
:param objects: A sequence (supporting :func:`len`) containing objects
implementing the wait protocol (rawlink() and unlink()).
:keyword int count: If not `None`, then a number specifying the maximum number
of objects to wait for. If ``None`` (the default), all objects
are waited for.
:keyword float timeout: If given, specifies a maximum number of seconds
to wait. If the timeout expires before the desired waited-for objects
are available, then this method returns immediately.
.. seealso:: :func:`wait`
.. versionchanged:: 1.1a1
Add the *count* parameter.
.. versionchanged:: 1.1a2
No longer raise :exc:`LoopExit` if our caller switches greenlets
in between items yielded by this function.
"""
# QQQ would be nice to support iterable here that can be generated slowly (why?)
if objects is None:
yield get_hub().join(timeout=timeout)
return
count = len(objects) if count is None else min(count, len(objects))
waiter = _MultipleWaiter()
switch = waiter.switch
if timeout is not None:
timer = get_hub().loop.timer(timeout, priority=-1)
timer.start(switch, _NONE)
try:
for obj in objects:
obj.rawlink(switch)
for _ in xrange(count):
item = waiter.get()
waiter.clear()
if item is _NONE:
return
yield item
finally:
if timeout is not None:
timer.stop()
for obj in objects:
unlink = getattr(obj, 'unlink', None)
if unlink:
try:
unlink(switch)
except:
traceback.print_exc()