forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison_tools.py
More file actions
33 lines (23 loc) · 1.06 KB
/
comparison_tools.py
File metadata and controls
33 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
'''Defines various tools for comparisons.'''
import sys
def underscore_hating_key(string):
'''Key function for sorting that treats `_` as last character.'''
assert isinstance(string, str)
return str(string).replace('_', chr(sys.maxunicode))
def process_key_function_or_attribute_name(key_function_or_attribute_name):
'''
Make a key function given either a key function or an attribute name.
Some functions let you sort stuff by entering a key function or an
attribute name by which the elements will be sorted. This function tells
whether we were given a key function or an attribute name, and generates a
key function out of it if needed.
'''
if key_function_or_attribute_name is None:
return None
elif callable(key_function_or_attribute_name):
return key_function_or_attribute_name
else:
assert isinstance(key_function_or_attribute_name, str)
return lambda key: getattr(key, key_function_or_attribute_name)