From 1c982319e181d6bef76ff205ab6429b820388e18 Mon Sep 17 00:00:00 2001 From: Tue Herlau <tuhe@dtu.dk> Date: Fri, 3 Jun 2022 18:43:50 +0200 Subject: [PATCH] updates --- .../__pycache__/snip_dir.cpython-39.pyc | Bin 1913 -> 2048 bytes src/snipper/block_parsing.py | 4 + src/snipper/fix_bf.py | 99 ++++++++++++++++-- src/snipper/test.py | 1 + 4 files changed, 98 insertions(+), 6 deletions(-) create mode 100644 src/snipper/test.py diff --git a/src/snipper/__pycache__/snip_dir.cpython-39.pyc b/src/snipper/__pycache__/snip_dir.cpython-39.pyc index 9ef477901204cbd8229df0cdb6aef9d69a860bb7..d0bd7380a3443205d7db33300c33c4de34e7da06 100644 GIT binary patch literal 2048 zcmYjS&2J+$6t|tp%w(FRX`6oRZb2guNU21F&`L<Fr3+}0I8})DH0%;h#!fPHKC<>? z+eRK~MOOk2tjY<&f|N>J_Qn+s{2@NVi8Ch_4m>+4TaWzwp5KRme$V!VjfRJy{r>HF z|Gg!I{?NkJ=7R7pbg>VFB8n3f%tstxkV!()N>IUiu98@3HK-aMCw5>Pdna(r=msuz zXqDQJaZsc90Cn6yAtu`IV6_ynOys;bh!X~)^-)-i4x^e@FWLr-fDcumi-aH~Vf7=z zw+l4G7pTMqp_meh&j5?87~NY1j1&z4+1Hh_A}uq!H$<fcyfwsc16Np|RcE%u7g&5+ zSTjd+7@1jDq6=JJ@-SawNlc6<Mqkr<THQn?S=bY>ne(0mgcpSWTr5%h41bPgmH$@M zXN}AFmn%tJ)~F*<j7typ;7X4=*U(c$YqItf{b-$CqA?iHF}JQnsn448bpY}5tVKP8 zeWh51JmA?HUAlm=T!J0f<<ebr|NXLIkhTq7ml_RV{tA782AI}q<FPBPy9j)mvOe38 zjbc+a&u|?LzzU;t{NN^SmdmmZv7X!`Wvg5<k+x*(0-s~?sJJFuu<~`ftY!W}HhTUm zM0g}uv<5J(#;9B?ZphXEOIs4@NWd*C(xoduICAYL`~;V6*{01S>zu&8iEKk&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s-8zo* zNd_sBQwlc2q@=7D?u{XH<^`J!##32B2q|D?C*g<{jK`AEaLCKtrq>9HFfHsMErjIL zM3($Ic*zWL(j`&5U`P9$4H;)e!bH?IhW3+ka>CQmSnj<s8SZ+z&ZmX_b_m@xBsgng z5xSlLWi9EJD6^@l@4g8?T1^WmD4<Q?Q?qYsJR^hfExzBqqDd*VYxvq{#~>_pkjID2 zA_k@{45F^zI!@TcAZlNX0Xow@&n0I}dlQ}(QU}IU%W!3y16yQ_O|)wq;O0Z7-BDH^ z%nKV7CC{zV8fV$udJKgusCMGwM0=?K2ebpKIsmRXO2NSz@CxvVoBq<Z_(04xQ(=Ad zIm^rMSa(}Dgl%?{79E&ww^YrxHD!7u$yi+MzxNF6y#twy$oR<idPFi2HR8lX9Unjj zaHet2SBzy`Xgt*Z{4(-}R@+UBVF}5AObXsHV*cS#%twN+!htWt61fGq7}a=g%VhhS zo9{edGV;MiTbe&5F@y!-0K2$}8@O$$25vYmCYb!^d)tOj0%Fz<NCoiQ#Orv|^ReqT z@ec5=VZYvh{o?l_Aay{m30gj2HbJx9;-7(~F1fqc4fr~c{4y-j^5SXr7W*RUCcZ2^ kuodMyM%rl4Lox4kPR--Fk(YFuv4Lq^5x}zI7|Vh4KfhEH3;+NC diff --git a/src/snipper/block_parsing.py b/src/snipper/block_parsing.py index 27ca1b9..d8996d8 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 d06de2f..b405ad8 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 @@ -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 diff --git a/src/snipper/test.py b/src/snipper/test.py new file mode 100644 index 0000000..233f5f1 --- /dev/null +++ b/src/snipper/test.py @@ -0,0 +1 @@ +import d4rl -- GitLab