Skip to content Skip to sidebar Skip to footer

Custom Python Charmap Codec

I'm trying to write a custom Python codec. Here's a short example: import codecs class TestCodec(codecs.Codec): def encode(self, input_, errors='strict'): return codec

Solution 1:

Look at https://docs.python.org/3/library/codecs.html#encodings-and-unicode (third paragraph):

There’s another group of encodings (the so called charmap encodings) that choose a different subset of all Unicode code points and how these code points are mapped to the bytes 0x0-0xff. To see how this is done simply open e.g. encodings/cp1252.py (which is an encoding that is used primarily on Windows). There’s a string constant with 256 characters that shows you which character is mapped to which byte value.

take the hint to look at encodings/cp1252.py, and check out the following code:

import codecs

class TestCodec(codecs.Codec):
    def encode(self, input_, errors='strict'):
        return codecs.charmap_encode(input_, errors, encoding_table)

    def decode(self, input_, errors='strict'):
        return codecs.charmap_decode(input_, errors, decoding_table)

def lookup(name):
    if name != 'test':
        return None
    return codecs.CodecInfo(
        name='test',
        encode=TestCodec().encode,
        decode=TestCodec().decode,
    )

decoding_table = (
    'z'
    'a'
    'b'
    'c'
)    
encoding_table=codecs.charmap_build(decoding_table)
codecs.register(lookup)

### --- following is test/debug code
print(ascii(encoding_table))

print(b'\x01\x02\x03'.decode('test'))
foo = 'abc'.encode('test')
print(ascii(foo))

Output:

{97: 1, 122: 0, 99: 3, 98: 2}
abc
b'\x01\x02\x03'

Post a Comment for "Custom Python Charmap Codec"