from typing importIterableimport os defsum_even_numbers(numbers:Iterable[int])->int:"""给定一个整数可迭代对象,返回其中所有偶数的和。"""returnsum( num for num in numbersif num %2==0)
接下来,我们将 Ruff 添加到项目中:
uv add--dev ruff
然后,可以通过 uv run ruff check 对项目运行 Ruff 代码检查:
$ uv run ruff check src/numbers/__init__.py
:3:8: F401 [*]`os` imported but unused Found1 error.[*]1 fixable with the `--fix` option.
在 src/numbers/__init__.py文件的第 3 行第 8 列,导入了 os 模块,但并未使用。
我们可以运行以下命令,让 Ruff 自动修复该问题:
$ uv run ruff check--fix Found1 error (1fixed,0 remaining).
•创建文件 starter.py 设置环境变量 OPENAI_API_KEY,值为你的 OpenAI API Key[1]
import asynciofrom llama_index.core.agent.workflow importAgentWorkflowfrom llama_index.llms.openai importOpenAI # Define a simple calculator tooldefmultiply(a:float, b:float)->float:"""Useful for multiplying two numbers."""return a * b # Create an agent workflow with our calculator toolagent =AgentWorkflow.from_tools_or_functions([multiply], llm=OpenAI(model="gpt-4o-mini"), system_prompt="You are a helpful assistant that can multiply two numbers.",) asyncdefmain():# Run the agent response = await agent.run("What is 1234 * 4567?")print(str(response)) # Run the agentif __name__ =="__main__": asyncio.run(main())
The result of \( 1234 \times 4567 \) is \( 5,678,678 \).
8. Robyn — 最快的 Python Web 框架
Robyn 是一个高性能 Python Web 框架,是 Flask 和 FastAPI 的替代方案之一,针对多核处理进行了优化。
为什么要使用它?
•性能约为 FastAPI 的 5 倍•支持异步(async)与多线程•底层使用 Rust 构建,性能极高•适合高并发 Web 服务与 API 开发
文档
https://robyn.tech/documentation/en
安装方法
pip install robyn
示例
使用以下命令创建一个简单项目:
$ python -m robyn --create
运行后,会生成如下项目结构:
$ python3 -m robyn --create?DirectoryPath:.?NeedDocker?(Y/N) Y?Pleaseselect project type (Mongo/Postgres/Sqlalchemy/Prisma):❯NoDBSqlitePostgresMongoDBSqlAlchemyPrisma
运行创建命令后,会生成如下应用结构:
├── src│├── app.py├──Dockerfile
编写应用代码
接下来,你可以在 app.py 中编写如下代码:
from robyn importRequest @app.get("/")asyncdefh(request:Request)-> str:return"Hello, world"