-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.py
128 lines (90 loc) · 2.79 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from collections import OrderedDict
import datetime
import klepto
from os.path import dirname, abspath, join, expanduser, isfile, exists
from os import environ, makedirs
import pytz
import re
from socket import gethostname
def get_root_path():
return dirname(abspath(__file__))
def get_data_path():
return join(get_root_path(), 'data')
def get_corpus_path():
return join(get_data_path(), 'corpus')
def get_save_path():
return join(get_root_path(), 'save')
def load(filepath, print_msg=True):
fp = proc_filepath(filepath)
if isfile(fp):
return load_klepto(fp, print_msg)
elif print_msg:
print('Trying to load but no file {}'.format(fp))
def load_klepto(filepath, print_msg):
rtn = klepto.archives.file_archive(filepath)
rtn.load()
if print_msg:
print('Loaded from {}'.format(filepath))
return rtn
def save(obj, filepath, print_msg=True):
if type(obj) is not dict and type(obj) is not OrderedDict:
raise ValueError('Can only save a dict or OrderedDict'
' NOT {}'.format(type(obj)))
fp = proc_filepath(filepath, ext='.klepto')
create_dir_if_not_exists(dirname(filepath))
save_klepto(obj, fp, print_msg)
def create_dir_if_not_exists(dir):
if not exists(dir):
makedirs(dir)
def save_klepto(dic, filepath, print_msg):
if print_msg:
print('Saving to {}'.format(filepath))
klepto.archives.file_archive(filepath, dict=dic).dump()
def proc_filepath(filepath, ext='.klepto'):
if type(filepath) is not str:
raise RuntimeError('Did you pass a file path to this function?')
return append_ext_to_filepath(ext, filepath)
def append_ext_to_filepath(ext, fp):
if not fp.endswith(ext):
fp += ext
return fp
def parse_as_int_list(il):
rtn = []
for x in il.split('_'):
x = int(x)
rtn.append(x)
return rtn
def get_user():
try:
home_user = expanduser("~").split('/')[-1]
except:
home_user = 'user'
return home_user
def get_host():
host = environ.get('HOSTNAME')
if host is not None:
return host
return gethostname()
tstamp = None
def get_ts():
global tstamp
if not tstamp:
tstamp = get_current_ts()
return tstamp
def get_current_ts(zone='US/Pacific'):
return datetime.datetime.now(pytz.timezone(zone)).strftime(
'%Y-%m-%dT%H-%M-%S.%f')
def sorted_nicely(l, reverse=False):
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
if type(s) is not str:
raise ValueError('{} must be a string in l: {}'.format(s, l))
return [tryint(c) for c in re.split('([0-9]+)', s)]
rtn = sorted(l, key=alphanum_key)
if reverse:
rtn = reversed(rtn)
return rtn