forked from dhvanipa/error_deep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnn_hub.py
219 lines (186 loc) · 7.19 KB
/
cnn_hub.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Copyright 2017 Dhvani Patel
from check_pypy_syntax import checkPyPySyntax
from compile_error import CompileError
import sqlite3
import tokenize
import token
from io import StringIO
import keyword
from Token import Token
import json
from pylab import imshow, show, get_cmap
import numpy as np
from PIL import Image
global all_tokens
def open_closed_tokens(token):
"""
'Flattens' Python into tokens based on whether the token is open or
closed.
"""
# List of token names that whose text should be used verbatim as the type.
VERBATIM_CLASSES = {
"AMPER", "AMPEREQUAL", "ASYNC", "AT", "ATEQUAL", "AWAIT", "CIRCUMFLEX",
"CIRCUMFLEXEQUAL", "COLON", "COMMA", "DOT", "DOUBLESLASH",
"DOUBLESLASHEQUAL", "DOUBLESTAR", "DOUBLESTAREQUAL", "ELLIPSIS",
"EQEQUAL", "EQUAL", "GREATER", "GREATEREQUAL", "LBRACE", "LEFTSHIFT",
"LEFTSHIFTEQUAL", "LESS", "LESSEQUAL", "LPAR", "LSQB", "MINEQUAL",
"MINUS", "NOTEQUAL", "OP", "PERCENT", "PERCENTEQUAL", "PLUS", "PLUSEQUAL",
"RARROW", "RBRACE", "RIGHTSHIFT", "RIGHTSHIFTEQUAL", "RPAR", "RSQB",
"SEMI", "SLASH", "SLASHEQUAL", "STAR", "STAREQUAL", "TILDE", "VBAR",
"VBAREQUAL"
}
OTHER = { "NEWLINE", "INDENT", "DEDENT"}
CHECK = {"None", "True", "False"}
if token.type == 'NAME':
# Special case for NAMES, because they can also be keywords.
if keyword.iskeyword(token.value):
return token.value
elif token.value in CHECK:
return token.value
else:
return '<IDENTIFIER>'
elif token.type in VERBATIM_CLASSES:
# These tokens should be mapped verbatim to their names.
assert ' ' not in token.value
return token.value
elif token.type in {'NUMBER', 'STRING'}:
# These tokens should be abstracted.
# Use the <ANGLE-BRACKET> notation to signify these classes.
return "<" + token.type.upper() + ">"
elif token.type in OTHER:
return token.type
else:
# Use these token's name verbatim.
# assert token.type in {
# 'NEWLINE', 'INDENT', 'DEDENT',
# 'ENDMARKER', 'ENCODING', 'COMMENT', 'NL', 'ERRORTOKEN'
#}
return token.value
def vocabularize_tokens(every_token, flag):
if flag == False:
EXTRANEOUS_TOKENS = {
# Always occurs as the first token: internally indicates the file
# ecoding, but is irrelelvant once the stream is already tokenized
'ENCODING',
# Always occurs as the last token.
'ENDMARKER',
# Insignificant newline; not to be confused with NEWLINE
'NL',
'NEWLINE',
# Discard comments
'COMMENT',
# Represents a tokenization error. This should never appear for
# syntatically correct files.
'ERRORTOKEN',
}
elif flag == True:
EXTRANEOUS_TOKENS = {
# Always occurs as the first token: internally indicates the file
# ecoding, but is irrelelvant once the stream is already tokenized
'ENCODING',
# Always occurs as the last token.
'ENDMARKER',
# Discard comments
'COMMENT',
# Represents a tokenization error. This should never appear for
# syntatically correct files.
'ERRORTOKEN',
}
all_tokens_iter = every_token[:]
for Token in all_tokens_iter:
vocab_entry = open_closed_tokens(Token)
Token.value = vocab_entry
if Token.type in EXTRANEOUS_TOKENS:
every_token.remove(Token)
if flag == True:
if Token.value == "\\n":
every_token.remove(Token)
#for Token in every_token:
#print Token.value
return every_token
# Create list of tokens
def handle_token(all_tokens):
allReturn = []
for tok in all_tokens:
type = tok[0]
token = tok[1]
take = tok[2]
takeT = tok[3]
line = tok[4]
srow = take[0]
scol = take[1]
erow = takeT[0]
ecol = takeT[1]
if repr(token)[:2] == 'u\'':
val = repr(token)[2:len(repr(token))-1]
else:
val = repr(token)[1:len(repr(token))-1]
send = Token(tokenize.tok_name[type], val, srow, scol, erow, ecol, line)
allReturn.append(send)
print ("%d,%d-%d,%d:\t%s\t%s" % \
(srow, scol, erow, ecol, tokenize.tok_name[type], repr(token)))
return allReturn
def create(numFile):
sqlite_file = "/home/dhvani/python-sources.sqlite3"
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
#print "Success Connection to database..."
c.execute("SELECT source FROM source_file INNER JOIN eligible_source ON source_file.hash = eligible_source.hash")
#print "Executed SELECT..."
#print "Fetching all rows..."
all_rows = c.fetchmany(size=2100)
conn.close()
toTest = checkPyPySyntax(all_rows[numFile][0])
if toTest == None:
#print all_rows[numFile][0]
all_tokens = []
text = (all_rows[numFile][0]).decode('utf-8')
print (type(text))
tokenStream = tokenize.generate_tokens(StringIO(text).readline)
print (tokenize.tok_name)
for tok in tokenStream:
all_tokens.append([tok.exact_type, tokenize.tok_name[tok.exact_type], tok[2], tok[3], tok[4]])
#print (tok)
allGood = handle_token(all_tokens[:])
#print (allGood[21].type)
gotWhat = vocabularize_tokens(allGood, False)
lines = []
maxCol = -1
for tok in allGood:
print (tok.value)
lines.append(tok.srow)
maxComp = tok.ecol
if maxComp > maxCol:
maxCol = maxComp
all_text = (all_rows[numFile][0]).decode()
#print gotWhat[0].value
print (len(all_tokens))
print (len(gotWhat))
print (lines)
num_lines = len(set(lines))
print (num_lines)
print (maxCol)
imageArrOne = [-1] * maxCol
a = []
for _ in range(num_lines):
a.append(imageArrOne)
#a = np.column_stack((imageArrOne, imageArrTwo))
#print (a.shape)
#imshow([[0], [1], [0]], cmap=get_cmap("Spectral"), interpolation='none')
#show()
im = Image.new("RGB", (maxCol, num_lines))
pix = im.load()
for x in range(maxCol):
for y in range(num_lines):
pix[x,y] = (255,255,255)
pix[5,6] = (255,0,0)
im.save("test.png", "PNG")
#print all_text
#print one_hot_good
else:
print ("Try again...")
print (numFile)
if __name__ == '__main__':
create(4)
#for x in range(10):
#create(x)