本文整理汇总了Python中codecs.escape_encode方法的典型用法代码示例。如果您正苦于以下问题:Python codecs.escape_encode方法的具体用法?Python codecs.escape_encode怎么用?Python codecs.escape_encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类codecs
的用法示例。
在下文中一共展示了codecs.escape_encode方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_escape_encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
def test_escape_encode(self):
tests = [
(b'', (b'', 0)),
(b'foobar', (b'foobar', 6)),
(b'spam\0eggs', (b'spam\\x00eggs', 9)),
(b'a\'b', (b"a\\'b", 3)),
(b'b\\c', (b'b\\\\c', 3)),
(b'c\nd', (b'c\\nd', 3)),
(b'd\re', (b'd\\re', 3)),
(b'f\x7fg', (b'f\\x7fg', 3)),
]
for data, output in tests:
with self.subTest(data=data):
self.assertEqual(codecs.escape_encode(data), output)
self.assertRaises(TypeError, codecs.escape_encode, 'spam')
self.assertRaises(TypeError, codecs.escape_encode, bytearray(b'spam'))
示例2: asciiChar
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
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
示例3: asciiHex
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
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
示例4: unicodeChar
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
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
示例5: encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
def encode(self, input, final=False):
return codecs.escape_encode(input, self.errors)[0]
示例6: test_escape_encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
def test_escape_encode(self):
#sanity checks
value, length = codecs.escape_encode("abba")
self.assertEqual(value, "abba")
self.assertEqual(length, 4)
value, length = codecs.escape_encode("ab\a\b\t\n\r\f\vba")
self.assertEqual(value, 'ab\\x07\\x08\\t\\n\\r\\x0c\\x0bba')
if is_cli: #http://ironpython.codeplex.com/workitem/27899
self.assertEqual(length, 26)
else:
self.assertEqual(length, 11)
value, length = codecs.escape_encode("\\a")
self.assertEqual(value, "\\\\a")
if is_cli: #http://ironpython.codeplex.com/workitem/27899
self.assertEqual(length, 3)
else:
self.assertEqual(length, 2)
l = []
for i in xrange(256):
l.append(chr(i))
value, length = codecs.escape_encode(''.join(l))
self.assertEqual(value, '\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f !"#$%&\\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff')
if is_cli: #http://ironpython.codeplex.com/workitem/27899
self.assertEqual(length, 735)
else:
self.assertEqual(length, 256)
示例7: test_encode_length
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
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
示例8: test_escape_encode
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
def test_escape_encode(self):
#sanity checks
value, length = codecs.escape_encode(b"abba")
self.assertEqual(value, b"abba")
self.assertEqual(length, 4)
value, length = codecs.escape_encode(b"ab\a\b\t\n\r\f\vba")
self.assertEqual(value, b'ab\\x07\\x08\\t\\n\\r\\x0c\\x0bba')
self.assertEqual(length, 11)
value, length = codecs.escape_encode(b"\\a")
self.assertEqual(value, b"\\\\a")
self.assertEqual(length, 2)
value, length = codecs.escape_encode(bytes(range(256)))
self.assertEqual(value, b'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a\\x1b\\x1c\\x1d\\x1e\\x1f !"#$%&\\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd\\xfe\\xff')
self.assertEqual(length, 256)
self.assertRaises(TypeError, codecs.escape_encode, "abc")
self.assertRaises(TypeError, codecs.escape_encode, None)
self.assertRaises(TypeError, codecs.escape_encode, None, None)
self.assertEquals(codecs.escape_encode(b"\\", None), (b"\\\\", 1))
self.assertEquals(codecs.escape_encode(b"\\", 'strict'), (b"\\\\", 1))
self.assertRaises(TypeError, codecs.escape_encode, bytearray(b"abc"))
self.assertRaises(TypeError, codecs.escape_encode, memoryview(b"abc"))
self.assertRaises(TypeError, codecs.escape_encode, array.array('I', (1633771873,)))
示例9: build
# 需要导入模块: import codecs [as 别名]
# 或者: from codecs import escape_encode [as 别名]
def build(self, pre=None, shortest=False):
"""Build the ``Quote`` instance
:param list pre: The prerequisites list
:param bool shortest: Whether or not the shortest reference-chain (most minimal) version of the field should be generated.
"""
res = super(Q, self).build(pre, shortest=shortest)
if self.escape:
return self._repr_escape(res)
elif self.html_js_escape:
res, _ = codecs.escape_encode(res)
return (b"'" + res.replace(b"<", b"\\x3c").replace(b">", b"\\x3e") + b"'")
else:
return b"".join([self.quote, res, self.quote])