-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.py
executable file
·32 lines (27 loc) · 982 Bytes
/
IO.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
#!/usr/bin/python
import pprint
import gzip,os,sys,time
print sys.version
import hickle as hkl
from numpy import *
#all numpy symbols have to be imported as * in order to read "array([...])" in .txt file with LoadDict function
set_printoptions(threshold=nan) #make sure numpy will print all elements, so that SaveDict and LoadDict will work even for very large array
def SaveDict(filename, mode, root):
if filename[-4:]!=".txt":
filename+=".txt"
with open(filename, mode) as f:
f.write(pprint.pformat(root))
def LoadDict(filename):
if filename[-4:]!=".txt":
filename+=".txt"
with open(filename, "r") as f:
return eval(f.read())
def SaveBigDict(filename, root):
if filename[-4:]!=".hkl":
filename+=".hkl"
hkl.dump(root, "_"+filename, mode='w', compression='gzip')
os.rename("_"+filename, filename)
def LoadBigDict(filename):
if filename[-4:]!=".hkl":
filename+=".hkl"
return hkl.load(filename)