diff --git a/src/snipper/__pycache__/snip_dir.cpython-39.pyc b/src/snipper/__pycache__/snip_dir.cpython-39.pyc index 9ef477901204cbd8229df0cdb6aef9d69a860bb7..f06c0cff0dda895b9c14db5d9f395341c8e08f6d 100644 Binary files a/src/snipper/__pycache__/snip_dir.cpython-39.pyc and b/src/snipper/__pycache__/snip_dir.cpython-39.pyc differ diff --git a/src/snipper/block_parsing.py b/src/snipper/block_parsing.py index 27ca1b91d2f8b21173ffa5186bf863c83cf73672..d8996d86bf7c37ad76f343f29a77faea42cba068 100644 --- a/src/snipper/block_parsing.py +++ b/src/snipper/block_parsing.py @@ -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) diff --git a/src/snipper/fix_bf.py b/src/snipper/fix_bf.py index 8326139200cc466d86dff1d70453be840083b331..00fb61da60310a0284258ae694c5477bcb9e880e 100644 --- a/src/snipper/fix_bf.py +++ b/src/snipper/fix_bf.py @@ -1,9 +1,9 @@ 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 @@ -69,7 +69,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 @@ -78,6 +82,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 @@ -86,8 +92,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): @@ -98,11 +124,77 @@ def fix_b(lines, keep=False): if b == None: break args = {k:v for k, v in b['start_tag_args'].items() if len(k) > 0} +<<<<<<< HEAD cutout.append( b['block'] ) b['block'], dn = _block_fun(b['block'], start_extra=b['arg1'], end_extra=b['arg2'], **args, keep=keep) +======= + cutout += b['block'] + # 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) + +>>>>>>> 1006ff966030717f7654e4b98720394bb394bb57 lines = block_join(b) # cutout +_= 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 diff --git a/src/snipper/test.py b/src/snipper/test.py new file mode 100644 index 0000000000000000000000000000000000000000..233f5f12b361dd6780240e92fe89d863d68ad09f --- /dev/null +++ b/src/snipper/test.py @@ -0,0 +1 @@ +import d4rl