我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用codecs.escape_encode()。
def writer(self): """loop forever and copy socket->serial""" while self.alive: try: data = self.socket.recv(1024) if not data: break if self.ser_newline and self.net_newline: # do the newline conversion # XXX fails for CR+LF in input when it is cut in half at the begin or end of the string data = ser_newline.join(data.split(net_newline)) self.serial.write(data) # get a bunch of bytes and send them # the spy shows what's on the serial port, so log it after converting newlines if self.spy: sys.stdout.write(codecs.escape_encode(data)[0]) sys.stdout.flush() except socket.error, msg: sys.stderr.write('ERROR: %s\n' % msg) # probably got disconnected break self.alive = False self.thread_read.join()
def _escape_bytes(val): """ If val is pure ascii, returns it as a str(), otherwise escapes into a sequence of escaped bytes: b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6' note: the obvious "v.decode('unicode-escape')" will return valid utf-8 unicode if it finds them in the string, but we want to return escaped bytes for any byte, even if they match a utf-8 string. """ # source: http://goo.gl/bGsnwC import codecs encoded_bytes, _ = codecs.escape_encode(val) return encoded_bytes.decode('ascii')
def encode(self, input, final=False): return codecs.escape_encode(input, self.errors)[0]
def _escape_strings(val): """If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes: b'\xc3\xb4\xc5\xd6' -> u'\\xc3\\xb4\\xc5\\xd6' and escapes unicode objects into a sequence of escaped unicode ids, e.g.: '4\\nV\\U00043efa\\x0eMXWB\\x1e\\u3028\\u15fd\\xcd\\U0007d944' note: the obvious "v.decode('unicode-escape')" will return valid utf-8 unicode if it finds them in bytes, but we want to return escaped bytes for any byte, even if they match a utf-8 string. """ if isinstance(val, bytes): if val: # source: http://goo.gl/bGsnwC encoded_bytes, _ = codecs.escape_encode(val) return encoded_bytes.decode('ascii') else: # empty bytes crashes codecs.escape_encode (#1087) return '' else: return val.encode('unicode_escape').decode('ascii')
def test_encode_length(self): # Issue 3739 encoder = codecs.getencoder("unicode_internal") self.assertEqual(encoder("a")[1], 1) self.assertEqual(encoder("\xe9\u0142")[1], 2) self.assertEqual(codecs.escape_encode(br'\x00')[1], 4) # From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
def test_encode_length(self): with support.check_warnings(('unicode_internal codec has been ' 'deprecated', DeprecationWarning)): # Issue 3739 encoder = codecs.getencoder("unicode_internal") self.assertEqual(encoder("a")[1], 1) self.assertEqual(encoder("\xe9\u0142")[1], 2) self.assertEqual(codecs.escape_encode(br'\x00')[1], 4) # From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
def asciiChar(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for c in range(0, 256): asciiChar = chr(c) additionalInfo = 'ASCII [Code: {} - Char: {}]'.format(c, codecs.escape_encode(asciiChar)) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiChar=asciiChar),additionalInfo) result.append(HTTPRequestHolderObjTemp) return result
def asciiHex(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for c in range(0, 256): asciiChar = chr(c) asciiHex = "{:02x}".format(c) additionalInfo = 'ASCII [Code: {} - Char: {} - Hex: {}]'.format(c, codecs.escape_encode(asciiChar), asciiHex) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiHex=asciiHex),additionalInfo) result.append(HTTPRequestHolderObjTemp) return result
def unicodeChar(HTTPRequestHolderObjs): result = [] for HTTPRequestHolderObj in HTTPRequestHolderObjs: initHTTPReq = HTTPRequestHolderObj.rawHTTPRequest initAdditionalInfo = HTTPRequestHolderObj.additionalInfo for u in range(0, 65535): unicodeChar = int2bytes(u) additionalInfo = 'UNICODE [Code: {} - Char: {}]'.format(u, codecs.escape_encode(unicodeChar)) if initAdditionalInfo != '': additionalInfo = initAdditionalInfo + ' - ' + additionalInfo HTTPRequestHolderObjTemp = HTTPRequestHolder.HTTPRequestHolder(Template(initHTTPReq).safe_substitute(asciiChar=asciiChar),additionalInfo) result.append(HTTPRequestHolderObjTemp) return result