# fastapi-demo **Repository Path**: azhao-1981/fastapi-demo ## Basic Information - **Project Name**: fastapi-demo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-03-24 - **Last Updated**: 2024-05-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 下载代码 ```bash # 如果机器装了conda,需要unset unset CONDA_PREFIX unset CONDA_PREFIX; uv pip install -r requirements.txt -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple ``` ## 启动 http ```bash uv pip install uvicorn -U -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple # 使用这个可以热重启 uvicorn main:app --reload ``` ### 添加新包 ```bash uv venv .demovenv uv pip install sqlalchemy fastapi pymysql mysql-connector-python -U -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple uv pip freeze > requirements.txt ``` ### grpc ### 国内源,uv不生效 ```bash pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/ pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn alias uv="unset CONDA_PREFIX; uv " alias uip="unset CONDA_PREFIX; uv pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple" uip grpcio grpcio-tools ``` ### 启动 grpc ### error: 输入中不应该包括 body 因为与http 请求的 body 关键字冲突,会造成报参数错误 ## sqlalchemy ```python SessionFactory = sessionmaker() ScopedSession = scoped_session(SessionFactory) ``` ## pytest ### ignore warnings ```json "python.testing.pytestArgs": [ "tests","-s","-p no:warnings" ], ``` "-p no:warnings" 取消 warnings ### 打印 Logging https://stackoverflow.com/questions/4673373/logging-within-pytest-tests 方法一:直接在pytest中修改 ```bash pytest --log-cli-level=DEBUG --capture=tee-sys tests/test_log.py::test_logger pytest --log-cli-level=DEBUG --capture=no tests/test_log.py::test_logger ``` 好像这两个没有区别 方法二:写到 pytest.ini 中 ```ini [pytest] log_cli = 1 log_cli_level = INFO ``` 方法三: settings.json,这个可以在 vscode TEST_RESULTS 中使用 ```json "python.testing.pytestArgs": [ "tests","-s","--log-cli-level=DEBUG","--capture=tee-sys" ], ``` 三个方法 都没有写到 log里,只是打印在命令行中 方法四:使用 main 来跑 ```python if __name__ == '__main__': test_logging() ``` ### warning /home/azhao/projects/demo/fastapi-demo/.demovenv/lib/python3.10/site-packages/pydantic/_internal/_config.py:272: PydanticDeprecatedSince20: Support for class-based `config` is deprecated, use ConfigDict instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/ 这是 schemas 中的 `class Config` 注意是 C 是大写 /home/azhao/projects/demo/fastapi-demo/.demovenv/lib/python3.10/site-packages/pydantic/main.py:1024: PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.6/migration/ warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', category=PydanticDeprecatedSince20) ## TIPS: [grpc 数据库连接最佳实践](doc/grpc.db.session.md) [cached since x.xxxs ago] 指的是查询的 SQL 语句编译结果被缓存,而不是插入的数据缓存。 ```sql SELECT * FROM information_schema.PROCESSLIST WHERE db='xx_db'; ```