emmental/curds/sort.py
Anna Schumaker 8a2c817de0 curds: Pass key_func() to sort.bisect()
Rather than relying on giving objects their own sort_key() func. This is
needed to switch over to the new Track class.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2020-11-01 08:59:37 -05:00

32 lines
853 B
Python

# Copyright 2019 (c) Anna Schumaker.
import unicodedata
import os
def bisect(lst, lhs, key_func):
begin = 0
end = len(lst)
while end - begin > 0:
pos = (end + begin) // 2
if lhs == (rhs := key_func(lst[pos])):
return (pos, lst[pos])
elif lhs < rhs:
end = pos
else:
begin = pos + 1
return (begin, None)
def key(text):
if os.path.exists(text):
return text.strip("/").split("/")
words = normalize(text).lower().split()
words = [ ''.join(filter(str.isalnum, w)) for w in words ]
words = [ w for w in words if w != "" ]
if len(words) > 0 and words[0] in [ "a", "the" ]:
return words[1:]
return words
def normalize(text):
decode = unicodedata.normalize("NFD", text)
return decode.encode("ascii", "ignore").decode("utf8")