Skip to content

Commit 285d509

Browse files
committed
Add some practice, not sicp related
1 parent b52bfbd commit 285d509

7 files changed

+110
-2
lines changed

README.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
# sicp-in-python
2-
learning notes on sicp in python: https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book.html
1+
# SICP In Python 学习笔记
2+
3+
重启 SICP 学习,去年学了[Structure and Interpretation of Computer Programs, 2e: Top](file://localhost/Users//Library/Application%20Support/Zotero/Profiles/h6gwndqs.default/zotero/storage/98VGGP3G/index.html)的前两章(未全部完成),后搁置了,期间是夹杂着Python和LISP学的,LISP完全没基础,PYthon也比较小白,数学基础也不甚好,所以学来有些累。后来又因其他事 + 用中学的理念,就彻底放置至今。
4+
5+
但是心中一直念念不忘,总觉得这是一盏指明灯,认真学必会照亮编程的路,而且不仅限于编程。
6+
7+
8+
9+
- [Welcome to the SICP Web Site](https://mitpress.mit.edu/sites/default/files/sicp/index.html)

practice/createGitlabUser.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import requests
2+
3+
token="5rZ5xPpKhXd3YBqZ3m_z"
4+
url = f"https://gitlab.com/api/v4/users?private_token={token}"
5+
6+
data = {
7+
'name': 'vivian',
8+
'username': 'vivian',
9+
'email':'[email protected]',
10+
'password':'debuguself'
11+
}
12+
13+
headers = {
14+
'Content-type':'application/json'
15+
}
16+
resp = requests.post(url, headers=headers, json=data)
17+
# resp = requests.get(url + "&order_by=id")
18+
print(resp.text)
19+
print(resp.status_code)

practice/generator.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def example():
2+
l = [1, 2,3 ,4]
3+
for i in l:
4+
yield i
5+
6+
gen = example()
7+
print(gen)
8+
9+
print(dir(gen))

practice/instaviz_test.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import instaviz
2+
3+
def testt():
4+
a = 1
5+
6+
instaviz.show(testt)

practice/replace_file_content.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
replace certain contents in a file" Originated for Anki import
3+
2019-01-19 init 1h: %
4+
2019-01-19 add line break "---"
5+
6+
"""
7+
import argparse
8+
9+
files = [
10+
#{"file_name":"/Users/wanjia/Flashcard Decks/伤寒论全部条文.md"},
11+
#{"file_name":"/Users/wanjia/Flashcard Decks/金匮要略背诵条文.md"},
12+
# {"file_name":"/Users/wanjia/Flashcard Decks/刘希彦注解伤寒论.md","s1":"原文:","s2":"注解:"}
13+
{"file_name":"/Users/wanjia/Flashcard Decks/医理真传.md","s1":"问曰:","s2":"答曰:"}
14+
]
15+
16+
def replace(text, old, new):
17+
return str.replace(text, old, new)
18+
19+
for dic in files:
20+
file_name = dic["file_name"]
21+
s1 = dic["s1"]
22+
s2 = dic["s2"]
23+
with open(file_name, "r+", encoding="utf-8") as f:
24+
contents = f.readlines()
25+
for index, content in enumerate(contents):
26+
#contents[index] = replace(content, "%", "\n%\n")
27+
contents[index] = replace(content, s1, "---\n"+s1)
28+
contents[index] = replace(contents[index], s2, "%\n"+s2)
29+
print(contents)
30+
f.writelines(contents)
31+
32+
33+
34+
35+
36+

practice/test_tokens.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Hello world!
2+
def my_function():
3+
proceed

practice/trace.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sys
2+
import dis
3+
import traceback
4+
import io
5+
6+
def trace(frame, event, args):
7+
frame.f_trace_opcodes = True
8+
stack = traceback.extract_stack(frame)
9+
pad = " "*len(stack) + "|"
10+
if event == 'opcode':
11+
with io.StringIO() as out:
12+
dis.disco(frame.f_code, frame.f_lasti, file=out)
13+
lines = out.getvalue().split('\n')
14+
[print(f"{pad}{l}") for l in lines]
15+
elif event == 'call':
16+
print(f"{pad}Calling {frame.f_code}")
17+
elif event == 'return':
18+
print(f"{pad}Returning {args}")
19+
elif event == 'line':
20+
print(f"{pad}Changing line to {frame.f_lineno}")
21+
else:
22+
print(f"{pad}{frame} ({event} - {args})")
23+
print(f"{pad}----------------------------------")
24+
return trace
25+
sys.settrace(trace)
26+
27+
# Run some code for a demo
28+
eval('"-".join([letter for letter in "hello"])')

0 commit comments

Comments
 (0)