Python C API ---- Write Python C Extension and Call Python From C
C 语言 转 Python
解析 Python 变量
函数: PyArg_ParseTuple
- 单个 python 变量转成 C 变量
- python tuple 转成 多个 C 变量
接口 (PyArg_ParseTuple(args, "ii", &num1, &num2)
- args: Python 所有参数
- "ii": Python 参数的类型与个数,相当于翻译模板, "ii" 代表两个 整数
- &num1, &num2: 使用引用指针接收,解析出来的变量
- 返回值: 失败为零, 成功非零
| |
coverage ---- Python unit test and code coverage tool 代码覆盖率探测
report 中包含大量 site-packages 模块测试
- 参考:coverage run does not ignore .venv dir
解决办法
coverage run --omit ".venv/*" -m pytest tests
修复 report, no source code error
情况一
案例
py38 run-test: commands[3] | coverage html --skip-covered No source for code: '/mnt/d/source/infrared_quantity/D:\source\infrared_quantity\tests\test_textrender.py'. Aborting report output, consider using -i. ERROR: InvocationError for command /mnt/d/source/infrared_quantity/.tox/py38/bin/coverage html --skip-covered (exited with code 1)
修复方法
mypy ---- Python typing checker
missing types or stubs not installed
解决办法
- mypy –install-types tests
- mypy –install-types -c 'import tests'
参考
pyproject.toml 配置文件
参考
例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23# mypy global options: [tool.mypy] python_version = "2.7" warn_return_any = true warn_unused_configs = true # mypy per-module options: [[tool.mypy.overrides]] module = ["mycode.foo.*", "module2", "module3", ...] disallow_untyped_defs = true [[tool.mypy.overrides]] module = "mycode.bar" warn_return_any = false [[tool.mypy.overrides]] module = [ "somelibrary", "some_other_library" ] ignore_missing_imports = true
初次启动慢
- 正常现象
tox ---- Python project multiple python version test framework
修改 pip 源
- 参考:Basic usage — tox 3.24.5.dev3 documentation
方法
- 设置环境变量 PIP_INDEX_URL
例子
1 2 3[testenv] setenv = PIP_INDEX_URL = https://pypi.my-alternative-index.org
与 pyenv 联用
- 先设定 pyenv local
在运行 tox
1 2 3pyenv local system 3.5.2 3.7.8 3.9.10 tox
fastapi ---- a Python Web Framework
教程
- 官方文档: FastAPI
类似项目对比
与 flask 对比
Why Choose Flask Over FastAPI - Python kitchen
- flask 和 starlette 是 counterpart
- flask-restx 和 fastapi 是 counterpart
兄弟项目
Typer, 命令行工具
相关工具