-
Notifications
You must be signed in to change notification settings - Fork 1
/
ez_auto.py
executable file
·63 lines (50 loc) · 1.28 KB
/
ez_auto.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
#!/usr/bin/python
import os,sys,time,json
def strbin(num,l):
str=bin(num)[2:]
while len(str) < l:
str = '0' + str
return str
def strtrans(trans, offset):
if trans.get('delay'):
str = strbin(trans.get('delay'),7) + '1'
else:
str = trans.get('inB') + trans.get('inA') + '00'
str = str + strbin(offset[trans.get('target')],8)
return str
def strstate(state):
str = state.get('outC')
ntrans = 0
if state.get('transitions'):
ntrans = len(state.get('transitions'))
str = str + strbin(ntrans,4) + state.get('outA')
return str
#### #### #### ####
usage = "Usage: ez_auto.py <automaton.json> [--int]"
if len(sys.argv) < 2 :
print usage
exit(1)
filename = sys.argv[1]
file = open(filename,'r')
autotext = file.read()
autodict = json.loads(autotext )
autosize = 0
offset = []
for state in autodict:
offset.append(autosize)
autosize = autosize + 2
if state.get('transitions'):
autosize = autosize + len(state.get('transitions')) * 2
autobin = strbin(autosize,8)
for state in autodict:
autobin = autobin + strstate(state)
if state.get('transitions'):
for trans in state.get('transitions'):
autobin = autobin + strtrans(trans,offset)
if len(sys.argv) < 3 :
print autobin
else:
ibyte = 0
while ibyte + 8 <= len(autobin):
print int(autobin[ibyte:ibyte+8],2)
ibyte = ibyte + 8