Python codecs.getencoder() Examples
The following are 30
code examples of codecs.getencoder().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
codecs
, or try the search function
.
Example #1
Source File: test_codecs.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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)
Example #2
Source File: html.py From pyspelling with MIT License | 6 votes |
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
Example #3
Source File: strings.py From recruit with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: ch10_ex5.py From Mastering-Object-Oriented-Python-Second-Edition with MIT License | 6 votes |
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.
Example #5
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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)
Example #7
Source File: templates.py From king-phisher with BSD 3-Clause "New" or "Revised" License | 6 votes |
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
Example #8
Source File: strings.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: strings.py From vnpy_crypto with MIT License | 6 votes |
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)
Example #10
Source File: test_codecs.py From ironpython2 with Apache License 2.0 | 6 votes |
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)
Example #11
Source File: strings.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
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)
Example #12
Source File: sqltypes.py From pyRevit with GNU General Public License v3.0 | 5 votes |
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
Example #13
Source File: sqltypes.py From stdm with GNU General Public License v2.0 | 5 votes |
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("Unicode type received non-unicode" "bind param 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("Unicode type received non-unicode bind " "param value") return value return process else: return None
Example #14
Source File: utils.py From pdf-quench with GNU General Public License v2.0 | 5 votes |
def hexencode(b): if sys.version_info[0] < 3: return b.encode('hex') else: import codecs coder = codecs.getencoder('hex_codec') return coder(b)[0]
Example #15
Source File: http.py From instagram_private_api with MIT License | 5 votes |
def iter(self, fields, files): """ :param fields: sequence of (name, value) elements for regular form fields :param files: sequence of (name, filename, contenttype, filedata) elements for data to be uploaded as files :return: """ encoder = codecs.getencoder('utf-8') for (key, value) in fields: key = self.u(key) yield encoder('--{}\r\n'.format(self.boundary)) yield encoder(self.u('Content-Disposition: form-data; name="{}"\r\n').format(key)) yield encoder('\r\n') if isinstance(value, (int, float)): value = str(value) yield encoder(self.u(value)) yield encoder('\r\n') for (key, filename, contenttype, fd) in files: key = self.u(key) filename = self.u(filename) yield encoder('--{}\r\n'.format(self.boundary)) yield encoder(self.u('Content-Disposition: form-data; name="{}"; filename="{}"\r\n').format(key, filename)) yield encoder('Content-Type: {}\r\n'.format( contenttype or mimetypes.guess_type(filename)[0] or 'application/octet-stream')) yield encoder('Content-Transfer-Encoding: binary\r\n') yield encoder('\r\n') yield (fd, len(fd)) yield encoder('\r\n') yield encoder('--{}--\r\n'.format(self.boundary))
Example #16
Source File: http.py From instagram_private_api with MIT License | 5 votes |
def iter(self, fields, files): """ :param fields: sequence of (name, value) elements for regular form fields :param files: sequence of (name, filename, contenttype, filedata) elements for data to be uploaded as files :return: """ encoder = codecs.getencoder('utf-8') for (key, value) in fields: key = self.u(key) yield encoder('--{}\r\n'.format(self.boundary)) yield encoder(self.u('Content-Disposition: form-data; name="{}"\r\n').format(key)) yield encoder('\r\n') if isinstance(value, (int, float)): value = str(value) yield encoder(self.u(value)) yield encoder('\r\n') for (key, filename, contenttype, fd) in files: key = self.u(key) filename = self.u(filename) yield encoder('--{}\r\n'.format(self.boundary)) yield encoder(self.u('Content-Disposition: form-data; name="{}"; filename="{}"\r\n').format(key, filename)) yield encoder('Content-Type: {}\r\n'.format( contenttype or mimetypes.guess_type(filename)[0] or 'application/octet-stream')) yield encoder('Content-Transfer-Encoding: binary\r\n') yield encoder('\r\n') yield (fd, len(fd)) yield encoder('\r\n') yield encoder('--{}--\r\n'.format(self.boundary))
Example #17
Source File: csv_connector.py From python-compat-runtime with Apache License 2.0 | 5 votes |
def __init__(self, stream, fieldnames, encoding='utf-8', **kwds): """Initialzer. Args: stream: Stream to write to. fieldnames: Fieldnames to pass to the DictWriter. encoding: Desired encoding. kwds: Additional arguments to pass to the DictWriter. """ writer = codecs.getwriter(encoding) if (writer is encodings.utf_8.StreamWriter or writer is encodings.ascii.StreamWriter or writer is encodings.latin_1.StreamWriter or writer is encodings.cp1252.StreamWriter): self.no_recoding = True self.encoder = codecs.getencoder(encoding) self.writer = csv.DictWriter(stream, fieldnames, **kwds) else: self.no_recoding = False self.encoder = codecs.getencoder('utf-8') self.queue = cStringIO.StringIO() self.writer = csv.DictWriter(self.queue, fieldnames, **kwds) self.stream = writer(stream)
Example #18
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_getencoder(self): self.assertRaises(TypeError, codecs.getencoder) self.assertRaises(LookupError, codecs.getencoder, "__spam__")
Example #19
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
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
Example #20
Source File: test_codecs.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_bad_encode_args(self): for encoding in all_unicode_encodings: encoder = codecs.getencoder(encoding) with support.check_warnings(): # unicode-internal has been deprecated self.assertRaises(TypeError, encoder)
Example #21
Source File: schema.py From python2017 with MIT License | 5 votes |
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). try: import sqlite3 value = sqlite3.adapt(value) except ImportError: pass 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)))
Example #22
Source File: __init__.py From aws_logging_handlers with MIT License | 5 votes |
def encoder(self, val): _ = codecs.getencoder(val) self._encoder = val
Example #23
Source File: schema.py From openhgsenti with Apache License 2.0 | 5 votes |
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)))
Example #24
Source File: sqltypes.py From planespotter with MIT License | 5 votes |
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
Example #25
Source File: test_certification.py From python with Apache License 2.0 | 5 votes |
def new_client_id(self, _server): client_id = codecs.getencoder('hex')(os.urandom(16))[0].decode("ascii") if not hasattr(_server, "next_method"): _server.next_method = {} if not hasattr(_server, "lifetimes"): _server.lifetimes = [] _server.next_method[client_id] = "Start" _server.lifetimes.append((time.time(), client_id)) return client_id
Example #26
Source File: schema.py From python with Apache License 2.0 | 5 votes |
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). try: import sqlite3 value = sqlite3.adapt(value) except ImportError: pass 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)))
Example #27
Source File: subprocess_repl.py From jupyter-powershell with MIT License | 5 votes |
def __init__(self, cmd): self.encoder = getencoder('utf8') self.decoder = getincrementaldecoder('utf8')() self.popen = Popen(cmd, bufsize=1, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, stdout=subprocess.PIPE) if POSIX: flags = fcntl.fcntl(self.popen.stdout, fcntl.F_GETFL) fcntl.fcntl(self.popen.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
Example #28
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_basics(self): binput = bytes(range(256)) for encoding in bytes_transform_encodings: with self.subTest(encoding=encoding): # generic codecs interface (o, size) = codecs.getencoder(encoding)(binput) self.assertEqual(size, len(binput)) (i, size) = codecs.getdecoder(encoding)(o) self.assertEqual(size, len(o)) self.assertEqual(i, binput)
Example #29
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_bad_encode_args(self): for encoding in all_unicode_encodings: encoder = codecs.getencoder(encoding) with support.check_warnings(): # unicode-internal has been deprecated self.assertRaises(TypeError, encoder)
Example #30
Source File: test_codecs.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_getencoder(self): self.assertRaises(TypeError, codecs.getencoder) self.assertRaises(LookupError, codecs.getencoder, "__spam__")