Skip to content

Commit

Permalink
Updated to follow python style guide.
Browse files Browse the repository at this point in the history
Removed underscores and extra lines.
Followed python code convention.
  • Loading branch information
MiSayali committed Feb 9, 2023
1 parent 32fb7fd commit 3769e0e
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions gen_err.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import random
import re

__action_pattern_map = {
action_pattern_map = {
'delete(': ("\(", ""),
'delete)': ("\)", ""),
'delete,': (",", ""),
Expand All @@ -26,8 +26,7 @@
'replace,with;': (",", ";"),
}


__action_pattern_map_kw = {
action_pattern_map_kw = {
'deleteWhile': ("while", ""),
'deleteElif': ("elif", ""),
'deleteIf': ("if", ""),
Expand All @@ -48,63 +47,51 @@
'replaceElifwithElseif': ("elif", "elseif"),
}

_actions = list(__action_pattern_map.keys())
_actions_kw = list(__action_pattern_map_kw.keys())
actions = list(action_pattern_map.keys())
actions_kw = list(action_pattern_map_kw.keys())

def corrupt_syntax(line):

err_cnt = random.randint(0, 2)
cnt = 0
np.random.shuffle(_actions)

for act in _actions:
np.random.shuffle(actions)
for act in actions:
if cnt > err_cnt:
break

else:
_patt = __action_pattern_map[act] #value _patt[0], _patt[1]

positions = [m.span() for m in re.finditer(_patt[0], line)]

patt = action_pattern_map[act] #value patt[0], patt[1]
positions = [m.span() for m in re.finditer(patt[0], line)]
if(len(positions) == 0):
continue
else:
if len(positions) > 1:
to_corrupt = np.random.randint(len(positions))
else:
to_corrupt = 0

line = line[:positions[to_corrupt][0]] + _patt[1] + line[positions[to_corrupt][1]:]
line = line[:positions[to_corrupt][0]] + patt[1] + line[positions[to_corrupt][1]:]
return line

def corrupt_kw_typo(line):

err_cnt = random.randint(0, 2)
cnt = 0

np.random.shuffle(_actions_kw)

for act in _actions_kw:
np.random.shuffle(actions_kw)

for act in actions_kw:
if cnt > err_cnt:
break

else:
_patt = __action_pattern_map_kw[act] #value _patt[0], _patt[1]

positions = [m.span() for m in re.finditer(_patt[0], line)]

patt = action_pattern_map_kw[act] #value patt[0], patt[1]
positions = [m.span() for m in re.finditer(patt[0], line)]
if(len(positions) == 0):
continue
else:
if len(positions) > 1:
to_corrupt = np.random.randint(len(positions))
else:
to_corrupt = 0

line = line[:positions[to_corrupt][0]] + _patt[1] + line[positions[to_corrupt][1]:]
line = line[:positions[to_corrupt][0]] + patt[1] + line[positions[to_corrupt][1]:]
return line


file1 = open('good.txt', 'r')
file2 = open('bad.txt', 'w')

Expand All @@ -114,4 +101,4 @@ def corrupt_kw_typo(line):
file2.write(final_line)

file1.close()
file2.close()
file2.close()

0 comments on commit 3769e0e

Please sign in to comment.