e0bbcbd2f873dbc3cc05b3e3ce45080f3adafe5c
[python-collate.git] / collate / codepoint.py
1 """Codepoint-based collation.
2
3 This collation backend sorts using only the basic codepoint order. It
4 is primarily intended to be used as a baseline and example for other
5 collation backends.
6
7 Use this collation backend if...
8 - You are writing tests for pycollate.
9 - You are writing specialized Unicode software.
10 - You are on a system with no locale module.
11
12 Avoid this backend if...
13 - You are writing a normal program for a normal runtime environment.
14 - You are sorting strings to show normal humans.
15
16 """
17
18 import collate._abcollator
19 import collate._locale
20
21 __all__ = ["Collate"]
22
23 class Collator(collate._abcollator.Collator):
24 """Codepoint-based collation.
25
26 Arguments
27 locale - all parts but encoding ignored, always 'C'
28 encoding - try to use this string encoding
29 """
30
31 def __init__(self, locale=None, encoding=None):
32 dummy, self.encoding = collate._locale.getpair(locale, encoding)
33 self.locale = "C"
34
35 def key(self, string):
36 """Sort key for a string.
37
38 If string is a str instance, it is first decoded according to
39 the 'encoding' attribute of the Collator.
40 """
41 if isinstance(string, str):
42 string = string.decode(self.encoding, 'replace')
43 return string