0ae5d451f59defb65cb34fb4a2ca6b2f36152212
[python-collate.git] / collate / _abcollator.py
1 import collate.strings
2
3 class Collator(object):
4 def cmp(self, string1, string2):
5 """Return negative if a < b, zero if a == b, positive if a > b."""
6 return cmp(self.key(string1), self.key(string2))
7
8 def words(self, string):
9 """Split the string along word boundries."""
10 if isinstance(string, str):
11 string = string.decode(self.encoding, 'replace')
12 return string.split()
13
14 def sortemekey(self, string, invalid=float('inf')):
15 """Return a key based on sortemes of a string.
16
17 If the string is a str instance, it is decoded to a unicode
18 instance according to the 'encoding' attribute of the
19 Collator.
20 """
21 keys = []
22 if isinstance(string, str):
23 string = string.decode(self.encoding, 'replace')
24 for sorteme in collate.strings.sortemes(string):
25 num, alpha = collate.strings.numeric(sorteme, invalid)
26 if num == invalid:
27 keys.append(self.key(alpha))
28 else:
29 keys.append(num)
30 # Shove the sortkeyed original string on the end to resolve
31 # ties intelligently.
32 return (keys, self.key(string))