-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_module.py
More file actions
executable file
·74 lines (53 loc) · 2.02 KB
/
file_module.py
File metadata and controls
executable file
·74 lines (53 loc) · 2.02 KB
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename: file_module.py
# Escrevendo em python3 e usando python2.7:
from __future__ import print_function, unicode_literals, absolute_import, division
from os import path, mkdir
from itertools import izip
def l_to_s(list_of_numbers):
''' Return list in a likeable format '''
return ' '.join(map(str, list(list_of_numbers)))
#
# Print pretty matrix format
#
def pretty_print(matrix):
try:
matrix = [[str(x) if x else "0" for x in row] for row in matrix]
field_length = max(len(x) for row in matrix for x in row)
output = "\n".join(" ".join("%%%ds" % field_length % x for x in row) for row in matrix)
except TypeError:
matrix = [str(x) if x else "0" for x in matrix]
field_length = max(len(x) for x in matrix)
output = " ".join("%%%ds" % field_length % x for x in matrix)
return output
def FileQuery(query,filename):
log = open(path.join("Backup",str(filename)+".dat"),'w').write
for item1 in dict.iterkeys(query):
log("\n{0} \n\n".format(item1))
for item2 in dict.iterkeys(query[item1]):
log("{0:8} ".format(item2)+" ".join([str(item) for item in dict.itervalues(query[item1][item2])])+"\n")
print(str(filename)+".dat has been generated.")
def writedict(dic, f):
f.write('\n'.join(["{0} {1}".format(item, dic[item]) for item in dict.iterkeys(dic)]))
f.close()
def writeit(text, f):
f.write('\n'.join([line for line in text]))
f.close()
def pickle(query, test, filename):
'''
Pickle a dictionary.
'''
import cPickle as pickle
output = open(path.join("TESTS",test,filename+".pkl"),'wb')
pickle.dump(query,output, 2)
output.close()
def unpickle(test, filename):
'''
Recover a Pickle of a dictionary.
'''
import cPickle as pickle
output = open(path.join("TESTS",test,filename+".pkl"),'rb')
rec = pickle.load(output)
output.close()
return rec