-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
64 lines (51 loc) · 1.4 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
#coding=utf8
import sys
import os
import json
import random
import numpy
import timeit
from conf import *
import pickle
sys.setrecursionlimit(1000000)
random.seed(0)
def sample_action(action_probability):
ac = action_probability
ac = ac/ac.sum()
action = numpy.random.choice(numpy.arange(len(ac)),p=ac)
return action
def choose_action(action_probability):
ac_list = list(action_probability)
action = ac_list.index(max(ac_list))
return action
# basic utils
def load_pickle(fname):
with open(fname) as f:
return pickle.load(f)
def write_pickle(o, fname):
with open(fname, 'w') as f:
pickle.dump(o, f, -1)
def load_json_lines(fname):
with open(fname) as f:
for line in f:
yield json.loads(line)
def lines_in_file(fname):
return int(subprocess.check_output(
['wc', '-l', fname]).strip().split()[0])
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
def rmkdir(path):
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
def aa():
return 0
def get_file_name(path,dir_list):
if os.path.isfile(path):
dir_list.append(path)
elif os.path.isdir(path):
for item in os.listdir(path):
itemsrc = os.path.join(path, item)
get_file_name(itemsrc,dir_list)
return dir_list