def none_cmp(xx, yy): x = xx[1] y = yy[1] if x is None and y is None: # No sort_key needed here, because defaults are ascii try: return cmp(xx[2], yy[2]) except TypeError: return 0 if x is None: return 1 if y is None: return -1 if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes): x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y)) try: c = cmp(x, y) except TypeError: c = 0 if c != 0: return c # same as above -- no sort_key needed here try: return cmp(xx[2], yy[2]) except TypeError: return 0
def compare_to_other(self, other): a = cmp(self.base, other.base) if a != 0: return a cx, cy = self.comments_len, other.comments_len if cx and cy: t = (cx + cy) / 20 delta = cy - cx if abs(delta) > t: return -1 if delta < 0 else 1 return cmp(self.extra, other.extra)
def compare_to_other(self, other): if self.is_finished != other.is_finished: return 1 if self.is_finished else -1 if self.start_time is None: if other.start_time is None: # Both waiting return cmp(other.id, self.id) return 1 if other.start_time is None: return -1 # Both running return cmp((other.start_time, id(other)), (self.start_time, id(self)))
def test_sorting(self): ' Test the various sorting APIs ' german = '''Sonntag Montag Dienstag Januar Februar März Fuße Fluße Flusse flusse fluße flüße flüsse'''.split() german_good = '''Dienstag Februar flusse Flusse fluße Fluße flüsse flüße Fuße Januar März Montag Sonntag'''.split() french = '''dimanche lundi mardi janvier février mars déjà Meme deja même dejà bpef bœg Boef Mémé bœf boef bnef pêche pèché pêché pêche pêché'''.split() french_good = '''bnef boef Boef bœf bœg bpef deja dejà déjà dimanche février janvier lundi mardi mars Meme Mémé même pèché pêche pêche pêché pêché'''.split() # noqa # Test corner cases sort_key = icu.sort_key s = '\U0001f431' self.ae(sort_key(s), sort_key(s.encode(sys.getdefaultencoding())), 'UTF-8 encoded object not correctly decoded to generate sort key') self.ae(s.encode('utf-16'), s.encode('utf-16'), 'Undecodable bytestring not returned as itself') self.ae(b'', sort_key(None)) self.ae(0, icu.strcmp(None, b'')) self.ae(0, icu.strcmp(s, s.encode(sys.getdefaultencoding()))) # Test locales with make_collation_func('dsk', 'de', func='sort_key') as dsk: self.ae(german_good, sorted(german, key=dsk)) with make_collation_func('dcmp', 'de', template='_strcmp_template') as dcmp: for x in german: for y in german: self.ae(cmp(dsk(x), dsk(y)), dcmp(x, y)) with make_collation_func('fsk', 'fr', func='sort_key') as fsk: self.ae(french_good, sorted(french, key=fsk)) with make_collation_func('fcmp', 'fr', template='_strcmp_template') as fcmp: for x in french: for y in french: self.ae(cmp(fsk(x), fsk(y)), fcmp(x, y)) with make_collation_func('ssk', 'es', func='sort_key') as ssk: self.assertNotEqual(ssk('peña'), ssk('pena')) with make_collation_func('scmp', 'es', template='_strcmp_template') as scmp: self.assertNotEqual(0, scmp('pena', 'peña')) for k, v in iteritems({u'pèché': u'peche', u'flüße':u'Flusse', u'Štepánek':u'ŠtepaneK'}): self.ae(0, icu.primary_strcmp(k, v)) # Test different types of collation self.ae(icu.primary_sort_key('Aä'), icu.primary_sort_key('aa')) self.assertLess(icu.numeric_sort_key('something 2'), icu.numeric_sort_key('something 11')) self.assertLess(icu.case_sensitive_sort_key('A'), icu.case_sensitive_sort_key('a')) self.ae(0, icu.strcmp('a', 'A')) self.ae(cmp('a', 'A'), icu.case_sensitive_strcmp('a', 'A')) self.ae(0, icu.primary_strcmp('ä', 'A'))
def none_cmp(xx, yy): x = xx[1] y = yy[1] if x is None and y is None: # No sort_key needed here, because defaults are ascii return cmp(xx[2], yy[2]) if x is None: return 1 if y is None: return -1 if isinstance(x, string_or_bytes) and isinstance(y, string_or_bytes): x, y = sort_key(force_unicode(x)), sort_key(force_unicode(y)) c = cmp(x, y) if c != 0: return c # same as above -- no sort_key needed here return cmp(xx[2], yy[2])
def pynocase(one, two, encoding='utf-8'): if isbytestring(one): try: one = one.decode(encoding, 'replace') except: pass if isbytestring(two): try: two = two.decode(encoding, 'replace') except: pass return cmp(one.lower(), two.lower())
def two_args(a, b): nonlocal func if func is None: func = getattr(collator_function(), func_name) try: return func(a, b) except TypeError: if isinstance(a, bytes): try: a = a.decode(sys.getdefaultencoding()) except Exception: return cmp(a, b) elif a is None: a = '' if isinstance(b, bytes): try: b = b.decode(sys.getdefaultencoding()) except Exception: return cmp(a, b) elif b is None: b = '' return func(a, b)
def icu_collator(s1, s2): return cmp(sort_key(force_unicode(s1, 'utf-8')), sort_key(force_unicode(s2, 'utf-8')))
def compare_to_other(self, other): for i, ascending in enumerate(self.orders): ans = cmp(self.values[i], other.values[i]) if ans != 0: return ans * ascending return 0
def compareFileStream(file1, file2): return cmp(file1.fileName, file2.fileName)