Skip to content

Commit 48b3634

Browse files
committed
update
1 parent 2da8659 commit 48b3634

10 files changed

Lines changed: 222 additions & 45 deletions

File tree

github-toc-maker-for-sphinx.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,65 @@
11
import os
22
import re
3+
import git
4+
import linecache
35
from glob import glob
46

57
pwd = os.getcwd()
6-
all_chapters_path = glob(pwd + "/source/c0*") + glob(pwd + "/source/c1*")
7-
8+
source_dir = os.path.join(pwd, "source")
89

910
def get_chapter_name(file):
10-
with open(file) as f:
11-
f.readline()
12-
chapter_name = f.readline().strip()
13-
14-
return chapter_name
11+
return linecache.getline(file, 2).strip()
1512

1613
def get_title(file):
17-
with open(file) as f:
18-
first_line = f.readline()
14+
first_line = linecache.getline(file, 1)
1915

2016
if first_line.startswith("#"):
21-
return first_line[1:].strip()
17+
return first_line.replace("# ", "").strip()
18+
19+
def get_current_brance():
20+
repo = git.Repo(pwd)
21+
all_branchs = repo.git.branch()
22+
23+
current_branch = ""
24+
for branch in all_branchs.split('\n'):
25+
if "*" in branch:
26+
current_branch = branch.replace("* ", "")
27+
if current_branch == "master":
28+
current_branch = "latest"
29+
break
30+
31+
return current_branch
32+
33+
def get_all_chapter():
34+
all_chapters_path = []
35+
os.chdir(source_dir)
36+
37+
for dir_name in glob("c*"):
38+
if dir_name == "chapters" or dir_name == "conf.py":
39+
continue
40+
all_chapters_path.append(os.path.join(dir_name))
41+
return all_chapters_path
2242

23-
def generate_mapping():
43+
def generate_mapping(all_chapters_path):
2444
mapping = dict.fromkeys([os.path.basename(chapter_path) for chapter_path in all_chapters_path])
2545
for key in mapping.keys():
2646
chapter_file = os.path.join(pwd, "source", "chapters", key.replace("c", "p") + ".rst")
2747
mapping[key] = get_chapter_name(chapter_file)
2848

2949
return mapping
3050

31-
def get_toc_info():
51+
def get_toc_info(all_chapters_path, current_branch):
3252
toc = {}
33-
for dir_path in all_chapters_path:
34-
dir_name = os.path.basename(dir_path)
3553

54+
for dir_name in all_chapters_path:
3655
chapter_toc = {}
37-
files = glob(dir_path + "/*.md")
56+
os.chdir(os.path.join(source_dir, dir_name))
3857

39-
for file in files:
40-
file_name = os.path.basename(file)
58+
for file_name in sorted(glob(dir_name + "*.md")):
4159
section = int(re.findall(r"c\d{2}_(\d{2}).md", file_name)[0])
4260

43-
#md_path = os.path.join("./source/", dir_name, file_name)
44-
md_path = os.path.join("http://python.iswbm.com/en/latest/", dir_name, file_name.replace("md", "html"))
45-
title = get_title(file)
61+
md_path = os.path.join("http://pycharm.iswbm.com/zh_CN/", current_branch, dir_name, file_name.replace("md", "html"))
62+
title = get_title(file_name)
4663
if not title:
4764
continue
4865

@@ -63,8 +80,10 @@ def print_md_toc(toc_info, mapping):
6380
print(" ", f"* [{post[1][0]}]({post[1][1]})")
6481

6582
def main():
66-
mapping = generate_mapping()
67-
toc_info = get_toc_info()
83+
all_chapter = get_all_chapter()
84+
mapping = generate_mapping(all_chapter)
85+
current_branch = get_current_brance()
86+
toc_info = get_toc_info(all_chapter, current_branch)
6887
print_md_toc(toc_info, mapping)
6988

7089
if __name__ == '__main__':

source/c02/c02_01.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ NameError: name 'age' is not defined
104104

