X-Git-Url: https://git.korewanetadesu.com/?p=python-collate.git;a=blobdiff_plain;f=collate%2F_abcollator.py;fp=collate%2F_abcollator.py;h=622766d3b5056c27ca9b9a98ce4609961dd11c1d;hp=12575eaed844603f039009a62262fe66b8d29ace;hb=7644110ce07ec8a78003ee7db9dcdfe5cbca3854;hpb=92fc0878bc7b75741a3434d17310e390a9304e70 diff --git a/collate/_abcollator.py b/collate/_abcollator.py index 12575ea..622766d 100644 --- a/collate/_abcollator.py +++ b/collate/_abcollator.py @@ -1,13 +1,34 @@ +"""Abstract base collator.""" + import collate.strings class Collator(object): + """Abstract base class for Collators. + + Attributes: + locale - the collator follows rules for this locale + encoding - assumed string encoding + """ + + locale = "C" encoding = "ascii" + def __init__(self, locale=None, encoding=None): + pass + def cmp(self, string1, string2): """Return negative if a < b, zero if a == b, positive if a > b.""" return cmp(self.key(string1), self.key(string2)) def key(self, string): + """Return a good sorting key for the string. + + The sort key should be considered an opaque value which is + only meaningful when compared to other sort keys from the same + collator. + """ + if isinstance(string, str): + string = string.decode(self.encoding, 'replace') return string def words(self, string): @@ -19,9 +40,12 @@ class Collator(object): def sortemekey(self, string): """Return a key based on sortemes of a string. - If the string is a str instance, it is decoded to a unicode - instance according to the 'encoding' attribute of the - Collator. + A sorteme, by analogy with grapheme/morpheme/etc. is an atom + of sort information. This is larger than a word boundry but + smaller than a sentence boundry; roughly, a sorteme boundry + occurs between letters and numbers, between numbers and + numbers if 'too much' punctuation exists in between, between + lines. """ if isinstance(string, str): string = string.decode(self.encoding, 'replace')