X-Git-Url: https://git.korewanetadesu.com/?p=python-collate.git;a=blobdiff_plain;f=collate%2F_abcollator.py;h=622766d3b5056c27ca9b9a98ce4609961dd11c1d;hp=71f5f54631a42e8c33ac1945b57ff92066bc0496;hb=7644110ce07ec8a78003ee7db9dcdfe5cbca3854;hpb=c519e411927761939a0461bdf8d0a12b26d965e9 diff --git a/collate/_abcollator.py b/collate/_abcollator.py index 71f5f54..622766d 100644 --- a/collate/_abcollator.py +++ b/collate/_abcollator.py @@ -1,4 +1,57 @@ +"""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): + """Split the string along word boundries.""" + if isinstance(string, str): + string = string.decode(self.encoding, 'replace') + return string.split() + + def sortemekey(self, string): + """Return a key based on sortemes of a string. + + 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') + + # Shove the sortkeyed original string on the end to resolve + # ties intelligently. + return (collate.strings.sortemes(string, self.key), + self.key(string)) +