本文整理匯總了Python中codecs.getencoder方法的典型用法代碼示例。如果您正苦於以下問題:Python codecs.getencoder方法的具體用法?Python codecs.getencoder怎麽用?Python codecs.getencoder使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類codecs
的用法示例。
在下文中一共展示了codecs.getencoder方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: header_check
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def header_check(self, content):
"""Special HTML encoding check."""
encode = None
# Look for meta charset
m = RE_HTML_ENCODE.search(content)
if m:
enc = m.group(1).decode('ascii')
try:
codecs.getencoder(enc)
encode = enc
except LookupError:
pass
else:
encode = self._has_xml_encode(content)
return encode
示例2: str_encode
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def str_encode(arr, encoding, errors="strict"):
"""
Encode character string in the Series/Index using indicated encoding.
Equivalent to :meth:`str.encode`.
Parameters
----------
encoding : str
errors : str, optional
Returns
-------
encoded : Series/Index of objects
"""
if encoding in _cpython_optimized_encoders:
# CPython optimized implementation
f = lambda x: x.encode(encoding, errors)
else:
encoder = codecs.getencoder(encoding)
f = lambda x: encoder(x, errors)[0]
return _na_map(f, arr)
示例3: _filter_encode
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def _filter_encode(self, data, encoding):
if its.py_v3 and isinstance(data, str):
data = data.encode('utf-8')
encoding = encoding.lower()
encoding = re.sub(r'^(base|rot)-(\d\d)$', r'\1\2', encoding)
if encoding == 'base16' or encoding == 'hex':
data = base64.b16encode(data)
elif encoding == 'base32':
data = base64.b32encode(data)
elif encoding == 'base64':
data = base64.b64encode(data)
elif encoding == 'rot13':
data = codecs.getencoder('rot-13')(data.decode('utf-8'))[0]
else:
raise ValueError('Unknown encoding type: ' + encoding)
if its.py_v3 and isinstance(data, bytes):
data = data.decode('utf-8')
return data
示例4: test_all
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def test_all(self):
api = (
"encode", "decode",
"register", "CodecInfo", "Codec", "IncrementalEncoder",
"IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
"getencoder", "getdecoder", "getincrementalencoder",
"getincrementaldecoder", "getreader", "getwriter",
"register_error", "lookup_error",
"strict_errors", "replace_errors", "ignore_errors",
"xmlcharrefreplace_errors", "backslashreplace_errors",
"open", "EncodedFile",
"iterencode", "iterdecode",
"BOM", "BOM_BE", "BOM_LE",
"BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
"BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
"BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", # Undocumented
"StreamReaderWriter", "StreamRecoder",
)
self.assertEqual(sorted(api), sorted(codecs.__all__))
for api in codecs.__all__:
getattr(codecs, api)
示例5: test_all
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def test_all(self):
api = (
"encode", "decode",
"register", "CodecInfo", "Codec", "IncrementalEncoder",
"IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
"getencoder", "getdecoder", "getincrementalencoder",
"getincrementaldecoder", "getreader", "getwriter",
"register_error", "lookup_error",
"strict_errors", "replace_errors", "ignore_errors",
"xmlcharrefreplace_errors", "backslashreplace_errors",
"namereplace_errors",
"open", "EncodedFile",
"iterencode", "iterdecode",
"BOM", "BOM_BE", "BOM_LE",
"BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
"BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
"BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", # Undocumented
"StreamReaderWriter", "StreamRecoder",
)
self.assertCountEqual(api, codecs.__all__)
for api in codecs.__all__:
getattr(codecs, api)
示例6: test_all
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def test_all(self):
api = (
"encode", "decode",
"register", "CodecInfo", "Codec", "IncrementalEncoder",
"IncrementalDecoder", "StreamReader", "StreamWriter", "lookup",
"getencoder", "getdecoder", "getincrementalencoder",
"getincrementaldecoder", "getreader", "getwriter",
"register_error", "lookup_error",
"strict_errors", "replace_errors", "ignore_errors",
"xmlcharrefreplace_errors", "backslashreplace_errors",
"open", "EncodedFile",
"iterencode", "iterdecode",
"BOM", "BOM_BE", "BOM_LE",
"BOM_UTF8", "BOM_UTF16", "BOM_UTF16_BE", "BOM_UTF16_LE",
"BOM_UTF32", "BOM_UTF32_BE", "BOM_UTF32_LE",
"BOM32_BE", "BOM32_LE", "BOM64_BE", "BOM64_LE", # Undocumented
"StreamReaderWriter", "StreamRecoder",
)
self.assertCountEqual(api, codecs.__all__)
for api in codecs.__all__:
getattr(codecs, api)
示例7: alpha_encode
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def alpha_encode(data: Any, metadata: 'XMetadata', field_metadata: 'XField') -> bytes:
"""Encode alpha or alphanumeric data.
metadata has encoding.
field_metadata is not used.
Mock metadata objects
>>> import types
>>> meta = types.SimpleNamespace(encoding='ebcdic')
>>> meta.encode = codecs.getencoder(meta.encoding)
>>> field_meta = types.SimpleNamespace(length=6)
>>> data = '98765-'
>>> actual = alpha_encode(data, meta, field_meta)
>>> repr(actual)
"b'\\\\xf9\\\\xf8\\\\xf7\\\\xf6\\\\xf5`'"
>>> actual == bytes([0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0x60])
True
"""
bytes, _ = metadata.encode("{:<{size}s}".format(data, size=field_metadata.length))
return bytes
# Encoder for numeric USAGE DISPLAY, trailing sign.
示例8: _bytes_to_hex
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def _bytes_to_hex(bs):
return codecs.getencoder('hex')(bs)[0]
示例9: _verify_encoding
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def _verify_encoding(self, enc):
"""Verify encoding is okay."""
enc = PYTHON_ENCODING_NAMES.get(enc, enc)
try:
codecs.getencoder(enc)
encoding = enc
except LookupError:
encoding = None
return encoding
示例10: function
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def function(self):
name = self.inferior_frame().name()
return codecs.getencoder('rot13')(name)[0]
# Create and register filter that uses it
示例11: bind_processor
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def bind_processor(self, dialect):
if self.convert_unicode or dialect.convert_unicode:
if dialect.supports_unicode_binds and \
self.convert_unicode != 'force':
if self._warn_on_bytestring:
def process(value):
if isinstance(value, util.binary_type):
util.warn_limited(
"Unicode type received non-unicode "
"bind param value %r.",
(util.ellipses_string(value),))
return value
return process
else:
return None
else:
encoder = codecs.getencoder(dialect.encoding)
warn_on_bytestring = self._warn_on_bytestring
def process(value):
if isinstance(value, util.text_type):
return encoder(value, self.unicode_error)[0]
elif warn_on_bytestring and value is not None:
util.warn_limited(
"Unicode type received non-unicode bind "
"param value %r.",
(util.ellipses_string(value),))
return value
return process
else:
return None
示例12: quote_value
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def quote_value(self, value):
# The backend "mostly works" without this function and there are use
# cases for compiling Python without the sqlite3 libraries (e.g.
# security hardening).
import _sqlite3
try:
value = _sqlite3.adapt(value)
except _sqlite3.ProgrammingError:
pass
# Manual emulation of SQLite parameter quoting
if isinstance(value, type(True)):
return str(int(value))
elif isinstance(value, (Decimal, float)):
return str(value)
elif isinstance(value, six.integer_types):
return str(value)
elif isinstance(value, six.string_types):
return "'%s'" % six.text_type(value).replace("\'", "\'\'")
elif value is None:
return "NULL"
elif isinstance(value, (bytes, bytearray, six.memoryview)):
# Bytes are only allowed for BLOB fields, encoded as string
# literals containing hexadecimal data and preceded by a single "X"
# character:
# value = b'\x01\x02' => value_hex = b'0102' => return X'0102'
value = bytes(value)
hex_encoder = codecs.getencoder('hex_codec')
value_hex, _length = hex_encoder(value)
# Use 'ascii' encoding for b'01' => '01', no need to use force_text here.
return "X'%s'" % value_hex.decode('ascii')
else:
raise ValueError("Cannot quote parameter value %r of type %s" % (value, type(value)))
示例13: test_encode_length
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def test_encode_length(self):
# Issue 3739
encoder = codecs.getencoder("unicode_internal")
self.assertEqual(encoder(u"a")[1], 1)
self.assertEqual(encoder(u"\xe9\u0142")[1], 2)
encoder = codecs.getencoder("string-escape")
self.assertEqual(encoder(r'\x00')[1], 4)
# From http://www.gnu.org/software/libidn/draft-josefsson-idn-test-vectors.html
示例14: test_getencoder
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def test_getencoder(self):
self.assertRaises(TypeError, codecs.getencoder)
self.assertRaises(LookupError, codecs.getencoder, "__spam__")
示例15: test_bad_encode_args
# 需要導入模塊: import codecs [as 別名]
# 或者: from codecs import getencoder [as 別名]
def test_bad_encode_args(self):
for encoding in all_unicode_encodings:
encoder = codecs.getencoder(encoding)
self.assertRaises(TypeError, encoder)