本文整理匯總了Python中past.builtins.str方法的典型用法代碼示例。如果您正苦於以下問題:Python builtins.str方法的具體用法?Python builtins.str怎麽用?Python builtins.str使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類past.builtins
的用法示例。
在下文中一共展示了builtins.str方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: type_inherits_of_type
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def type_inherits_of_type(inheriting_type, base_type):
"""Checks whether inheriting_type inherits from base_type
:param str inheriting_type:
:param str base_type:
:return: True is base_type is base of inheriting_type
"""
assert isinstance(inheriting_type, type) or isclass(inheriting_type)
assert isinstance(base_type, type) or isclass(base_type)
if inheriting_type == base_type:
return True
else:
if len(inheriting_type.__bases__) != 1:
return False
return type_inherits_of_type(inheriting_type.__bases__[0], base_type)
示例2: chr
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def chr(i):
"""
Return a byte-string of one character with ordinal i; 0 <= i <= 256
"""
return oldstr(bytes((i,)))
示例3: transform
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def transform(self, node, results):
if node.type == token.STRING:
touch_import_top(u'past.types', u'oldstr', node)
if _literal_re.match(node.value):
new = node.clone()
# Strip any leading space or comments:
# TODO: check: do we really want to do this?
new.prefix = u''
new.value = u'b' + new.value
wrapped = wrap_in_fn_call("oldstr", [new], prefix=node.prefix)
return wrapped
示例4: transform
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def transform(self, node, results):
import pdb
pdb.set_trace()
if node.type == token.STRING:
touch_import_top(u'past.types', u'oldstr', node)
if _literal_re.match(node.value):
new = node.clone()
# Strip any leading space or comments:
# TODO: check: do we really want to do this?
new.prefix = u''
new.value = u'b' + new.value
wrapped = wrap_in_fn_call("oldstr", [new], prefix=node.prefix)
return wrapped
示例5: test_import_builtin_types
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def test_import_builtin_types(self):
code = """
s1 = 'abcd'
s2 = u'abcd'
b1 = b'abcd'
b2 = s2.encode('utf-8')
d1 = {}
d2 = dict((i, i**2) for i in range(10))
i1 = 1923482349324234L
i2 = 1923482349324234
"""
module = self.write_and_import(code, 'test_builtin_types')
self.assertTrue(isinstance(module.s1, oldstr))
self.assertTrue(isinstance(module.s2, unicode))
self.assertTrue(isinstance(module.b1, oldstr))
示例6: test_repr
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def test_repr(self):
s1 = oldstr(b'abc')
self.assertEqual(repr(s1), "'abc'")
s2 = oldstr(b'abc\ndef')
self.assertEqual(repr(s2), "'abc\\ndef'")
示例7: test_str
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def test_str(self):
s1 = oldstr(b'abc')
self.assertEqual(str(s1), 'abc')
s2 = oldstr(b'abc\ndef')
self.assertEqual(str(s2), 'abc\ndef')
示例8: test_isinstance
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def test_isinstance(self):
s = b'abc'
self.assertTrue(isinstance(s, basestring))
s2 = oldstr(b'abc')
self.assertTrue(isinstance(s2, basestring))
示例9: test_open
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def test_open(self):
"""
In conservative mode, futurize would not import io.open because
this changes the default return type from bytes to text.
"""
before = """
filename = 'temp_file_open.test'
contents = 'Temporary file contents. Delete me.'
with open(filename, 'w') as f:
f.write(contents)
with open(filename, 'r') as f:
data = f.read()
assert isinstance(data, str)
assert data == contents
"""
after = """
from past.builtins import open, str as oldbytes, unicode
filename = oldbytes(b'temp_file_open.test')
contents = oldbytes(b'Temporary file contents. Delete me.')
with open(filename, oldbytes(b'w')) as f:
f.write(contents)
with open(filename, oldbytes(b'r')) as f:
data = f.read()
assert isinstance(data, oldbytes)
assert data == contents
assert isinstance(oldbytes(b'hello'), basestring)
assert isinstance(unicode(u'hello'), basestring)
assert isinstance(oldbytes(b'hello'), basestring)
"""
self.convert_check(before, after, conservative=True)
示例10: convert_string_to_type
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def convert_string_to_type(string_value):
"""Converts a string into a type or class
:param string_value: the string to be converted, e.g. "int"
:return: The type derived from string_value, e.g. int
"""
# If the parameter is already a type, return it
if string_value in ['None', type(None).__name__]:
return type(None)
if isinstance(string_value, type) or isclass(string_value):
return string_value
# Get object associated with string
# First check whether we are having a built in type (int, str, etc)
if sys.version_info >= (3,):
import builtins as builtins23
else:
import __builtin__ as builtins23
if hasattr(builtins23, string_value):
obj = getattr(builtins23, string_value)
if type(obj) is type:
return obj
# If not, try to locate the module
try:
obj = locate(string_value)
except ErrorDuringImport as e:
raise ValueError("Unknown type '{0}'".format(e))
# Check whether object is a type
if type(obj) is type:
return locate(string_value)
# Check whether object is a class
if isclass(obj):
return obj
# Raise error if none is the case
raise ValueError("Unknown type '{0}'".format(string_value))
示例11: convert_string_value_to_type_value
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def convert_string_value_to_type_value(string_value, data_type):
"""Helper function to convert a given string to a given data type
:param str string_value: the string to convert
:param type data_type: the target data type
:return: the converted value
"""
from ast import literal_eval
try:
if data_type in (str, type(None)):
converted_value = str(string_value)
elif data_type == int:
converted_value = int(string_value)
elif data_type == float:
converted_value = float(string_value)
elif data_type == bool:
converted_value = bool(literal_eval(string_value))
elif data_type in (list, dict, tuple):
converted_value = literal_eval(string_value)
if type(converted_value) != data_type:
raise ValueError("Invalid syntax: {0}".format(string_value))
elif data_type == object:
try:
converted_value = literal_eval(string_value)
except (ValueError, SyntaxError):
converted_value = literal_eval('"' + string_value + '"')
elif isinstance(data_type, type): # Try native type conversion
converted_value = data_type(string_value)
elif isclass(data_type): # Call class constructor
converted_value = data_type(string_value)
else:
raise ValueError("No conversion from string '{0}' to data type '{0}' defined".format(
string_value, data_type.__name__))
except (ValueError, SyntaxError, TypeError) as e:
raise AttributeError("Can't convert '{0}' to type '{1}': {2}".format(string_value, data_type.__name__, e))
return converted_value
示例12: compat_oldstr
# 需要導入模塊: from past import builtins [as 別名]
# 或者: from past.builtins import str [as 別名]
def compat_oldstr(s):
if USING_PYTHON2:
return oldstr(s)
else:
return s.decode('utf-8') if isinstance(s, bytes) else s