Skip to content
Snippets Groups Projects
Commit 37700e58 authored by tuhe's avatar tuhe
Browse files

updates

parent 8c3decca
Branches
No related tags found
No related merge requests found
@online{codesnipper,
title={Codesnipper (0.1.0): \texttt{pip install codesnipper}},
title={Codesnipper (0.1.7): \texttt{pip install codesnipper}},
url={https://lab.compute.dtu.dk/tuhe/snipper},
urldate = {2021-09-07},
urldate = {2022-05-19},
month={9},
publisher={Technical University of Denmark (DTU)},
author={Tue Herlau},
year={2021},
year={2022},
}
\ No newline at end of file
def primes_sieve(limit):
limitn = limit+1 #!b
primes = range(2, limitn)
for i in primes:
factors = list(range(i, limitn, i))
for f in factors[1:]:
if f in primes:
primes.remove(f)
return primes #!b
def primes_sieve(limit):
limitn = limit+1 #!b;
primes = range(2, limitn)
for i in primes:
factors = list(range(i, limitn, i))
for f in factors[1:]:
if f in primes:
primes.remove(f)
return primes #!b
def primes_sieve(limit):
limitn = limit+1 #!b
primes = range(2, limitn)
for i in primes:
factors = list(range(i, limitn, i))
for f in factors[1:]:
if f in primes:
primes.remove(f)
return primes #!b
def primes_sieve(limit):
limitn = limit+1 #!b
primes = range(2, limitn)
for i in primes:
factors = list(range(i, limitn, i))
for f in factors[1:]:
if f in primes:
primes.remove(f)
return primes #!b
# Use this guide:
# Use: pipreqs.exe slider --no-pin --force for requirements.txt
# Use: pipreqs.exe slider --no-pin --force for requirements_pip.txt
# https://packaging.python.org/tutorials/packaging-projects/
# py -m build && twine upload dist/*
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -7,6 +7,9 @@ from snipper.block_parsing import block_split, block_join
def fix_f(lines, debug, keep=False):
lines2 = []
i = 0
# if "GymPacmanEnvironment" in lines[0]:
# print("\n".join(lines))
# z = 324
while i < len(lines):
l = lines[i]
dx = l.find("#!f")
......@@ -35,15 +38,18 @@ def fix_f(lines, debug, keep=False):
for c in comments:
lines2 += c.split("\n")
# print(funrem)
f = [id + l.strip() for l in funrem.splitlines()]
# f = [id + l.strip() for l in funrem.splitlines()] # note sure. mangles function body.
f = [l.rstrip() for l in funrem.splitlines()]
f[0] = f[0] + "#!b"
errm = l_head if len(l_head) > 0 else "Implement function body"
""" If function body is more than 1 line long and ends with a return we keep the return. """
if keep or (len( funrem.strip().splitlines() ) == 1 or not f[-1].strip().startswith("return ")):
f[-1] = f[-1] + f' #!b {errm}'
f[-1] = f[-1] + f' #!b {errm}' # Indent of last line may be wrong.
else:
f[-2] = f[-2] + f' #!b {errm}'
f[-1] = id + f[-1].lstrip() # Fix indent of last line to match function scope
lines2 += f
i = len(lines2)
else:
......@@ -92,9 +98,10 @@ 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}
cutout += b['block']
cutout.append( b['block'] )
b['block'], dn = _block_fun(b['block'], start_extra=b['arg1'], end_extra=b['arg2'], **args, keep=keep)
lines = block_join(b)
# cutout +_=
n += dn
# lines2, _, _, cutout = block_process(lines, tag="#!b", block_fun=functools.partial(block_fun, stats=stats))
......
......@@ -31,6 +31,8 @@ def fix_bibtex(lines, bibtex):
if i < 0:
break
if reference not in bibtex:
for k in bibtex:
print(k)
raise IndexError("no such reference: " + reference)
ref = bibtex[reference]
label = ref['label']
......
......@@ -32,6 +32,4 @@ def run_o(lines, file, output):
cmd = "python " + file_run
import subprocess
s = subprocess.check_output(cmd, shell=True)
# s,ok = execute_command(cmd.split(), shell=True)
print(s)
os.remove(file_run)
\ No newline at end of file
......@@ -4,8 +4,9 @@ from pathlib import Path
import time
import fnmatch
import tempfile
from line_profiler_pycharm import profile
@profile
def snip_dir(source_dir, # Sources
dest_dir=None, # Will write to this directory
output_dir=None, # Where snippets are going to be stored
......@@ -14,8 +15,10 @@ def snip_dir(source_dir, # Sources
run_files=True, # Run #!o tags and #!i tags
cut_files=True, # censor files.
license_head=None,
censor_files=True,
):
print("Snipper fixing", source_dir)
if dest_dir == None:
dest_dir = tempfile.mkdtemp()
......@@ -42,17 +45,17 @@ def snip_dir(source_dir, # Sources
if clean_destination_dir:
shutil.rmtree(dest_dir)
os.makedirs(dest_dir)
os.makedirs(dest_dir)
out = dest_dir
hw = {'base': source_dir}
print(f"[snipper] Synchronizing directories: {hw['base']} -> {out}")
print(f"[snipper]: {hw['base']} -> {out}")
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
shutil.copytree(source_dir, dest_dir)
time.sleep(0.1)
time.sleep(0.02)
ls = list(Path(dest_dir).glob('**/*'))
acceptable = []
......@@ -62,21 +65,26 @@ def snip_dir(source_dir, # Sources
acceptable.append( (l, not any(m) ))
n = 0
cutouts = {}
for f, accept in acceptable:
if os.path.isdir(f) or not str(f).endswith(".py") or str(f).endswith("_grade.py"):
continue
if accept:
solution_list = []
kwargs = {}
nrem = censor_file(f, run_files=run_files, run_out_dirs=output_dir, cut_files=cut_files,
solution_list=solution_list,
print("Snipper processing", f)
nrem, cut = censor_file(f, run_files=run_files, run_out_dirs=output_dir, cut_files=cut_files,
# solution_list=solution_list,
base_path=dest_dir,
references=references,
license_head=license_head,
censor_files=censor_files,
**kwargs)
if nrem > 0:
print(f"{nrem}> {f}")
cutouts[str(f)] = cut
n += nrem
# print("[snipper]", "removing")
for rm_file, accept in acceptable:
rm_file = os.path.abspath(rm_file)
if not accept:
......@@ -85,4 +93,5 @@ def snip_dir(source_dir, # Sources
else:
if os.path.isdir(rm_file+"/"):
shutil.rmtree(rm_file)
return n
print("[snipper]", "done")
return n, cutouts
......@@ -27,7 +27,8 @@ def censor_code(lines, keep=True):
return lines
def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, solution_list=None,
def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True,
output_solution=False,
censor_files=True,
base_path=None,
strict=True,
......@@ -37,6 +38,7 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, solutio
if references == None:
references = {}
cut = []
dbug = False
with open(file, 'r', encoding='utf8') as f:
s = f.read()
......@@ -69,31 +71,13 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, solutio
if censor_files:
lines = fix_f(lines, dbug)
if "pacman" in str(file):
print("sdaf")
lines, nB, cut = fix_b(lines)
else:
nB = 0
lines = fix_r(lines)
# if censor_files and len(cut) > 0 and solution_list is not None:
# # fname = file.__str__()
# # i = fname.find("irlc")
# # wk = fname[i+5:fname.find("\\", i+6)]
# # sp = paths['02450students'] +"/solutions/"
# # if not os.path.exists(sp):
# # os.mkdir(sp)
# # sp = sp + wk
# # if not os.path.exists(sp):
# # os.mkdir(sp)
# sols = []
# stext = ["\n".join(lines) for lines in cut]
# # for i,sol in enumerate(stext):
# # # sols.append( (sol,) )
# # # sout = sp + f"/{os.path.basename(fname)[:-3]}_TODO_{i+1}.py"
# # # wsol = any([True for s in solution_list if os.path.basename(sout).startswith(s)])
# # # print(sout, "(published)" if wsol else "")
# # # if wsol:
# # # with open(sout, "w") as f:
# # # f.write(sol)
if len(lines) > 0 and len(lines[-1])>0:
lines.append("")
......@@ -104,7 +88,7 @@ def censor_file(file, run_files=True, run_out_dirs=None, cut_files=True, solutio
with open(file, 'w', encoding='utf-8') as f:
f.write(s2)
return nB
return nB, cut
def fix_copyright(s, license_head):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment