Skip to content
Snippets Groups Projects
Commit 1c982319 authored by tuhe's avatar tuhe
Browse files

updates

parent 8c3decca
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -83,6 +83,10 @@ def block_split(lines, tag):
post = line[nx_tag:]
else:
post = ''
if " " in arg1: # remove block tags.
arg1 = arg1[arg1.find(" "):].strip()
else:
arg1 = ""
return arg1, post
contents['arg1'], contents['post1'] = argpost(lines[i], j)
......
import functools
import hashlib
from snipper.legacy import gcoms
from snipper.block_parsing import indent
from snipper.block_parsing import block_split, block_join
def fix_f(lines, debug, keep=False):
lines2 = []
i = 0
......@@ -63,7 +63,11 @@ def fix_f(lines, debug, keep=False):
return lines2
# stats = {'n': 0}
def _block_fun(lines, start_extra, end_extra, keep=False, silent=False):
def _block_fun(lines, start_extra, end_extra, keep=False, permute=False, questionmarks=false, halfquestionmarks=False, silent=False):
methods = {'remove': 0}
# if method not in ['remove', 'permute', 'questionmark', 'halfquestionmark']:
# assert False
id = indent(lines[0])
if not keep:
lines = lines[1:] if len(lines[0].strip()) == 0 else lines
......@@ -72,6 +76,8 @@ def _block_fun(lines, start_extra, end_extra, keep=False, silent=False):
ee = end_extra.strip()
if len(ee) >= 2 and ee[0] == '"':
ee = ee[1:-1]
if len(ee) == 0:
ee = "Insert your solution and remove this error."
start_extra = start_extra.strip()
if keep:
l2 = ['GARBAGE'] * cc
......@@ -80,8 +86,28 @@ def _block_fun(lines, start_extra, end_extra, keep=False, silent=False):
l2 = []
cc = 0
else:
l2 = ([id + start_extra] if len(start_extra) > 0 else []) + [id + f"# TODO: {cc} lines missing.",
id + f'raise NotImplementedError("{ee}")']
# Ok we got so far. Now decide on randomization strategies and so on.
insert_lines = False
msg = []
if permute:
msg = [id + "# TODO: The following lines have been permuted. Can you put them back in order?"]
# Permute all lines.
lines = f1(lines)
insert_lines = True
pass
if questionmarks:
lines = f2(lines)
insert_lines = True
elif halfquestionmarks:
lines = f3(lines)
insert_lines = True
if not insert_lines:
lines = [id + f"# TODO: {cc} lines missing."]
else:
lines = msg + lines
lines += [id + f'raise NotImplementedError("{ee}")']
l2 = ([id + start_extra] if len(start_extra) > 0 else []) + lines # [id + f"# TODO: {cc} lines missing.",
# id + f'raise NotImplementedError("{ee}")']
return l2, cc
def fix_b(lines, keep=False):
......@@ -93,9 +119,70 @@ def fix_b(lines, keep=False):
break
args = {k:v for k, v in b['start_tag_args'].items() if len(k) > 0}
cutout += b['block']
b['block'], dn = _block_fun(b['block'], start_extra=b['arg1'], end_extra=b['arg2'], **args, keep=keep)
# method = b['start_tag_args'].get('', 'remove')
b['block'], dn = _block_fun(b['block'], start_extra=b['arg1'], end_extra=b['arg1'], **args, keep=keep)
lines = block_join(b)
n += dn
# lines2, _, _, cutout = block_process(lines, tag="#!b", block_fun=functools.partial(block_fun, stats=stats))
return lines, n, cutout
\ No newline at end of file
return lines, n, cutout
import textwrap
import numpy as np
def wspace(l):
whitespace = " " * (len(l) - len(l.lstrip()))
return whitespace
def cmnt(lines):
whitespace = " " * (len(lines[0]) - len(lines[0].lstrip()))
lines = textwrap.dedent("\n".join(lines)).splitlines()
lines = ["# " + l for l in lines]
return lines, whitespace
# Example 1: Simply permute the lines
def f1(lines, seed=None):
# Hash the seed.
if seed == None:
ss = "".join([l.strip() for l in lines])
seed = int(hashlib.sha1(ss.encode("utf-8")).hexdigest(), 16) % (10 ** 8)
# seed = abs(hash("".join([l.strip() for l in lines]))) % (10 ** 8)
permutation = np.random.RandomState(seed=seed).permutation(len(lines))
# print(seed)
# print(lines)
# print(permutation)
lines, whitespace = cmnt(lines)
lines = [lines[i] for i in permutation]
lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
return lines
# obscure(blk, f1, 'cs101_output/obscure_1.py')
# Example 2: Try to preserve keywords and special syntax symbols
def f2(lines):
lines, whitespace = cmnt(lines)
kp = """#'"[](){},.+-012345679:="""
l2 = []
for line in lines:
line2 = []
for w in line.split(' '):
if w in ['', 'return', 'if', 'else' '=', '#', "for", "in"]:
line2.append(w)
else:
w2 = "".join( [ (t if t in kp else '?') for t in w] )
line2.append(w2)
l2.append(" ".join(line2))
lines = l2
lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
return lines
# obscure(blk, f2, 'cs101_output/obscure_2.py')
# Example 3: keep half of the lines
def f3(lines):
lines = [ (l.strip(), len(l.strip()), wspace(l)) for l in lines ]
lines = [ wp + l[:k//2] + "?"*(k-k//2) for l, k, wp in lines]
lines, whitespace = cmnt(lines)
lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
return lines
\ No newline at end of file
import d4rl
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment