Skip to content
Snippets Groups Projects
Commit 0d610f1f authored by tuhe's avatar tuhe
Browse files

Refactor citation + tags and update README.md

parent 401b3338
Branches
No related tags found
No related merge requests found
Showing
with 51 additions and 115 deletions
...@@ -47,8 +47,6 @@ if __name__ == "__main__": ...@@ -47,8 +47,6 @@ if __name__ == "__main__":
``` ```
The output can be found in `examples/students/f_tag.py`. It will cut out the body of the function but leave any return statement and docstrings. It will also raise an exception (and print how many lines are missing) to help students. The output can be found in `examples/students/f_tag.py`. It will cut out the body of the function but leave any return statement and docstrings. It will also raise an exception (and print how many lines are missing) to help students.
```python ```python
"""
"""
def myfun(a,b): def myfun(a,b):
""" The doc-string is not removed. """ """ The doc-string is not removed. """
# TODO: 1 lines missing. # TODO: 1 lines missing.
...@@ -75,8 +73,6 @@ print("and that is a fact!") ...@@ -75,8 +73,6 @@ print("and that is a fact!")
``` ```
Is compiled into: Is compiled into:
```python ```python
"""
"""
def primes_sieve(limit): def primes_sieve(limit):
# TODO: 8 lines missing. # TODO: 8 lines missing.
raise NotImplementedError("Compute the list `primes` here of all primes up to `limit`") raise NotImplementedError("Compute the list `primes` here of all primes up to `limit`")
...@@ -120,44 +116,30 @@ This example will produce three files ...@@ -120,44 +116,30 @@ This example will produce three files
```python ```python
# s_tag.py # s_tag.py
print("Area of square of width", width, "and height", height, "is:") print("Area of square of width", width, "and height", height, "is:")
print(width*height) #!s # This is an example of a simple cutout
print("and that is a fact!")
print("An extra cutout") #!s #!s # This will be added to the above cutout
def primes_sieve(limit):
print(width*height) print(width*height)
print("and that is a fact!")
print("An extra cutout") #!s #!s # This will be added to the above cutout
def primes_sieve(limit):
limitn = limit+1
primes = range(2, limitn)
for i in primes:
print("An extra cutout") print("An extra cutout")
```
and
```python
# s_tag.py
def primes_sieve(limit): def primes_sieve(limit):
limitn = limit+1 limitn = limit+1
primes = range(2, limitn) primes = range(2, limitn)
for i in primes: for i in primes: #!s=b A nested/named cutout.
factors = list(range(i, limitn, i)) factors = list(range(i, limitn, i))
for f in factors[1:]: for f in factors[1:]:
if f in primes: if f in primes:
primes.remove(f) primes.remove(f) #!s=b
print("An extra cutout") return primes
def primes_sieve(limit): ```
limitn = limit+1 and finally:
primes = range(2, limitn) ```python
# s_tag.py
for i in primes: for i in primes:
factors = list(range(i, limitn, i)) factors = list(range(i, limitn, i))
for f in factors[1:]: for f in factors[1:]:
if f in primes: if f in primes:
primes.remove(f) primes.remove(f)
return primes
```
and
```python
```
and finally:
```python
``` ```
I recommend using `\inputminted{filename}` to insert the cutouts in LaTeX. I recommend using `\inputminted{filename}` to insert the cutouts in LaTeX.
......
...@@ -19,9 +19,6 @@ def my_nup(path): ...@@ -19,9 +19,6 @@ def my_nup(path):
if __name__ == "__main__": if __name__ == "__main__":
from snipper.fix_s import save_s from snipper.fix_s import save_s
from snipper.snipper_main import censor_file from snipper.snipper_main import censor_file
# from examples.process_cs101_references import main
# main()
data = {} data = {}
EX_BASE = "../examples" EX_BASE = "../examples"
np = EX_BASE + "/latex" np = EX_BASE + "/latex"
......
# s_tag.py # s_tag.py
print("Area of square of width", width, "and height", height, "is:") print("Area of square of width", width, "and height", height, "is:")
print(width*height) #!s # This is an example of a simple cutout
print("and that is a fact!")
print("An extra cutout") #!s #!s # This will be added to the above cutout
def primes_sieve(limit):
print(width*height) print(width*height)
print("and that is a fact!")
print("An extra cutout") #!s #!s # This will be added to the above cutout
def primes_sieve(limit):
limitn = limit+1
primes = range(2, limitn)
for i in primes:
print("An extra cutout") print("An extra cutout")
\ No newline at end of file
def primes_sieve(limit):
limitn = limit+1
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)
print("An extra cutout")
def primes_sieve(limit):
limitn = limit+1
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
\ No newline at end of file
# s_tag.py
def primes_sieve(limit):
limitn = limit+1
primes = range(2, limitn)
for i in primes: #!s=b A nested/named cutout.
factors = list(range(i, limitn, i))
for f in factors[1:]:
if f in primes:
primes.remove(f) #!s=b
return primes
\ No newline at end of file
# s_tag.py
for i in primes:
factors = list(range(i, limitn, i))
for f in factors[1:]:
if f in primes:
primes.remove(f)
\ No newline at end of file
"""
"""
def primes_sieve(limit): def primes_sieve(limit):
# TODO: 8 lines missing. # TODO: 8 lines missing.
raise NotImplementedError("Compute the list `primes` here of all primes up to `limit`") raise NotImplementedError("Compute the list `primes` here of all primes up to `limit`")
......
"""
"""
def myfun(a,b): def myfun(a,b):
""" The doc-string is not removed. """ """ The doc-string is not removed. """
# TODO: 1 lines missing. # TODO: 1 lines missing.
......
"""
"""
for animal in ["Dog", "cat", "wolf"]: for animal in ["Dog", "cat", "wolf"]:
print("An example of a four legged animal is", animal) print("An example of a four legged animal is", animal)
......
"""
"""
if __name__ == "__main__": if __name__ == "__main__":
print("Here are the first 4 square numbers") print("Here are the first 4 square numbers")
for k in range(1,5): for k in range(1,5):
......
"""
"""
width, height = 2, 4 width, height = 2, 4
print("Area of square of width", width, "and height", height, "is:") print("Area of square of width", width, "and height", height, "is:")
print(width*height) print(width*height)
......
"""
References:
[Ber07] Dimitri P. Bertsekas. Dynamic Programming and Optimal Control, Vol. II. Athena Scientific, 3rd edition, 2007. ISBN 1886529302.
[Her21] Tue Herlau. Sequential decision making. (See 02465_Notes.pdf), 2021.
"""
def myfun(): #!s
"""
To solve this exercise, look at eq. (1) in Section 1.
You can also look at (Ber07) and (Her21)
More specifically, look at (Ber07, Equation 117) and (Her21, Figure 1)
We can also write a special tag to reduce repetition: (Her21, Figure 1) and (Her21, Section 1).
"""
return 42 #!s
\ No newline at end of file
# load_references.py
from snipper.load_citations import get_bibtex, get_aux
bibfile = "latex/library.bib"
auxfile = 'latex/index.aux'
bibtex = get_bibtex(bibfile)
aux = get_aux(auxfile)
\ No newline at end of file
# load_references.py
file = "citations.py"
with open(file, 'r') as f:
lines = f.read().splitlines()
lines = fix_aux(lines, aux=aux)
lines = fix_aux_special(lines, aux=aux, command='\\nref', bibref='herlau')
lines = fix_bibtex(lines, bibtex=bibtex)
with open('output/citations.py', 'w') as f:
f.write("\n".join(lines))
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
...@@ -21,6 +21,16 @@ def block_iterate(lines, tag): ...@@ -21,6 +21,16 @@ def block_iterate(lines, tag):
def block_split(lines, tag): def block_split(lines, tag):
# print("\n".join(lines))
# if tag != "#!s":
# return {}
if tag =="#!s" and "#!s" in "".join(lines):
print("tag is", tag)
# else:
# return dict(name="asdfasdf", joined='')
z = 234
stag = tag[:2] # Start of any next tag. stag = tag[:2] # Start of any next tag.
def join(contents): def join(contents):
...@@ -30,7 +40,7 @@ def block_split(lines, tag): ...@@ -30,7 +40,7 @@ def block_split(lines, tag):
i, j = f2(lines, tag) i, j = f2(lines, tag)
def get_tag_args(line): def get_tag_args(line):
line = line.strip() # line = line.strip()
k = line.find(" ") k = line.find(" ")
tag_args = ((line[:k + 1] if k >= 0 else line)[len(tag):] ).strip() tag_args = ((line[:k + 1] if k >= 0 else line)[len(tag):] ).strip()
...@@ -45,8 +55,10 @@ def block_split(lines, tag): ...@@ -45,8 +55,10 @@ def block_split(lines, tag):
if i is None: if i is None:
return None return None
else: else:
print( lines[i] )
start_tag_args = get_tag_args(lines[i][j:]) start_tag_args = get_tag_args(lines[i][j:])
START_TAG = f"{tag}={start_tag_args['']}" if '' in start_tag_args else tag START_TAG = f"{tag}={start_tag_args['']}" if start_tag_args[''] != '' else tag
END_TAG = START_TAG END_TAG = START_TAG
i2, j2 = f2(lines, END_TAG, i=i, j=j+1) i2, j2 = f2(lines, END_TAG, i=i, j=j+1)
if i2 == None: if i2 == None:
......
...@@ -8,6 +8,10 @@ def get_s(lines): ...@@ -8,6 +8,10 @@ def get_s(lines):
blocks = defaultdict(list) blocks = defaultdict(list)
for c in block_iterate(lines, "#!s"): for c in block_iterate(lines, "#!s"):
blocks[c['name']].append(c) blocks[c['name']].append(c)
if len(blocks) > 0:
print("\n".join(lines))
print("asdf")
output = {} output = {}
for name, co in blocks.items(): for name, co in blocks.items():
output[name] = [l for c in co for l in c['block']] output[name] = [l for c in co for l in c['block']]
......
...@@ -2,12 +2,12 @@ import os ...@@ -2,12 +2,12 @@ import os
import functools import functools
import textwrap import textwrap
import re import re
# from snipper.fix_s import save_s from snipper.fix_s import save_s
from snipper.fix_cite import fix_citations
from snipper.fix_bf import fix_f, fix_b2 from snipper.fix_bf import fix_f, fix_b2
from snipper.fix_o import run_o from snipper.fix_o import run_o
from snipper.legacy import indent, block_process from snipper.legacy import indent, block_process
def fix_r(lines): def fix_r(lines):
for i,l in enumerate(lines): for i,l in enumerate(lines):
if "#!r" in l: if "#!r" in l:
...@@ -162,7 +162,6 @@ def censor_file(file, info, run_files=True, run_out_dirs=None, cut_files=True, s ...@@ -162,7 +162,6 @@ def censor_file(file, info, run_files=True, run_out_dirs=None, cut_files=True, s
if references == None: if references == None:
references = {} references = {}
from snipper.fix_cite import fix_citations
dbug = False dbug = False
with open(file, 'r', encoding='utf8') as f: with open(file, 'r', encoding='utf8') as f:
...@@ -194,7 +193,6 @@ def censor_file(file, info, run_files=True, run_out_dirs=None, cut_files=True, s ...@@ -194,7 +193,6 @@ def censor_file(file, info, run_files=True, run_out_dirs=None, cut_files=True, s
run_o(lines, file=file, output=ofiles[0]) run_o(lines, file=file, output=ofiles[0])
run_i(lines, file=file, output=ofiles[0]) run_i(lines, file=file, output=ofiles[0])
if cut_files: if cut_files:
from snipper.fix_s import save_s
save_s(lines, file_path=os.path.relpath(file, base_path), output_dir=run_out_dirs) # save file snips to disk save_s(lines, file_path=os.path.relpath(file, base_path), output_dir=run_out_dirs) # save file snips to disk
lines = full_strip(lines, ["#!s", "#!o", '#!i']) lines = full_strip(lines, ["#!s", "#!o", '#!i'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment