Skip to content

Commit 6498cf8

Browse files
committed
add signet
1 parent c5e6407 commit 6498cf8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+8865
-7162
lines changed

apply_patch.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,27 @@
44

55

66
patch = sys.argv[1]
7-
parts = patch.split('/')
7+
parts = patch.split("/")
88
start = int(parts[0][-1])
99
print(start)
1010

11-
if parts[1] == 'complete':
11+
if parts[1] == "complete":
1212
start += 1
1313
patch_file = parts[2]
1414
skip = False
1515
else:
1616
patch_file = parts[1]
1717
skip = True
1818

19-
to_patch = patch_file.split('.')[0] + '.py'
19+
to_patch = patch_file.split(".")[0] + ".py"
2020

2121
for session in range(start, 9):
2222
if skip:
2323
skip = False
2424
else:
25-
filename = 'session{}/{}'.format(session, to_patch)
26-
call('git checkout {}'.format(filename), shell=True)
27-
call('patch -p1 {} < {}'.format(filename, patch), shell=True)
28-
filename = 'session{}/complete/{}'.format(session, to_patch)
29-
call('git checkout {}'.format(filename), shell=True)
30-
call('patch -p1 {} < {}'.format(filename, patch), shell=True)
25+
filename = "session{}/{}".format(session, to_patch)
26+
call("git checkout {}".format(filename), shell=True)
27+
call("patch -p1 {} < {}".format(filename, patch), shell=True)
28+
filename = "session{}/complete/{}".format(session, to_patch)
29+
call("git checkout {}".format(filename), shell=True)
30+
call("patch -p1 {} < {}".format(filename, patch), shell=True)

generate_jupyter.py

+44-42
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,79 @@
66
if len(sessions) == 0:
77
sessions = range(9)
88

9-
FIRST_CELL = '''############## PLEASE RUN THIS CELL FIRST! ###################
9+
FIRST_CELL = """############## PLEASE RUN THIS CELL FIRST! ###################
1010
1111
# import everything and define a test runner function
1212
from importlib import reload
1313
from helper import run
14-
'''
14+
"""
1515

16-
UNITTEST_TEMPLATE_1 = '''### Exercise {exercise_number}
16+
UNITTEST_TEMPLATE_1 = """### Exercise {exercise_number}
1717
1818
{instructions}
1919
20-
#### Make [this test](/edit/{path}/{module}.py) pass: `{module}.py:{test_suite}:{test}`'''
20+
#### Make [this test](/edit/{path}/{module}.py) pass: `{module}.py:{test_suite}:{test}`"""
2121

2222

23-
UNITTEST_TEMPLATE_2 = '''# Exercise {exercise_number}
23+
UNITTEST_TEMPLATE_2 = """# Exercise {exercise_number}
2424
2525
reload({module})
26-
run({module}.{test_suite}('{test}'))'''
26+
run({module}.{test_suite}('{test}'))"""
2727

2828

29-
UNITTEST_TEMPLATE_3 = '''# Exercise {exercise_number}
29+
UNITTEST_TEMPLATE_3 = """# Exercise {exercise_number}
3030
3131
reload({module})
3232
reload(script)
33-
run({module}.{test_suite}('{test}'))'''
33+
run({module}.{test_suite}('{test}'))"""
3434

3535

36-
EXERCISE_TEMPLATE_1 = '''### Exercise {exercise_number}
37-
{instructions}'''
36+
EXERCISE_TEMPLATE_1 = """### Exercise {exercise_number}
37+
{instructions}"""
3838

3939

40-
EXERCISE_TEMPLATE_2 = '''# Exercise {exercise_number}
40+
EXERCISE_TEMPLATE_2 = """# Exercise {exercise_number}
4141
42-
{code}'''
42+
{code}"""
4343

4444

