Skip to content

Commit a674c72

Browse files
committed
chore: move dirs
1 parent e68c3a7 commit a674c72

Some content is hidden

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

92 files changed

+846
-2488
lines changed

.vscode/extensions.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"recommendations": [
3-
"DavidAnson.vscode-markdownlint",
43
"yzhang.markdown-all-in-one",
5-
"bierner.markdown-mermaid"
4+
"vstirbu.vscode-mermaid-preview",
5+
"bpruitt-goddard.mermaid-markdown-syntax-highlighting",
6+
"bierner.markdown-mermaid",
7+
"DavidAnson.vscode-markdownlint"
68
]
79
}

.vscode/settings.json

+1-62
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,4 @@
11
{
22
"python.analysis.typeCheckingMode": "basic",
3-
// Enable the ESlint flat config support
4-
"eslint.experimental.useFlatConfig": true,
5-
// Disable the default formatter, use eslint instead
6-
"prettier.enable": false,
7-
"editor.formatOnSave": false,
8-
// Auto fix
9-
"editor.codeActionsOnSave": {
10-
"source.fixAll.eslint": true,
11-
"source.organizeImports": false
12-
},
13-
// Silent the stylistic rules in you IDE, but still auto fix them
14-
"eslint.rules.customizations": [
15-
{
16-
"rule": "style/*",
17-
"severity": "off"
18-
},
19-
{
20-
"rule": "*-indent",
21-
"severity": "off"
22-
},
23-
{
24-
"rule": "*-spacing",
25-
"severity": "off"
26-
},
27-
{
28-
"rule": "*-spaces",
29-
"severity": "off"
30-
},
31-
{
32-
"rule": "*-order",
33-
"severity": "off"
34-
},
35-
{
36-
"rule": "*-dangle",
37-
"severity": "off"
38-
},
39-
{
40-
"rule": "*-newline",
41-
"severity": "off"
42-
},
43-
{
44-
"rule": "*quotes",
45-
"severity": "off"
46-
},
47-
{
48-
"rule": "*semi",
49-
"severity": "off"
50-
}
51-
],
52-
// Enable eslint for all supported languages
53-
"eslint.validate": [
54-
"javascript",
55-
"javascriptreact",
56-
"typescript",
57-
"typescriptreact",
58-
"vue",
59-
"html",
60-
"markdown",
61-
"json",
62-
"jsonc",
63-
"yaml"
64-
]
3+
"python.analysis.autoImportCompletions": true
654
}

docs/.vuepress/config.ts

