how to reference custom codec in a package?

Brian Quinlan brian at sweetapp.com
Mon Nov 10 17:40:52 EST 2003



-----Original Message-----
From: python-list-bounces+brian=sweetapp.com at python.org
[mailto:python-list-bounces+brian=sweetapp.com at python.org] On Behalf Of Skip
Montanaro
Sent: Monday, November 10, 2003 2:29 PM
To: python-list at python.org
Subject: how to reference custom codec in a package?

> I wrote a simple codec which strips accents from latin-1 characters (e.g.,
> maps 'é' to 'e').  If it lives in a package how do I refer to it?  For
> example, what should this statement look like 

>    x = unicode(x, "latin1").encode("latscii")

You probably want to write a codec registry function. Something like this:

import codecs

def latscii_search_function(name):
    if name == 'latscii':
        try:
        	import latscii
        except ImportError:
            return None
        else:
            codec = latscii.Codec()
            return (codec.encode, codec.decode, latscii.StreamReader,
                    latscii.StreamWriter)

codecs.register(latscii_search_function)

Cheers,
Brian






More information about the Python-list mailing list