本文整理汇总了Python中__builtin__.str方法的典型用法代码示例。如果您正苦于以下问题:Python __builtin__.str方法的具体用法?Python __builtin__.str怎么用?Python __builtin__.str使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类__builtin__
的用法示例。
在下文中一共展示了__builtin__.str方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __new__
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def __new__(mcls, enum_type, *args, **kwds):
"""
Creates an ArsdkBitfield type from its associated enum type
"""
if ArsdkBitfieldMeta._base is None:
cls = type.__new__(
ArsdkBitfieldMeta, builtin_str("ArsdkBitfield"), *args, **kwds)
mcls._base = cls
else:
cls = mcls._classes.get(enum_type)
if cls is not None:
return cls
cls = type.__new__(
mcls,
builtin_str(enum_type.__name__ + "_Bitfield"),
(mcls._base,),
dict(_enum_type_=enum_type))
mcls._classes[enum_type] = cls
return cls
示例2: _decode_line
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def _decode_line(line, encoding=None):
"""Decode bytes from binary input streams.
Defaults to decoding from 'latin1'. That differs from the behavior of
np.compat.asunicode that decodes from 'ascii'.
Parameters
----------
line : str or bytes
Line to be decoded.
Returns
-------
decoded_line : unicode
Unicode in Python 2, a str (unicode) in Python 3.
"""
if type(line) is bytes:
if encoding is None:
line = line.decode('latin1')
else:
line = line.decode(encoding)
return line
示例3: __init__
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def __init__(self, jss):
"""Initialize a Casper object.
Args:
jss: A JSS object to request the casper page from.
"""
self.jss = jss
self.url = "%s/casper.jxml" % self.jss.base_url
# This may be incorrect, but the assumption here is that this
# request wants the auth information as urlencoded data; and
# urlencode needs bytes.
# TODO: If we can just pass in bytes rather than
# urlencoded-bytes, we can remove this and let the request
# adapter handle the outgoing data encoding.
user = text(self.jss.user)
password = text(self.jss.password)
self.auth = urlencode(
{"username": user, "password": password})
super(Casper, self).__init__("Casper")
self.update()
示例4: str2bool
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def str2bool(value):
"""
Tries to transform a string supposed to represent a boolean to a boolean.
Parameters
----------
value : str
The string that is transformed to a boolean.
Returns
-------
boolval : bool
The boolean representation of `value`.
Raises
------
ValueError
If the string is not 'True' or 'False' (case independent)
Examples
--------
>>> np.lib._iotools.str2bool('TRUE')
True
>>> np.lib._iotools.str2bool('false')
False
"""
value = value.upper()
if value == 'TRUE':
return True
elif value == 'FALSE':
return False
else:
raise ValueError("Invalid boolean")
示例5: issubsctype
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def issubsctype(arg1, arg2):
"""
Determine if the first argument is a subclass of the second argument.
Parameters
----------
arg1, arg2 : dtype or dtype specifier
Data-types.
Returns
-------
out : bool
The result.
See Also
--------
issctype, issubdtype,obj2sctype
Examples
--------
>>> np.issubsctype('S8', str)
True
>>> np.issubsctype(np.array([1]), int)
True
>>> np.issubsctype(np.array([1]), float)
False
"""
return issubclass(obj2sctype(arg1), obj2sctype(arg2))
示例6: write
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def write(self, x):
old_f.write(x.replace("\n", " [%s]\n" % str(datetime.now())))
示例7: underline_print
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def underline_print(string):
under_str = "\033[4m%s\033[0m" % (str(string),)
return under_str
示例8: str2bool
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def str2bool(value):
"""
Tries to transform a string supposed to represent a boolean to a boolean.
Parameters
----------
value : str
The string that is transformed to a boolean.
Returns
-------
boolval : bool
The boolean representation of `value`.
Raises
------
ValueError
If the string is not 'True' or 'False' (case independent)
Examples
--------
>>> np.lib._iotools.str2bool('TRUE')
True
>>> np.lib._iotools.str2bool('false')
False
"""
value = value.upper()
if value == asbytes('TRUE'):
return True
elif value == asbytes('FALSE'):
return False
else:
raise ValueError("Invalid boolean")
示例9: setUp
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def setUp(self):
self.s = TEST_UNICODE_STR
self.s2 = str(self.s)
self.b = b'ABCDEFG'
self.b2 = bytes(self.b)
示例10: test_native_str
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def test_native_str(self):
"""
Tests whether native_str is really equal to the platform str.
"""
if PY2:
import __builtin__
builtin_str = __builtin__.str
else:
import builtins
builtin_str = builtins.str
inputs = [b'blah', u'blah', 'blah']
for s in inputs:
self.assertEqual(native_str(s), builtin_str(s))
self.assertTrue(isinstance(native_str(s), builtin_str))
示例11: test_native
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def test_native(self):
a = int(10**20) # long int
b = native(a)
self.assertEqual(a, b)
if PY2:
self.assertEqual(type(b), long)
else:
self.assertEqual(type(b), int)
c = bytes(b'ABC')
d = native(c)
self.assertEqual(c, d)
if PY2:
self.assertEqual(type(d), type(b'Py2 byte-string'))
else:
self.assertEqual(type(d), bytes)
s = str(u'ABC')
t = native(s)
self.assertEqual(s, t)
if PY2:
self.assertEqual(type(t), unicode)
else:
self.assertEqual(type(t), str)
d1 = dict({'a': 1, 'b': 2})
d2 = native(d1)
self.assertEqual(d1, d2)
self.assertEqual(type(d2), type({}))
示例12: test_raise_
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def test_raise_(self):
"""
The with_value() test currently fails on Py3
"""
def valerror():
try:
raise ValueError("Apples!")
except Exception as e:
raise_(e)
self.assertRaises(ValueError, valerror)
def with_value():
raise_(IOError, "This is an error")
self.assertRaises(IOError, with_value)
try:
with_value()
except IOError as e:
self.assertEqual(str(e), "This is an error")
def with_traceback():
try:
raise ValueError("An error")
except Exception as e:
_, _, traceback = sys.exc_info()
raise_(IOError, str(e), traceback)
self.assertRaises(IOError, with_traceback)
try:
with_traceback()
except IOError as e:
self.assertEqual(str(e), "An error")
示例13: test_ensure_new_type
# 需要导入模块: import __builtin__ [as 别名]
# 或者: from __builtin__ import str [as 别名]
def test_ensure_new_type(self):
s = u'abcd'
s2 = str(s)
self.assertEqual(ensure_new_type(s), s2)
self.assertEqual(type(ensure_new_type(s)), str)
b = b'xyz'
b2 = bytes(b)
self.assertEqual(ensure_new_type(b), b2)
self.assertEqual(type(ensure_new_type(b)), bytes)
i = 10000000000000
i2 = int(i)
self.assertEqual(ensure_new_type(i), i2)
self.assertEqual(type(ensure_new_type(i)), int)