105105
理解变量在计算机内存中的表示也非常重要。
106106

107-
当我们写:`name = "Jack"` 时,Python解释器干了两件事情:
107+
当我们写:`a = "Jack"` 时,Python解释器干了两件事情:
108108

109-
1. 在内存中创建了一个‘ABC’的字符串对象;
109+
1. 在内存中创建了一个`'Jack'`的字符串对象;
110110

111111
2. 在内存中创建了一个名为a的变量,并把它指向 `'Jack'`
112112

@@ -127,7 +127,7 @@ NameError: name 'age' is not defined
127127
4332916664
128128
```
129129

130-
通过` id()` 可以查看变量值的内存地址,打印出来的 namename_bak 的内存地址是一样的,因此二者其实是一个数据。
130+
通过` id()` 可以查看变量值的内存地址,打印出来的 ab的内存地址是一样的,因此二者其实是一个数据。
131131

132132
但如果继续对 a 进行赋值其他值, 会发现 a 的内存地址变了,而 b 的并没有变
133133

source/c02/c02_01.rst

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ Python 中 用 ``=`` 号来给变量赋值,比如下面这个表达式,age
116116

117117
理解变量在计算机内存中的表示也非常重要。
118118

119-
当我们写:\ ``name = "Jack"`` 时,Python解释器干了两件事情:
119+
当我们写:\ ``a = "Jack"`` 时,Python解释器干了两件事情:
120120

121-
1. 在内存中创建了一个‘ABC’的字符串对象;
121+
1. 在内存中创建了一个\ ``'Jack'``\ 的字符串对象;
122122

123123
2. 在内存中创建了一个名为a的变量,并把它指向 ``'Jack'``\
124124

@@ -137,8 +137,8 @@ Python 中 用 ``=`` 号来给变量赋值,比如下面这个表达式,age
137137
>>> id(b)
138138
4332916664
139139
140-
通过\ ``id()`` 可以查看变量值的内存地址,打印出来的 name 和 name_bak
141-
的内存地址是一样的,因此二者其实是一个数据。
140+
通过\ ``id()`` 可以查看变量值的内存地址,打印出来的 a 和
141+
b的内存地址是一样的,因此二者其实是一个数据。
142142

143143
但如果继续对 a 进行赋值其他值, 会发现 a 的内存地址变了,而 b 的并没有变
144144

@@ -160,10 +160,7 @@ Python 中 用 ``=`` 号来给变量赋值,比如下面这个表达式,age
160160
- 执行a =
161161
‘Tom’,解释器创建字符串‘Tom’对象,并把a改为指向‘Tom’对象,与b无关。
162162

163-
.. figure:: https://img2020.cnblogs.com/blog/1762677/202010/1762677-20201007160345007-1420712915.png
164-
:alt: image.png-43.8kB
165-
166-
image.png-43.8kB
163+
|image0|
167164

168165
6. 简单介绍常量
169166
---------------
@@ -181,3 +178,6 @@ Python 中 用 ``=`` 号来给变量赋值,比如下面这个表达式,age
181178
但事实上,从Python语法角度看,PI仍然是一个变量,因为Python根本没有任何机制保证PI不会被改变。你完全可以给PI赋值为10,不会弹出任何错误。所以,用全部大写的变量名表示常量只是一个习惯上的用法。
182179

183180
常量通常放置在代码的最上部,并作为全局使用。
181+
182+
.. |image0| image:: http://image.iswbm.com/20210116171300.png
183+

source/c04/c04_02.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,4 @@ for 元素 in 序列对象:
103103
... print("循环非常正常")
104104
...
105105
当前的数是 0
106-
```
106+
```

source/c04/c04_04.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,5 @@ for i in range(1, 10):
165165
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
166166
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
167167
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
168-
```
168+
```
169+

source/c09/c09_05.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ if __name__ == '__main__':
4141
做为脚本调用,方法很简单,就像正常执行python脚本一样,只是多加了`-m pdb`
4242

4343
```
44-
ptyhon -m pdb pdb_demo.py
44+
python -m pdb pdb_demo.py
4545
```
4646

4747
使用这个方式进入调试模式,会在脚本的第一行开始单步调试。

source/c09/c09_05.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pdb,可能你还不知道它,为了讲解这个神器,我写了这篇文
4545

4646
::
4747

48-
ptyhon -m pdb pdb_demo.py
48+
python -m pdb pdb_demo.py
4949

5050
使用这个方式进入调试模式,会在脚本的第一行开始单步调试。
5151

source/c09/c09_10.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 9.10 【调试技巧】报错后直接切换pdb调试
2+
3+
前面的文章里介绍了两种 pdb 的调试入口,也是大部分所熟知的。
4+
5+
这里再带大家回顾一下
6+
7+
**第一种**:指定 `-m pdb` 来开启
8+
9+
```python
10+
$ python -m pdb pdb_demo.py
11+
```
12+
13+
**第二种**:使用 `pdb.set_trace()` 在代码中设置断点
14+
15+
```python
16+
import pdb
17+
18+
pdb.set_trace()
19+
```
20+
21+
但其实,pdb 还另外两种调试方法,第一种方法,可能有 99% 的开发者都没用过,甚至连见过都没有。
22+
23+
这两种方法,是配合 Python Console 的交互界面来实现的。
24+
25+
首先我准备好一个名为 `utils.py` 的 Python文件,里面定义了一个 sum 的工具函数(仅作演示用)。
26+
27+
```python
28+
def sum(*args):
29+
result = 0
30+
for arg in args:
31+
result += arg
32+
33+
return result
34+
```
35+
36+
然后在终端敲入 Python 进入 Console 的模式,导入这个模块,并调用 sum 函数,在正常情况下,函数可以正常工作。
37+
38+
```python
39+
>>> import utils
40+
>>> utils.sum(1,2,3)
41+
6
42+
```
43+
44+
但如果你的参数类型传成了 str,函数就会报错啦~
45+
46+
```python
47+
>>> utils.sum(1,2,"3")
48+
Traceback (most recent call last):
49+
File "<stdin>", line 1, in <module>
50+
File "/Users/MING/utils.py", line 4, in sum
51+
result += arg
52+
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
53+
```
54+
55+
由于这里的报错是我刻意触发的,从报错来看,是很容易定位的。
56+
57+
但是在实际应用中,难免会遇到一些无法从报错信息直接判断 bug 所在的情况。
58+
59+
这个时候,如果可以在报错后,切换到 pdb 的调试模式就好了~
60+
61+
**事实上,pdb 是支持这种用法的。**
62+
63+
只要你在当前的会话中,导入 pdb,再执行 `pdb.pn()`,就可以切换到熟悉的 pdb 调试界面,并在抛错的地方打上断点,然后你就可以任意的查看运行时的变量信息。
64+
65+
![](http://image.iswbm.com/20210314164424.png)
66+
67+
如果你不是想等报错了再调试,而是一开始就想进入调试模式,可以使用 `pdb.runcall()` 函数
68+
69+
![](http://image.iswbm.com/20210314170221.png)
70+
71+
有的同学可能还会想到 `pdb.run()``pdb.runeval()` 这两个函数,但这两种方法,是需要提前在函数调试断点的,这就比较麻烦了,一般情况下不推荐使用。
72+
73+
综上所述,今天 给大家介绍了两种新的 pdb 调试入口:
74+
75+
1. `pdb.pm()`:在出错后直接切换到调试模式,并定位到报错位置。 -- **今天的重点**
76+
2. `pdb.runcall()`:可以在不设置断点的情况下,直接调试代码片段。

source/c09/c09_10.rst

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
9.10 【调试技巧】报错后直接切换pdb调试
2+
======================================
3+
4+
前面的文章里介绍了两种 pdb 的调试入口,也是大部分所熟知的。
5+
6+
这里再带大家回顾一下
7+
8+
**第一种**\ :指定 ``-m pdb`` 来开启
9+
10+
.. code:: python
11+
12+
$ python -m pdb pdb_demo.py
13+
14+
**第二种**\ :使用 ``pdb.set_trace()`` 在代码中设置断点
15+
16+
.. code:: python
17+
18+
import pdb
19+
20+
pdb.set_trace()
21+
22+
但其实,pdb 还另外两种调试方法,第一种方法,可能有 99%
23+
的开发者都没用过,甚至连见过都没有。
24+
25+
这两种方法,是配合 Python Console 的交互界面来实现的。
26+
27+
首先我准备好一个名为 ``utils.py`` 的 Python文件,里面定义了一个 sum
28+
的工具函数(仅作演示用)。
29+
30+
.. code:: python
31+
32+
def sum(*args):
33+
result = 0
34+
for arg in args:
35+
result += arg
36+
37+
return result
38+
39+
然后在终端敲入 Python 进入 Console 的模式,导入这个模块,并调用 sum
40+
函数,在正常情况下,函数可以正常工作。
41+
42+
.. code:: python
43+
44+
>>> import utils
45+
>>> utils.sum(1,2,3)
46+
6
47+
48+
但如果你的参数类型传成了 str,函数就会报错啦~
49+
50+
.. code:: python
51+
52+
>>> utils.sum(1,2,"3")
53+
Traceback (most recent call last):
54+
File "<stdin>", line 1, in <module>
55+
File "/Users/MING/utils.py", line 4, in sum
56+
result += arg
57+
TypeError: unsupported operand type(s) for +=: 'int' and 'str'
58+
59+
由于这里的报错是我刻意触发的,从报错来看,是很容易定位的。
60+
61+
但是在实际应用中,难免会遇到一些无法从报错信息直接判断 bug 所在的情况。
62+
63+
这个时候,如果可以在报错后,切换到 pdb 的调试模式就好了~
64+
65+
**事实上,pdb 是支持这种用法的。**
66+
67+
只要你在当前的会话中,导入 pdb,再执行
68+
``pdb.pn()``\ ,就可以切换到熟悉的 pdb
69+
调试界面,并在抛错的地方打上断点,然后你就可以任意的查看运行时的变量信息。
70+
71+
|image0|
72+
73+
如果你不是想等报错了再调试,而是一开始就想进入调试模式,可以使用
74+
``pdb.runcall()`` 函数
75+
76+
|image1|
77+
78+
有的同学可能还会想到 ``pdb.run()````pdb.runeval()``
79+
这两个函数,但这两种方法,是需要提前在函数调试断点的,这就比较麻烦了,一般情况下不推荐使用。
80+
81+
综上所述,今天 给大家介绍了两种新的 pdb 调试入口:
82+
83+
1. ``pdb.pm()``\ :在出错后直接切换到调试模式,并定位到报错位置。 –
84+
**今天的重点**
85+
2. ``pdb.runcall()``\ :可以在不设置断点的情况下,直接调试代码片段。
86+
87+
.. |image0| image:: http://image.iswbm.com/20210314164424.png
88+
.. |image1| image:: http://image.iswbm.com/20210314170221.png
89+

我是如何学习 Python 的.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,14 +430,6 @@ Python 是一门非常高级的语言,写代码的人完全可以利用的人
430430

431431

432432

433-
434-
435-
436-
437-
438-
439-
害,码了两天,终于是写完了。
440-
441433
马上 2020 年就要划上句号了,回想自己已经做了三年多的 Python 程序员,抓着 2020 的尾巴,第一次认真地总结下我当年是如何走上编程这条不归路的,当然我也更希望我的这些经验能够帮助到更多想通过 Python 转行成为一个 1024er 的小白。
442434

443435
就写这么多吧,说实话,写这篇回答比写代码累。如果我的写东西对你有用,高抬贵手来个赞呗~

0 commit comments

Comments
 (0)