-
Notifications
You must be signed in to change notification settings - Fork 15
/
utils.py
52 lines (40 loc) · 1.17 KB
/
utils.py
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
51
52
'''
Miscellaneous utility functions
'''
import os
import operator
import sys
def is_python3():
'''
Return True if the python interpreter is 3
'''
return sys.version_info > (3, 0)
def get_basename_without_extension(filepath):
'''
Getting the basename of the filepath without the extension
E.g. 'data/formatted/dischargeSummariesClean.csv' -> 'dischargeSummariesClean'
'''
return os.path.basename(os.path.splitext(filepath)[0])
def reverse_dictionary(dict):
return {v: k for k, v in dict.items()}
def unique_sorted(my_list):
return sorted(set(my_list))
def sort_dictionary_by_value(my_dict):
'''
Example:
{1: 2, 3: 4, 4: 3, 2: 1, 0: 0} -> [(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
'''
sorted_dict = sorted(my_dict.items(), key=operator.itemgetter(1))
return sorted_dict
def sort_dictionary_keys_by_value(my_dict):
sorted_dict = sort_dictionary_by_value(my_dict)
output = []
for kv in sorted_dict :
output.append(kv[0])
return output
def create_folder_if_not_exists(dir):
'''
Create the folder if it doesn't exist already.
'''
if not os.path.exists(dir):
os.makedirs(dir)