本文整理汇总了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