Missing codecs in Python 3.0

"Martin v. Löwis" martin at v.loewis.de
Wed Jun 3 00:46:07 EDT 2009

samwyse wrote:
> I have a Python 2.6 program (a code generator, actually) that tries
> several methods of compressing a string and chooses the most compact.
> It then writes out something like this:
>   { encoding='bz2_codec', data = '...'}
> I'm having two problems converting this to Py3.  First is the absence
> of the bz2_codec, among others.  It was very convenient for my program
> to delay selection of the decoding method until run-time and then have
> an easy way to load the appropriate code.  Is this gone forever from
> the standard libraries?

bz2 compression is certainly not gone from the standard library; it
is still available from the bz2 module.

I recommend that you write it like

  { decompressor = bz2.decompress, data = '...'}

Then you can still defer invocation of the decompressor until you
need the data.

> Second, I would write my data out using the 'string_escape' codec.
> It, too, has been removed; there's a 'unicode_escape' codec which is
> similar, but I would rather use a 'byte_escape' codec to produce
> literals of the form b'asdf'.  Unfortunately, there isn't one that I
> can find.  I could use the repr function, but that seems less
> efficient.  Does anyone have any ideas?

Why does the repr() function seem less efficient? Did you measure
anything to make it seem so?

I would recommend to use repr() exactly.


More information about the Python-list mailing list