我们从Python开源项目中,提取了以下27个代码示例,用于说明如何使用past.builtins.str()。
def chr(i): """ Return a byte-string of one character with ordinal i; 0 <= i <= 256 """ return oldstr(bytes((i,)))
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
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)
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))
def test_isinstance(self): s = b'abc' self.assertTrue(isinstance(s, basestring)) s2 = oldstr(b'abc') self.assertTrue(isinstance(s2, basestring))
def test_repr(self): s1 = oldstr(b'abc') self.assertEqual(repr(s1), "'abc'") s2 = oldstr(b'abc\ndef') self.assertEqual(repr(s2), "'abc\\ndef'")
def test_str(self): s1 = oldstr(b'abc') self.assertEqual(str(s1), 'abc') s2 = oldstr(b'abc\ndef') self.assertEqual(str(s2), 'abc\ndef')
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
def process_bind_param(self, value, dialect): if not value: return value if isinstance(value, oldstr): value = value.decode('utf-8') # TODO: Ensure NFC order. value = iri_to_uri(value) return value