-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutil.py
90 lines (67 loc) · 1.83 KB
/
mutil.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
"""
mutil.py
Stands for Miles' Utilities
"""
def makeDictIfNot(thing, index):
if type(thing) != dict:
thing = dict()
if not thing.has_key(index):
thing[index] = dict()
if type(thing[index]) != dict:
thing[index] = dict()
def makeListIfNot(thing, index):
if type(thing) != list:
thing = dict()
if not thing.has_key(index):
thing[index] = dict()
if type(thing[index]) != dict:
thing[index] = dict()
def describeRows(r):
print
for i in range(len(r)-1, -1, -1):
fucknugget = 'row ' + str(i)
if i == 0:
fucknugget = 'input'
print fucknugget, 'members:',
for huh in r[i]:
print huh.id,
print
def extractNeuronId(neuron):
if type(neuron) == int:
return neuron
return neuron.id
def xor(a, b):
return int(a != b)
def matrixToString(matrix, header=None):
"""
Not mine, I found it here:
http://mybravenewworld.wordpress.com/2010/09/19/print-tabular-data-nicely-using-python/
"""
if type(header) is list:
header = tuple(header)
lengths = []
if header:
for column in header:
lengths.append(len(column))
for row in matrix:
for column in row:
i = row.index(column)
column = str(column)
cl = len(column)
try:
ml = lengths[i]
if cl > ml:
lengths[i] = cl
except IndexError:
lengths.append(cl)
lengths = tuple(lengths)
format_string = ""
for length in lengths:
format_string += "%-" + str(length) + "s "
format_string += "\n"
matrix_str = ""
if header:
matrix_str += format_string % header
for row in matrix:
matrix_str += format_string % tuple(row)
return matrix_str