-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ export default defineUserConfig({
7373
container: true,
7474
vPre: true,
7575
tabs: true,
76-
card: true,
7776
codetabs: true,
7877
include: {
7978
resolvePath: (file) => {

docs/python-core/generic-programming/index.md renamed to docs/advanced/generic-programming/index.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Python 泛型编程
22

3+
<!-- markdownlint-disable MD033 -->
4+
35
Python 是多范式的编程语言,支持泛型编程。本文介绍使用 Python 泛型编程的一些细节和实现。
46

57
[[TOC]]
@@ -15,6 +17,7 @@ Python 是多范式的编程语言,支持泛型编程。本文介绍使用 Pyt
1517
3. 减少不确定性的错误和预期外的结果
1618

1719
Python 的泛型编程和 C++/Java 的泛型编程有什么区别和共同点?
20+
1821
1. 共同点:都可以实现代码复用
1922
2. 不同点:Python 的泛型不是强制的,可以不遵循类型声明,可以使用任意的类型代替
2023

@@ -25,7 +28,7 @@ Python 使用 `typing` 模块实现类型注解,相对来讲,Python 的类
2528
虽然 Python 是动态类型的语言,但写程序时养成良好的习惯,会大大提高你的编码效率,减少错误出现,正如 *Zen of Python* 所说
2629

2730
> Explicit is better than implicit.(显式胜于隐式)
28-
>
31+
>
2932
> <cite>——Tim Peters</cite>
3033
3134
通俗一点来讲就是,如果你的函数返回类型是可知的,就不要把它藏起来,让别人猜测你的返回类型。
@@ -67,7 +70,7 @@ l0 = list[tuple[str]]()
6770
正如 *Zen of Python* 所说的:
6871

6972
> Special cases aren't special enough to break the rules.(特殊情形不足以特殊到打破规则)
70-
>
73+
>
7174
> <cite>——Tim Peters</cite>
7275
7376
:::

docs/advanced/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 进阶
2+
3+
<AutoCatalog />

docs/quicknote/cython-build-image.md renamed to docs/awesome/cython-build-image.md

+11-11
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ pip install -U cython
6161

6262
- `resources/`:项目用到的资源文件
6363
- `demo_server/`
64-
- `models/`
65-
- `__init__.py`
66-
- `xxx.py`
67-
- `services/`
68-
- `__init__.py`
69-
- `xxx.py`
70-
- `utils/`
71-
- `__init__.py`
72-
- `xxx.py`
64+
- `models/`
7365
- `__init__.py`
74-
- `core.py`
66+
- `xxx.py`
67+
- `services/`
68+
- `__init__.py`
69+
- `xxx.py`
70+
- `utils/`
71+
- `__init__.py`
72+
- `xxx.py`
73+
- `__init__.py`
74+
- `core.py`
7575
- `server.py`:主入口文件
7676
- `setup.py`:包配置文件
7777
- `requirements.txt`:依赖文件
@@ -206,7 +206,7 @@ pip-delete-this-directory.txt
206206

207207
## 3. 动态导入和路由发现
208208

209-
以下内容需要对 Python 的 `importlib` 模块有一定的了解,如果你不了解如何动态导入模块,可以参考 [此文章](../python-core/collections/import-python-file-from-path.md)
209+
以下内容需要对 Python 的 `importlib` 模块有一定的了解,如果你不了解如何动态导入模块,可以参考 [此文章](../basic/collections/import-python-file-from-path.md)
210210

211211
我们在构建网络服务时常常使用一些元编程技巧,这常常需要借助运行时反射来实现。如 SpringBoot 中的 `@ComponentScan` 能够自动扫描并加载所有的 `@Component` 注解的类,然后将它们注册到 IoC 容器中。
212212

docs/basic/asyncio/coroutine.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Python 协程
2+
3+
::: info 参考
4+
5+
[官方文档术语对照表](https://docs.python.org/zh-cn/3/glossary.html#term-coroutine)
6+
7+
:::
8+
9+
## 1. 协程
10+
11+
协程是子例程的更一般形式。
12+
13+
子例程可以在某一点进入并在另一点退出。协程则可以在许多不同的点上进入、退出和恢复。它们可通过 [async def](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#async-def) 语句来实现。参见 [PEP 492](https://www.python.org/dev/peps/pep-0492)
14+
15+
- *@3.5+* **协程**(coroutine)可以在多个位置上挂起和恢复执行
16+
- `await` 表达式,`async for` 以及 `async with` 只能在协程函数体中使用
17+
- 使用 `async def` 语法定义的函数总是为协程函数,即使它们不包含 `await``async` 关键字
18+
- 在协程函数体中使用 `yield from` 表达式将引发 `SyntaxError`
19+
20+
详细解释见 [官方文档语言参考手册](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#coroutines)
21+
22+
## 2. 协程函数
23+
24+
返回一个 [`Coroutine`](https://docs.python.org/zh-cn/3/glossary.html#term-coroutine) 对象的函数。
25+
26+
协程函数可通过 [`async def`](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#async-def) 语句来定义,并可能包含 [`await`](https://docs.python.org/zh-cn/3/reference/expressions.html#await)[`async for`](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#async-for)[`async with`](https://docs.python.org/zh-cn/3/reference/compound_stmts.html#async-with) 关键字。这些特性是由 [PEP 492](https://www.python.org/dev/peps/pep-0492) 引入的。
27+
28+
## 3. PEP 492
29+
30+
PEP 是 Python 语言发展的提案。
31+
32+
[PEP 492](https://www.python.org/dev/peps/pep-0492) 提出使用 `async``await` 语法实现协程,将协程作为 Python 中的一个正式的单独概念,并增加相应的支持语法。
33+
34+
该提案在 Python 3.5 版本实现。
File renamed without changes.

docs/basic/asyncio/intro.md

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Python 异步编程
2+
3+
::: info 转载
4+
5+
原文链接:[阮一峰的网络日志:Python 异步编程入门](https://www.ruanyifeng.com/blog/2019/11/python-asyncio.html)
6+
7+
:::
8+
9+
[[TOC]]
10+
11+
Python 3.4 引入了 `asyncio` 模块,增加了异步编程,跟 JavaScript 的 `async/await` 极为类似,大大方便了异步任务的处理。
12+
13+
## Python 的异步编程
14+
15+
历史上,Python 并不支持专门的异步编程语法,因为不需要。
16+
17+
有了多线程(`threading`)和多进程(`multiprocessing`),就没必要一定支持异步了。如果一个线程(或进程)阻塞,新建其他线程(或进程)就可以了,程序不会卡死。
18+
19+
但是,多线程有 “线程竞争” 的问题,处理起来很复杂,还涉及加锁。对于简单的异步任务来说(比如与网页互动),写起来很麻烦。
20+
21+
## asyncio 的设计
22+
23+
`asyncio` 模块最大特点就是,只存在一个线程。
24+
25+
由于只有一个线程,就不可能多个任务同时运行。`asyncio`**多任务合作** 模式(cooperative multitasking),允许异步任务交出执行权给其他任务,等到其他任务完成,再收回执行权继续往下执行,这和 JavaScript 也是一样的。
26+
27+
由于代码的执行权在多个任务之间交换,所以看上去好像多个任务同时运行,其实底层只有一个线程,多个任务分享运行时间。
28+
29+
`asyncio` 模块在单线程上启动一个事件循环(event loop),时刻监听新进入循环的事件,加以处理,并不断重复这个过程,直到异步任务结束。
30+
31+
## asyncio API
32+
33+
下面介绍 `asyncio` 模块最主要的几个 API。注意,必须使用 Python 3.7 或更高版本,早期的语法已经改变了。
34+
35+
第一步,`import` 导入 `asyncio` 模块。
36+
37+
```python
38+
import asyncio
39+
```
40+
41+
第二步,函数前面加上 `async` 关键字,就变成了 `async` 函数。这种函数最大特点是执行可以暂停,交出执行权。
42+
43+
```python
44+
async def main():
45+
pass
46+
```
47+
48+
第三步,在 `async` 函数内部的异步任务前面,加上 `await` 命令。
49+
50+
```python
51+
await asyncio.sleep(1)
52+
```
53+
54+
上面代码中,`asyncio.sleep(1)` 方法可以生成一个异步任务,休眠 1 秒钟然后结束。
55+
56+
执行引擎遇到 `await` 命令,就会在异步任务开始执行之后,暂停当前 `async` 函数的执行,把执行权交给其他任务。等到异步任务结束,再把执行权交回 `async` 函数,继续往下执行。
57+
58+
第四步,`async.run()` 方法加载 `async` 函数,启动事件循环。
59+
60+
```python
61+
asyncio.run(main())
62+
```
63+
64+
上面代码中,`asyncio.run()` 在事件循环上监听 `async` 函数 `main()` 的执行。等到 `main()` 执行完了,事件循环才会终止。
65+
66+
## async 函数示例
67+
68+
```python
69+
import asyncio
70+
71+
async def count():
72+
print('One')
73+
await asyncio.sleep(1)
74+
print('Two')
75+
76+
async def main():
77+
await asyncio.gather(count(), count(), count())
78+
79+
asyncio.run(main())
80+
```
81+
82+
执行结果:
83+
84+
```console
85+
$ python async.py
86+
One
87+
One
88+
One
89+
Two
90+
Two
91+
Two
92+
```
93+
94+
脚本总的运行时间是 1 秒,而它们同步执行的时间是 3 秒。
File renamed without changes.

docs/python-core/collections/python-ebnf.md renamed to docs/basic/collections/python-ebnf.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## 1. Python 语言定义
66

7-
我们在第三方库中介绍过一个 Python 语言解析器[`parso`](../../pypi-package/compiler/parso.md),它的核心就是一个 Python 语言定义,它是一个 EBNF 语法定义,用于解析 Python 代码。[^1]
7+
我们在第三方库中介绍过一个 Python 语言解析器[`parso`](../../packages/compiler/parso.md),它的核心就是一个 Python 语言定义,它是一个 EBNF 语法定义,用于解析 Python 代码。[^1]
88

99
[^1]: 冷门话题,聊一聊 Python 的 EBNF,酷 Python,<http://www.coolpython.net/informal_essay/21-04/py-ebnf.html>
1010

@@ -34,13 +34,13 @@ suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT
3434

3535
## 2. EBNF 语法
3636

37-
```card
37+
<!-- ```card
3838
title: AI 提示
3939
desc: 解释一下 Extended Backus Naur Form(EBNF)是什么?What is the Extended Backus Naur Form (EBNF)?
4040
link: https://chat.openai.com/
4141
logo: /vuepress-python-notes/chatgpt.svg
4242
color: rgba(226, 212, 255, 0.2)
43-
```
43+
``` -->
4444
4545
**巴科斯-诺尔范式**(Extended Backus-Naur Form,EBNF)是一种用于描述计算机语言语法的元语言。它是 Backus-Naur Form(BNF)的扩展版本,提供了更多的元素和语法结构,以便更准确地描述一些复杂的语法规则。
4646

docs/basic/concurrent/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 并发编程
2+
3+
<AutoCatalog />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 多进程
2+
3+
<AutoCatalog />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Python 多进程
2+
3+
[[TOC]]
4+
5+
## 1. 进程
6+
7+
多进程可以避免 Python 多线程的劣势,充分发挥多核优势。
8+
9+
## 2. 创建进程
10+
11+
创建过程与多线程一致:
12+
13+
@[code python](./src/mp_1.py)
14+
15+
## 3. 使用 `Queue` 进行进程间通信
16+
17+
使用 `multiprocessing.Queue` 返回输出的值:
18+
19+
@[code python](./src/mp_queue.py)
20+
21+
## 4. 多线程与多进程对比
22+
23+
性能对比
24+
25+
@[code python](./src/mp_2.py)
26+
27+
## 5. `Pool` 进程池
28+
29+
@[code python](./src/mp_pool.py)
30+
31+
`apply_async()` 函数一次使用一个值
32+
33+
```python
34+
res = pool.apply_async(job, (2,))
35+
print(res.get())
36+
pool = mp.Pool(processes=2)
37+
```
38+
39+
## 6. 共享内存
40+
41+
定义共享内存变量:
42+
43+
```python
44+
val = mp.Value('d', 1)
45+
array = mp.Array('i', [1, 2, 3])
46+
```
47+
48+
变量的类型值可以参考[标准库文档](https://docs.python.org/zh-cn/3/library/array.html#module-array)
49+
50+
## 7. `Lock`
51+
52+
使用锁:
53+
54+
@[code python](./src/mp_lock.py)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import multiprocessing as mp
2+
3+
4+
def job(a: int, b: int) -> None:
5+
print("a:", a, "b:", b)
6+
7+
8+
if __name__ == "__main__":
9+
p1 = mp.Process(target=job, args=(1, 2))
10+
p1.start()
11+
p1.join()

0 commit comments

Comments
 (0)