-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathutils.py
50 lines (36 loc) · 1.29 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
# -*- coding: utf-8 -*-
from time import time
import logging,inspect
import cPickle as pickle
from itertools import islice
import os.path
dir_f = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
folder_pickles = dir_f+"/../pickles/"
def returnPathStruc2vec():
return dir_f
def isPickle(fname):
return os.path.isfile(dir_f+'/../pickles/'+fname+'.pickle')
def chunks(data, SIZE=10000):
it = iter(data)
for i in xrange(0, len(data), SIZE):
yield {k:data[k] for k in islice(it, SIZE)}
def partition(lst, n):
division = len(lst) / float(n)
return [ lst[int(round(division * i)): int(round(division * (i + 1)))] for i in xrange(n) ]
def restoreVariableFromDisk(name):
logging.info('Recovering variable...')
t0 = time()
val = None
with open(folder_pickles + name + '.pickle', 'rb') as handle:
val = pickle.load(handle)
t1 = time()
logging.info('Variable recovered. Time: {}m'.format((t1-t0)/60))
return val
def saveVariableOnDisk(f,name):
logging.info('Saving variable on disk...')
t0 = time()
with open(folder_pickles + name + '.pickle', 'wb') as handle:
pickle.dump(f, handle, protocol=pickle.HIGHEST_PROTOCOL)
t1 = time()
logging.info('Variable saved. Time: {}m'.format((t1-t0)/60))
return