-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
50 lines (39 loc) · 1.13 KB
/
util.py
File metadata and controls
50 lines (39 loc) · 1.13 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import json
def sort_count_pairs(l):
return list(sorted(l, key=cmp_to_key(cmp_count_tuples)))
def cmp_to_key(mycmp):
class CmpFn:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return mycmp(self.obj, other.obj) < 0
def __gt__(self, other):
return mycmp(self.obj, other.obj) > 0
def __eq__(self, other):
return mycmp(self.obj, other.obj) == 0
def __le__(self, other):
return mycmp(self.obj, other.obj) <= 0
def __ge__(self, other):
return mycmp(self.obj, other.obj) >= 0
def __ne__(self, other):
return mycmp(self.obj, other.obj) != 0
return CmpFn
def cmp_count_tuples(t0, t1):
(key0, val0) = t0
(key1, val1) = t1
if val0 > val1:
return -1
if val0 < val1:
return 1
if key0 < key1:
return -1
if key0 > key1:
return 1
return 0
def get_json_from_file(filename):
try:
return json.load(open(filename))
except OSError as e:
print(e, file=sys.stderr)
sys.exit(1)