-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.py
More file actions
163 lines (126 loc) · 4.18 KB
/
util.py
File metadata and controls
163 lines (126 loc) · 4.18 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from os import popen
from pickle import dumps as serialize, loads as unserialize
from math import sqrt
# Exceptions
class AutomataError(Exception):
pass
# Single Attributes Get/Set
def genGetMethod(attr):
def getMethod(self):
return getattr(self, attr)
return getMethod
def genSetMethod(attr, validType):
if type(validType) is type:
def setMethod(self, val):
if type(val) == validType:
setattr(self, attr, val)
else:
raise AutomataError('%s must be a %s, not %s.' % (
attr.strip('_'), validType.__name__, type(val).__name__))
else:
def setMethod(self, val):
if validType(val):
setattr(self, attr, val)
else:
raise AutomataError(
'Attempted to set an invalid %s.' % attr.strip('_'))
return setMethod
# List Attributes Get/Set
def genCollGetMethod(attr):
def getMethod(self):
return tuple(getattr(self, attr))
return getMethod
def genCollSetMethod(attr, validType, colltype=None):
if colltype:
def setMethod(self, val):
if all([type(i) == validType for i in val]):
setattr(self, attr, colltype(val))
else:
raise AutomataError('%s must contain only items of type: %s.' % (
attr.strip('_'), validType.__name__))
else:
def setMethod(self, val):
if colltype and all([type(i) == validType for i in val]):
setattr(self, attr, val)
else:
raise AutomataError('%s must contain only items of type: %s.' % (
attr.strip('_'), validType.__name__))
return setMethod
def genAddMethod(attr, validType):
if type(validType) is type:
def addMethod(self, item):
if type(item) is validType:
getattr(self, attr).add(item)
else:
raise AutomataError('%s must contain only items of type: %s, not %s.' % (
attr.strip('_'), validType.__name__, type(item).__name__))
else:
def addMethod(self, item):
if validType(item):
getattr(self, attr).add(item)
else:
raise AutomataError(
'Attempted to set an invalid %s.' % attr.strip('_'))
return addMethod
def genRemMethod(attr, validType):
def remItemMethod(self, which):
try:
if type(which) is validType:
getattr(self, attr).remove(which)
else:
raise AutomataError('%s contains only items of type %s. Attempted to remove a %s' % (
attr.strip('_'), validType.__name__, type(val).__name__))
except:
raise AutomataError(
'Attempted to remove a non-existant item from %s.' % attr.strip('_'))
return remItemMethod
# High-level automata usage
def renderMachine(fa, outfile='temp.png', format='png', size=None):
gvcode = fa.toGraphViz(size)
f = popen(r'dot -T%s -o"%s"' % (format, outfile), 'w')
try:
f.write(gvcode)
except:
raise
finally:
f.close()
def loadMachine(filename):
f = open(filename)
try:
s = f.read()
fsm = unserialize(s)
finally:
f.close()
return fsm
def saveMachine(fsm, filename):
f = open(filename, 'w')
try:
s = serialize(fsm)
f.write(s)
finally:
f.close()
# Label arrangement - quick hack
def arrangeLabel(string):
pieces = string.split()
count = len(pieces)
delta = sqrt(count)
cur = 1
anchor = 0
out = ''
while anchor < count / 2:
out += ' '.join(pieces[int(anchor):int(anchor+cur)]) + '\\n'
anchor += cur
cur += delta
cur -= delta
count -= anchor
while count > 0:
out += ' '.join(pieces[int(anchor):int(anchor+cur)]) + '\\n'
anchor += cur
count -= cur
cur = (cur - delta) if (cur - delta) > 0 else 1
out = out.strip().replace('\\n\\n', '\\n').replace('\\n\\n', '\\n')
while out.startswith('\\n'):
out = out[2:]
while out.endswith('\\n'):
out = out[:-2]
return out