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

Refactor citation + tags and update README.md

parent 0d610f1f
No related branches found
No related tags found
No related merge requests found
......@@ -295,3 +295,80 @@ def myfun():
```
Since the aux/bibtex databases are just dictionaries it is easy to join them together from different sources.
I have written reference tags to lecture and exercise material as well as my notes and it makes reference management very easy.
## Advanced block processing
The default behavior for code removal (#!b and #!f-tags) is to simply remove the code and insert the number of missing lines.
We can easily create more interesting behavior. The code for the following example can be found in `examples/b_example.py` and will deal with the following problem:
```python
import numpy as np
# Implement a sieve here.
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
# Example use: print(primes_sieve(42))
```
The example shows how we can easily define custom functions for processing the code which is to be removed.
All such a function need is to take a list of lines (to be obfuscated) and return a new list of lines (the obfuscated code).
A couple of short examples:
### Permute lines
This example simple randomly permutes the line and prefix them with a comment tag to ensure the code still compiles
```python
import numpy as np
# Implement a sieve here.
def primes_sieve(limit):
# primes.remove(f)
# if f in primes:
# factors = list(range(i, limitn, i))
# for i in primes:
#
# limitn = limit+1
# return primes
# primes = range(2, limitn)
# for f in factors[1:]:
# Example use: print(primes_sieve(42))
raise NotImplementedError('Complete the above program')
```
### Partial replacement
This example replaces non-keyword, non-special-symbol parts of the lines:
```python
import numpy as np
# Implement a sieve here.
def primes_sieve(limit):
# ?????? = ?????+1
# ?????? = ?????(2, ??????)
#
# for ? in ??????:
# ??????? = ????(?????(?, ??????, ?))
# for ? in ???????[1:]:
# if ? in ??????:
# ??????.??????(?)
# return ??????
# Example use: print(primes_sieve(42))
raise NotImplementedError('Complete the above program')
```
### Half of the solution
The final solution display half of the proposed solution:
```python
import numpy as np
# Implement a sieve here.
def primes_sieve(limit):
# limitn =????????
# primes = ran?????????????
#
# for i in????????
# factors = list(ra??????????????????
# for f in f???????????
# if f in????????
# primes.r????????
# return???????
# Example use: print(primes_sieve(42))
raise NotImplementedError('Complete the above program')
```
\ No newline at end of file
......@@ -154,3 +154,30 @@ And this then produce the output:
```
Since the aux/bibtex databases are just dictionaries it is easy to join them together from different sources.
I have written reference tags to lecture and exercise material as well as my notes and it makes reference management very easy.
## Advanced block processing
The default behavior for code removal (#!b and #!f-tags) is to simply remove the code and insert the number of missing lines.
We can easily create more interesting behavior. The code for the following example can be found in `examples/b_example.py` and will deal with the following problem:
```python
{{ cs101_output.sieve_py }}
```
The example shows how we can easily define custom functions for processing the code which is to be removed.
All such a function need is to take a list of lines (to be obfuscated) and return a new list of lines (the obfuscated code).
A couple of short examples:
### Permute lines
This example simple randomly permutes the line and prefix them with a comment tag to ensure the code still compiles
```python
{{ cs101_output.obscure_1_py }}
```
### Partial replacement
This example replaces non-keyword, non-special-symbol parts of the lines:
```python
{{ cs101_output.obscure_2_py }}
```
### Half of the solution
The final solution display half of the proposed solution:
```python
{{ cs101_output.obscure_3_py }}
```
from snipper.fix_s import get_s #!s
from snipper.fix_s import get_s
from snipper.block_parsing import block_split
import textwrap
import numpy as np
import numpy as np #!s
# Implement a sieve here.
def primes_sieve(limit):
......@@ -14,13 +14,23 @@ def primes_sieve(limit):
if f in primes:
primes.remove(f)
return primes #!b
#!s
def obscure(blk, fun):
# Example use: print(primes_sieve(42)) #!s
def wspace(l):
whitespace = " " * (len(l) - len(l.lstrip()))
return whitespace
def obscure(blk, fun, outfile):
blok = block_split(blk, "#!b")
lines2 = blok['first'] + fun(blok['block']) + blok['last']
lines2.append( wspace(lines2[-1] ) + "raise NotImplementedError('Complete the above program')" )
s = '\n'.join(lines2)
print(s)
with open(outfile, 'w') as f:
f.write(s)
return s
# driver program
......@@ -29,6 +39,9 @@ if __name__ == '__main__':
s = f.read().splitlines()
blk = get_s(s)['']
with open("cs101_output/sieve.py", 'w') as f:
f.write('\n'.join(blk))
def cmnt(lines):
whitespace = " " * (len(lines[0]) - len(lines[0].lstrip()))
lines = textwrap.dedent("\n".join(lines)).splitlines()
......@@ -41,7 +54,7 @@ if __name__ == '__main__':
lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
return lines
obscure(blk, f1)
obscure(blk, f1, 'cs101_output/obscure_1.py')
def f2(lines):
lines, whitespace = cmnt(lines)
......@@ -60,4 +73,13 @@ if __name__ == '__main__':
lines = textwrap.indent("\n".join(lines), whitespace).splitlines()
return lines
obscure(blk, f2)
obscure(blk, f2, 'cs101_output/obscure_2.py')
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
obscure(blk, f3, 'cs101_output/obscure_3.py')
import numpy as np
# Implement a sieve here.
def primes_sieve(limit):
# primes.remove(f)
# if f in primes:
# factors = list(range(i, limitn, i))
# for i in primes:
#
# limitn = limit+1
# return primes
# primes = range(2, limitn)
# for f in factors[1:]:
# Example use: print(primes_sieve(42))
raise NotImplementedError('Complete the above program')
\ No newline at end of file
import numpy as np
# Implement a sieve here.
def primes_sieve(limit):
# ?????? = ?????+1
# ?????? = ?????(2, ??????)
#
# for ? in ??????:
# ??????? = ????(?????(?, ??????, ?))
# for ? in ???????[1:]:
# if ? in ??????:
# ??????.??????(?)
# return ??????
# Example use: print(primes_sieve(42))
raise NotImplementedError('Complete the above program')
\ No newline at end of file
import numpy as np
# Implement a sieve here.
def primes_sieve(limit):
# limitn =????????
# primes = ran?????????????
#
# for i in????????
# factors = list(ra??????????????????
# for f in f???????????
# if f in????????
# primes.r????????
# return???????
# Example use: print(primes_sieve(42))
raise NotImplementedError('Complete the above program')
\ No newline at end of file
import numpy as np
# Implement a sieve here.
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
# Example use: print(primes_sieve(42))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment