""" open/dulcinea/lib/sort.py """ from qpy import stringify def str_sort(seq): """(seq) -> [] Sort 'seq' by the str() of each element. """ return sorted(seq, key=stringify) def lexical_sort(seq): """(seq) -> [] Sort 'seq' by the stringify().lower() of each element. """ def stringify_lower(x): return stringify(x).lower() return sorted(seq, key=stringify_lower) def attr_sort(seq, attr): """(seq, attr : string) -> [] Sort 'seq' by the attribute 'attr' of each element. """ attr = str(attr) def get_attr(v): return getattr(v, attr) return sorted(seq, key=get_attr) def method_sort(seq, method): """(seq, method : string) -> [] Sort 'seq' by the result of calling method 'method' on each element. """ method = str(method) def meth(v): return getattr(v, method)() return sorted(seq, key=meth)