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
No related branches found
No related tags found
No related merge requests found
Showing
with 51 additions and 115 deletions
......@@ -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.
```python
"""
"""
def myfun(a,b):
""" The doc-string is not removed. """
# TODO: 1 lines missing.
......@@ -75,8 +73,6 @@ print("and that is a fact!")
```
Is compiled into:
```python
"""
"""
def primes_sieve(limit):
# TODO: 8 lines missing.
raise NotImplementedError("Compute the list `primes` here of all primes up to `limit`")
......@@ -120,44 +116,30 @@ This example will produce three files
```python
# s_tag.py
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("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")
```
and
```python
# s_tag.py
def primes_sieve(limit):
limitn = limit+1
primes = range(2, limitn)
for i in primes:
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)
print("An extra cutout")
def primes_sieve(limit):
limitn = limit+1
primes = range(2, limitn)
primes.remove(f) #!s=b
return primes
```
and finally:
```python
# 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)
return primes
```
and
```python
```
and finally:
```python
```
I recommend using `\inputminted{filename}` to insert the cutouts in LaTeX.
......
......@@ -19,9 +19,6 @@ def my_nup(path):
if __name__ == "__main__":
from snipper.fix_s import save_s
from snipper.snipper_main import censor_file
# from examples.process_cs101_references import main
# main()
data = {}
EX_BASE = "../examples"
np = EX_BASE + "/latex"
......
# s_tag.py
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("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")
\ 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):
# TODO: 8 lines missing.
raise NotImplementedError("Compute the list `primes` here of all primes up to `limit`")
......
"""
"""
def myfun(a,b):
""" The doc-string is not removed. """
# TODO: 1 lines missing.
......
"""
"""
for animal in ["Dog", "cat", "wolf"]:
print("An example of a four legged animal is", animal)
......
"""
"""
if __name__ == "__main__":
print("Here are the first 4 square numbers")
for k in range(1,5):
......
"""
"""
width, height = 2, 4
print("Area of square of width", width, "and height", height, "is:")
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):
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.
def join(contents):
......@@ -30,7 +40,7 @@ def block_split(lines, tag):
i, j = f2(lines, tag)
def get_tag_args(line):
line = line.strip()
# line = line.strip()
k = line.find(" ")
tag_args = ((line[:k + 1] if k >= 0 else line)[len(tag):] ).strip()
......@@ -45,8 +55,10 @@ def block_split(lines, tag):
if i is None:
return None
else:
print( lines[i] )
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
i2, j2 = f2(lines, END_TAG, i=i, j=j+1)
if i2 == None:
......
......@@ -8,6 +8,10 @@ def get_s(lines):
blocks = defaultdict(list)
for c in block_iterate(lines, "#!s"):
blocks[c['name']].append(c)
if len(blocks) > 0:
print("\n".join(lines))
print("asdf")
output = {}
for name, co in blocks.items():
output[name] = [l for c in co for l in c['block']]
......
......@@ -2,12 +2,12 @@ import os
import functools
import textwrap
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_o import run_o
from snipper.legacy import indent, block_process
def fix_r(lines):
for i,l in enumerate(lines):
if "#!r" in l:
......@@ -162,7 +162,6 @@ def censor_file(file, info, run_files=True, run_out_dirs=None, cut_files=True, s
if references == None:
references = {}
from snipper.fix_cite import fix_citations
dbug = False
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
run_o(lines, file=file, output=ofiles[0])
run_i(lines, file=file, output=ofiles[0])
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
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