4545
for session in sessions:
4646
notebook = nbformat.v4.new_notebook()
4747
notebook_complete = nbformat.v4.new_notebook()
48-
cells = notebook['cells']
49-
cells_complete = notebook_complete['cells']
50-
path = 'session{}'.format(session)
51-
with open('{}/answers.py'.format(path), 'r') as f:
52-
current = ''
48+
cells = notebook["cells"]
49+
cells_complete = notebook_complete["cells"]
50+
path = "session{}".format(session)
51+
with open("{}/answers.py".format(path), "r") as f:
52+
current = ""
5353
cell_type = None
5454
exercise_number = 1
5555
first_code_cell = True
5656
for l in f:
5757
line = l.strip()
58-
if line in ('#markdown', '#code', '#exercise', '#unittest'):
58+
if line in ("#markdown", "#code", "#exercise", "#unittest"):
5959
cell_type = line[1:]
60-
elif line == '#end{}'.format(cell_type):
61-
if cell_type == 'markdown':
60+
elif line == "#end{}".format(cell_type):
61+
if cell_type == "markdown":
6262
markdown_cell = nbformat.v4.new_markdown_cell(current)
6363
cells.append(markdown_cell)
6464
cells_complete.append(markdown_cell)
65-
elif cell_type == 'code':
65+
elif cell_type == "code":
6666
# only take >>> or ... lines
6767
lines = []
68-
for line in current.split('\n'):
69-
if line.startswith('>>> ') or line.startswith('... '):
70-
line = re.sub(r'\\\\x', r'\\x', line)
68+
for line in current.split("\n"):
69+
if line.startswith(">>> ") or line.startswith("... "):
70+
line = re.sub(r"\\\\x", r"\\x", line)
7171
lines.append(line[4:])
7272
if first_code_cell:
73-
code = FIRST_CELL + '\n'.join(lines)
73+
code = FIRST_CELL + "\n".join(lines)
7474
first_code_cell = False
7575
else:
76-
code = '\n'.join(lines)
76+
code = "\n".join(lines)
7777
code_cell = nbformat.v4.new_code_cell(code)
7878
cells.append(code_cell)
7979
cells_complete.append(code_cell)
80-
elif cell_type == 'exercise':
81-
instructions, code = current.split('---')
80+
elif cell_type == "exercise":
81+
instructions, code = current.split("---")
8282
markdown = EXERCISE_TEMPLATE_1.format(
8383
exercise_number=exercise_number,
8484
instructions=instructions,
@@ -88,31 +88,31 @@
8888
cells_complete.append(markdown_cell)
8989
lines = []
9090
lines_complete = []
91-
for line in code.split('\n'):
92-
if line.startswith('>>> ') or line.startswith('... '):
93-
line = re.sub(r'\\\\x', r'\\x', line)
94-
start_alt = line.find(' #/')
91+
for line in code.split("\n"):
92+
if line.startswith(">>> ") or line.startswith("... "):
93+
line = re.sub(r"\\\\x", r"\\x", line)
94+
start_alt = line.find(" #/")
9595
if start_alt == -1:
9696
lines.append(line[4:])
9797
lines_complete.append(line[4:])
9898
else:
99-
lines.append(line[start_alt+4:])
99+
lines.append(line[start_alt + 4 :])
100100
lines_complete.append(line[4:start_alt])
101101
code_1 = EXERCISE_TEMPLATE_2.format(
102-
code='\n'.join(lines),
102+
code="\n".join(lines),
103103
exercise_number=exercise_number,
104104
)
105105
code_cell_1 = nbformat.v4.new_code_cell(code_1)
106106
cells.append(code_cell_1)
107107
code_2 = EXERCISE_TEMPLATE_2.format(
108-
code='\n'.join(lines_complete),
108+
code="\n".join(lines_complete),
109109
exercise_number=exercise_number,
110110
)
111111
code_cell_2 = nbformat.v4.new_code_cell(code_2)
112112
cells_complete.append(code_cell_2)
113113
exercise_number += 1
114-
elif cell_type == 'unittest':
115-
module, test_suite, test, instructions = current.split(':', 3)
114+
elif cell_type == "unittest":
115+
module, test_suite, test, instructions = current.split(":", 3)
116116
markdown = UNITTEST_TEMPLATE_1.format(
117117
exercise_number=exercise_number,
118118
instructions=instructions,
@@ -124,7 +124,7 @@
124124
markdown_cell = nbformat.v4.new_markdown_cell(markdown)
125125
cells.append(markdown_cell)
126126
cells_complete.append(markdown_cell)
127-
if module == 'op':
127+
if module == "op":
128128
template = UNITTEST_TEMPLATE_3
129129
else:
130130
template = UNITTEST_TEMPLATE_2
@@ -139,9 +139,11 @@
139139
cells.append(code_cell)
140140
cells_complete.append(code_cell)
141141
exercise_number += 1
142-
current = ''
142+
current = ""
143143
cell_type = None
144144
elif cell_type:
145-
current += line + '\n'
146-
nbformat.write(notebook, '{}/session{}.ipynb'.format(path, session))
147-
nbformat.write(notebook_complete, '{}/complete/session{}.ipynb'.format(path, session))
145+
current += line + "\n"
146+
nbformat.write(notebook, "{}/session{}.ipynb".format(path, session))
147+
nbformat.write(
148+
notebook_complete, "{}/complete/session{}.ipynb".format(path, session)
149+
)

remake.py

+20-16
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@
33

44

55
for i in range(8):
6-
current = 'session{}'.format(i)
7-
branch = 's{}'.format(i)
8-
output = subprocess.check_output(['git', 'ls-tree', '-r', '--name-only', current]).decode('ascii')
6+
current = "session{}".format(i)
7+
branch = "s{}".format(i)
8+
output = subprocess.check_output(
9+
["git", "ls-tree", "-r", "--name-only", current]
10+
).decode("ascii")
911
for filename in output.split():
10-
if filename.endswith(('py','ipynb')) and filename != 'redo.py':
11-
if filename.endswith('ipynb') \
12-
and not filename.startswith(current):
12+
if filename.endswith(("py", "ipynb")) and filename != "redo.py":
13+
if filename.endswith("ipynb") and not filename.startswith(current):
1314
continue
14-
with open('{}/{}'.format(current, filename), 'wb') as f:
15-
contents = subprocess.check_output(['git', 'show', '{}:{}'.format(branch, filename)])
15+
with open("{}/{}".format(current, filename), "wb") as f:
16+
contents = subprocess.check_output(
17+
["git", "show", "{}:{}".format(branch, filename)]
18+
)
1619
f.write(contents)
17-
output = subprocess.check_output(['git', 'ls-tree', '-r', '--name-only', '{}-c'.format(branch)]).decode('ascii')
20+
output = subprocess.check_output(
21+
["git", "ls-tree", "-r", "--name-only", "{}-c".format(branch)]
22+
).decode("ascii")
1823
for filename in output.split():
19-
if filename.endswith(('py','ipynb')) and filename != 'redo.py':
20-
if filename.endswith('ipynb') \
21-
and not filename.startswith(current):
24+
if filename.endswith(("py", "ipynb")) and filename != "redo.py":
25+
if filename.endswith("ipynb") and not filename.startswith(current):
2226
continue
23-
with open('{}/complete/{}'.format(current, filename), 'wb') as f:
24-
contents = subprocess.check_output(['git', 'show', '{}-c:{}'.format(branch, filename)])
27+
with open("{}/complete/{}".format(current, filename), "wb") as f:
28+
contents = subprocess.check_output(
29+
["git", "show", "{}-c:{}".format(branch, filename)]
30+
)
2531
f.write(contents)
26-
27-

session0/answers.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'''
1+
"""
22
#code
33
>>> import helper
44
@@ -131,7 +131,7 @@
131131
Indexing bytes will get you the numerical value:
132132
133133
```python
134-
print(b'&'[0]) # 38 since & is charcter #38
134+
print(b'&'[0]) # 38 since & is character #38
135135
```
136136
137137
You can do the reverse by using bytes:
@@ -152,7 +152,7 @@
152152
>>> print(b[::-1]) # b'dlrow olleh'
153153
b'dlrow olleh'
154154
>>>
155-
>>> print(b'&'[0]) # 38 since & charcter #38
155+
>>> print(b'&'[0]) # 38 since & character #38
156156
38
157157
>>>
158158
>>> print(bytes([38])) # b'&'
@@ -302,29 +302,31 @@
302302
helper:HelperTest:test_int_to_little_endian:
303303
Similarly, we'll want to do the inverse operation, so write a function that will convert an integer to little-endian bytes given the number and the number of bytes it should take up.
304304
#endunittest
305-
'''
305+
"""
306306

307307

308308
from unittest import TestCase
309309

310310
import helper
311311

312312

313-
def bytes_to_str(b, encoding='ascii'):
313+
def bytes_to_str(b, encoding="ascii"):
314314
return b.decode(encoding)
315315

316-
def str_to_bytes(s, encoding='ascii'):
316+
317+
def str_to_bytes(s, encoding="ascii"):
317318
return s.encode(encoding)
318319

320+
319321
def little_endian_to_int(b):
320-
return int.from_bytes(b, 'little')
322+
return int.from_bytes(b, "little")
323+
321324

322325
def int_to_little_endian(n, length):
323-
return n.to_bytes(length, 'little')
326+
return n.to_bytes(length, "little")
324327

325328

326329
class SessionTest(TestCase):
327-
328330
def test_apply(self):
329331
helper.bytes_to_str = bytes_to_str
330332
helper.str_to_bytes = str_to_bytes

session0/complete/helper.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,51 @@ def run(test):
77
TextTestRunner().run(suite)
88

99

10-
def bytes_to_str(b, encoding='ascii'):
11-
'''Returns a string version of the bytes'''
10+
def bytes_to_str(b, encoding="ascii"):
11+
"""Returns a string version of the bytes"""
1212
# use the bytes.decode(encoding) method
1313
return b.decode(encoding)
1414

1515

16-
def str_to_bytes(s, encoding='ascii'):
17-
'''Returns a bytes version of the string'''
16+
def str_to_bytes(s, encoding="ascii"):
17+
"""Returns a bytes version of the string"""
1818
# use the string.encode(encoding) method
1919
return s.encode(encoding)
2020

2121

2222
def little_endian_to_int(b):
23-
'''little_endian_to_int takes byte sequence as a little-endian number.
24-
Returns an integer'''
23+
"""little_endian_to_int takes byte sequence as a little-endian number.
24+
Returns an integer"""
2525
# use the int.from_bytes(b, <endianness>) method
26-
return int.from_bytes(b, 'little')
26+
return int.from_bytes(b, "little")
2727

2828

2929
def int_to_little_endian(n, length):
30-
'''endian_to_little_endian takes an integer and returns the little-endian
31-
byte sequence of length'''
30+
"""endian_to_little_endian takes an integer and returns the little-endian
31+
byte sequence of length"""
3232
# use the int.to_bytes(length, <endianness>) method
33-
return n.to_bytes(length, 'little')
33+
return n.to_bytes(length, "little")
3434

3535

3636
class HelperTest(TestCase):
37-
3837
def test_bytes(self):
39-
b = b'hello world'
40-
s = 'hello world'
38+
b = b"hello world"
39+
s = "hello world"
4140
self.assertEqual(b, str_to_bytes(s))
4241
self.assertEqual(s, bytes_to_str(b))
4342

4443
def test_little_endian_to_int(self):
45-
h = bytes.fromhex('99c3980000000000')
44+
h = bytes.fromhex("99c3980000000000")
4645
want = 10011545
4746
self.assertEqual(little_endian_to_int(h), want)
48-
h = bytes.fromhex('a135ef0100000000')
47+
h = bytes.fromhex("a135ef0100000000")
4948
want = 32454049
5049
self.assertEqual(little_endian_to_int(h), want)
5150

5251
def test_int_to_little_endian(self):
5352
n = 1
54-
want = b'\x01\x00\x00\x00'
53+
want = b"\x01\x00\x00\x00"
5554
self.assertEqual(int_to_little_endian(n, 4), want)
5655
n = 10011545
57-
want = b'\x99\xc3\x98\x00\x00\x00\x00\x00'
56+
want = b"\x99\xc3\x98\x00\x00\x00\x00\x00"
5857
self.assertEqual(int_to_little_endian(n, 8), want)

0 commit comments

Comments
 (0)