本文整理汇总了Python中future.utils.PY2属性的典型用法代码示例。如果您正苦于以下问题:Python utils.PY2属性的具体用法?Python utils.PY2怎么用?Python utils.PY2使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类future.utils
的用法示例。
在下文中一共展示了utils.PY2属性的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def __new__(cls, offset, name=_Omitted):
if not isinstance(offset, timedelta):
raise TypeError("offset must be a timedelta")
if name is cls._Omitted:
if not offset:
return cls.utc
name = None
elif not isinstance(name, str):
###
# For Python-Future:
if PY2 and isinstance(name, native_str):
name = name.decode()
else:
raise TypeError("name must be a string")
###
if not cls._minoffset <= offset <= cls._maxoffset:
raise ValueError("offset must be a timedelta"
" strictly between -timedelta(hours=24) and"
" timedelta(hours=24).")
if (offset.microseconds != 0 or
offset.seconds % 60 != 0):
raise ValueError("offset must be a timedelta"
" representing a whole number of minutes")
return cls._create(offset, name)
示例2: test_suspend_hooks
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def test_suspend_hooks(self):
"""
Code like the try/except block here appears in Pyflakes v0.6.1. This
method tests whether suspend_hooks() works as advertised.
"""
example_PY2_check = False
with standard_library.suspend_hooks():
# An example of fragile import code that we don't want to break:
try:
import builtins
except ImportError:
example_PY2_check = True
if utils.PY2:
self.assertTrue(example_PY2_check)
else:
self.assertFalse(example_PY2_check)
# The import should succeed again now:
import builtins
示例3: test_call_with_args_does_nothing
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def test_call_with_args_does_nothing(self):
if utils.PY2:
from __builtin__ import super as builtin_super
else:
from builtins import super as builtin_super
class Base(object):
def calc(self,value):
return 2 * value
class Sub1(Base):
def calc(self,value):
return 7 + super().calc(value)
class Sub2(Base):
def calc(self,value):
return super().calc(value) - 1
class Diamond(Sub1,Sub2):
def calc(self,value):
return 3 * super().calc(value)
for cls in (Base,Sub1,Sub2,Diamond,):
obj = cls()
self.assertSuperEquals(builtin_super(cls), super(cls))
self.assertSuperEquals(builtin_super(cls,obj), super(cls,obj))
示例4: test_startswith
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def test_startswith(self):
s = str('abcd')
self.assertTrue(s.startswith('a'))
self.assertTrue(s.startswith(('a', 'd')))
self.assertTrue(s.startswith(str('ab')))
if utils.PY2:
# We allow this, because e.g. Python 2 os.path.join concatenates
# its arg with a byte-string '/' indiscriminately.
self.assertFalse(s.startswith(b'A'))
self.assertTrue(s.startswith(b'a'))
with self.assertRaises(TypeError) as cm:
self.assertFalse(s.startswith(bytes(b'A')))
with self.assertRaises(TypeError) as cm:
s.startswith((bytes(b'A'), bytes(b'B')))
with self.assertRaises(TypeError) as cm:
s.startswith(65)
示例5: test_mul
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def test_mul(self):
s = str(u'ABC')
c = s * 4
self.assertTrue(isinstance(c, str))
self.assertEqual(c, u'ABCABCABCABC')
d = s * int(4)
self.assertTrue(isinstance(d, str))
self.assertEqual(d, u'ABCABCABCABC')
if utils.PY2:
e = s * long(4)
self.assertTrue(isinstance(e, str))
self.assertEqual(e, u'ABCABCABCABC')
with self.assertRaises(TypeError):
s * 3.3
with self.assertRaises(TypeError):
s * (3.3 + 3j)
示例6: test_rmul
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def test_rmul(self):
s = str(u'XYZ')
c = 3 * s
self.assertTrue(isinstance(c, str))
self.assertEqual(c, u'XYZXYZXYZ')
d = s * int(3)
self.assertTrue(isinstance(d, str))
self.assertEqual(d, u'XYZXYZXYZ')
if utils.PY2:
e = long(3) * s
self.assertTrue(isinstance(e, str))
self.assertEqual(e, u'XYZXYZXYZ')
with self.assertRaises(TypeError):
3.3 * s
with self.assertRaises(TypeError):
(3.3 + 3j) * s
示例7: _safe_readinto
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def _safe_readinto(self, b):
"""Same as _safe_read, but for reading into a buffer."""
total_bytes = 0
mvb = memoryview(b)
while total_bytes < len(b):
if MAXAMOUNT < len(mvb):
temp_mvb = mvb[0:MAXAMOUNT]
if PY2:
data = self.fp.read(len(temp_mvb))
n = len(data)
temp_mvb[:n] = data
else:
n = self.fp.readinto(temp_mvb)
else:
if PY2:
data = self.fp.read(len(mvb))
n = len(data)
mvb[:n] = data
else:
n = self.fp.readinto(mvb)
if not n:
raise IncompleteRead(bytes(mvb[0:total_bytes]), len(b))
mvb = mvb[n:]
total_bytes += n
return total_bytes
示例8: idle_cpu_count
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def idle_cpu_count(mincpu=1):
"""Estimate number of idle CPUs, for use by multiprocessing code
needing to determine how many processes can be run without excessive
load. This function uses :func:`os.getloadavg` which is only available
under a Unix OS.
Parameters
----------
mincpu : int
Minimum number of CPUs to report, independent of actual estimate
Returns
-------
idle : int
Estimate of number of idle CPUs
"""
if PY2:
ncpu = mp.cpu_count()
else:
ncpu = os.cpu_count()
idle = int(ncpu - np.floor(os.getloadavg()[0]))
return max(mincpu, idle)
示例9: idle_cpu_count
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def idle_cpu_count(mincpu=1):
"""Estimate number of idle CPUs.
Estimate number of idle CPUs, for use by multiprocessing code
needing to determine how many processes can be run without excessive
load. This function uses :func:`os.getloadavg` which is only available
under a Unix OS.
Parameters
----------
mincpu : int
Minimum number of CPUs to report, independent of actual estimate
Returns
-------
idle : int
Estimate of number of idle CPUs
"""
if PY2:
ncpu = mp.cpu_count()
else:
ncpu = os.cpu_count()
idle = int(ncpu - np.floor(os.getloadavg()[0]))
return max(mincpu, idle)
示例10: readinto
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def readinto(self, b):
if self.fp is None:
return 0
if self._method == "HEAD":
self._close_conn()
return 0
if self.chunked:
return self._readinto_chunked(b)
if self.length is not None:
if len(b) > self.length:
# clip the read to the "end of response"
b = memoryview(b)[0:self.length]
# we do not use _safe_read() here because this may be a .will_close
# connection, and the user is reading more bytes than will be provided
# (for example, reading in 1k chunks)
if PY2:
data = self.fp.read(len(b))
n = len(data)
b[:n] = data
else:
n = self.fp.readinto(b)
if not n and b:
# Ideally, we would raise IncompleteRead if the content-length
# wasn't satisfied, but it might break compatibility.
self._close_conn()
elif self.length is not None:
self.length -= n
if not self.length:
self._close_conn()
return n
示例11: __repr__
# 需要导入模块: from future import utils [as 别名]
# 或者: from future.utils import PY2 [as 别名]
def __repr__(self):
if PY2 and isinstance(self.value, unicode):
val = str(self.value) # make it a newstr to remove the u prefix
else:
val = self.value
return '<%s: %s=%s>' % (self.__class__.__name__,
str(self.key), repr(val))