-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
166 lines (124 loc) · 3.96 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
__author__ = 'Samuel'
import configparser
import datetime
import pickle
import threading
import json
import ujson
import bz2
from itertools import tee
from .logger import *
log = initialize_logging(__name__)
def cross(o, a, b):
"""
2D cross product of OA and OB vectors, i.e. z-component of their 3D cross product.
Returns a positive value, if OAB makes a counter-clockwise turn,
negative for clockwise turn, and zero if the points are collinear.
"""
return (a[0] - o[0]) * (b[1] - o[1]) - (a[1] - o[1]) * (b[0] - o[0])
def check_colinear(v):
for t in tripletwise(v):
if cross(*t) != 0:
return False
return True
def pairwise(iterable):
"""s -> (s0,s1), (s1,s2), (s2, s3), ..."""
a, b = tee(iterable)
next(b)
return zip(a, b)
def grouped(iterable, n=2):
return zip(*[iter(iterable)]*n)
def tripletwise(iterable):
"""s -> (s0,s1,s2), (s1,s2,s3), (s2,s3,s4), ..."""
a, b, c = tee(iterable, 3)
next(b)
next(c)
next(c)
return zip(a, b, c)
def load_object(file_name):
"""
Carrega um objeto persistido.
:param file_name:
"""
with open(file_name, 'rb') as fin:
return pickle.load(fin)
def write_object(obj, file_name):
"""
Persiste um objeto em um arquivo.
:param obj:
:param file_name:
"""
with open(file_name, 'wb') as fout:
pickle.dump(obj, fout, pickle.HIGHEST_PROTOCOL)
def load_config(file_name):
conf = configparser.ConfigParser()
conf.read(file_name)
return conf
def parse_datetime(time_str):
return datetime.datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
def parse_date(time_str):
return datetime.datetime.strptime(time_str, '%Y-%m-%d').date()
def daterange(start_date, end_date):
for n in range(int((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
class SafeThreadError(Exception):
def __init__(self, message):
self.message = message
def safe_thread_start(function, arguments, timeout):
got_lock = threading.Event()
thread = threading.Thread(target=function, args=(got_lock,) + arguments)
thread.start()
if got_lock.wait(timeout):
return thread
else:
log.error('Unresponsive thread.')
raise SafeThreadError('Unresponsive thread.')
def chooser(key):
def element_chooser(indexable):
return indexable[key]
return element_chooser
def json_loader(path):
def loader(files_list):
try:
json_dict = {}
for key, file_name in files_list.items():
with open('%s/%s' % (path, file_name)) as f:
json_dict[key] = json.load(f)
return json_dict
except Exception as e:
log.exception(e)
raise e
return loader
def json_iterator(path):
file_type = path.split('.')[-1]
if file_type == 'bz2':
with bz2.BZ2File(path, 'rb') as f:
for line in f:
yield ujson.loads(line.decode('utf-8'))
else:
with open(path, 'rt', encoding='utf-8') as f:
for line in f:
yield ujson.loads(line)
def write_json_object(obj, path, file_name):
file_path = '%s/%s' % (path, file_name)
with open(file_path, 'w') as f:
json.dump(obj, f)
def path_name(*args):
return '/'.join(args)
def str_to_base36(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
if number < 0:
sign = '-'
number = -number
else:
sign = ''
number, mod = divmod(number, 36)
str_number = alphabet[mod]
while number > 0:
number, mod = divmod(number, 36)
str_number = alphabet[mod] + str_number
return sign + str_number
def base36toggle(number):
if type(number) == int:
return str_to_base36(number)
else:
return int(number, 36)