-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerateData.py
executable file
·76 lines (62 loc) · 2.23 KB
/
generateData.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
import sys
import numpy as np
from utils import *
# Dim of square code blocks
SIZE = 80
def chopFile(filename):
# Open file and create SIZExSIZE char arr to chop code into blocks
f = open(filename, "r")
matrix = np.zeros((SIZE, SIZE), dtype=np.uint8)
codeBlocks = []
char = f.read(1)
row = 0
col = 0
while char != '':
asciiVal = ord(char)
matrix[row, col] = asciiVal
# Increment & read next char
col += 1
if char == '\n' or col == SIZE:
row += 1
col = 0
col = col % SIZE
char = f.read(1)
# End of code block reached, create new block
if row == SIZE:
if isValidPython(codeBlockToString(matrix)):
codeBlocks.append(matrix)
matrix = np.zeros((SIZE, SIZE), dtype=np.uint8)
row = 0
col = 0
else:
nextBlock = np.zeros((0, SIZE), dtype=np.uint8)
lastRowErased = SIZE
while not isValidPython(codeBlockToString(matrix)):
nextBlock = np.insert(nextBlock, 0, matrix[lastRowErased - 1], axis=0)
matrix[lastRowErased - 1] = 0
lastRowErased -= 1
if lastRowErased < 1:
#print(len(codeBlocks))
#raise Exception("Can not create a valid code block...")
return writeBlocks(f, filename, codeBlocks)
codeBlocks.append(matrix)
matrix = np.zeros((SIZE, SIZE), dtype=np.uint8)
row = nextBlock.shape[0]
col = 0
matrix[0:row] = nextBlock[0:row]
if not (matrix == 0).all() and isValidPython(codeBlockToString(matrix)):
codeBlocks.append(matrix)
return writeBlocks(f, filename, codeBlocks)
def writeBlocks(f, filename, codeBlocks):
# Write chopped blocks to .npy file
writeName = filename.split("/")[-1].split(".")[0]
for i, block in enumerate(codeBlocks):
np.save("./dataset/" + writeName + "_" + str(i) + ".npy", block)
f.close()
return codeBlocks
if __name__ == "__main__":
# filename to be chopped passed as command line arg
if len(sys.argv) != 2:
print("Usage: python generateData.py <filename.py>")
else:
chopFile(sys.argv[1])