FastAPI后台开发基础(2):路径参数
fastapi框架下各种路径参数的使用方式
fastapi框架下各种路径参数的使用方式
特点 高性能:基于 Starlette 和 Pydantic,FastAPI 在性能上接近 Node.js 和 Go。 自动生成文档:内置 Swagger UI 和 ReDoc,可以自动生成交互式API文档。 类型安全:使用 Python 的类型提示,提供数据验证和序列化。 异步支持:原生支持异步编程,适合处理高并发请求。 最小化代码结构 1 2 3 4 5 6 7 8 9 from __future__ import annotations from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"message": "Hello, World!"} 服务启动方式 fastapi 开发模式 fastapi dev main.py --host 0.0.0.0 --port 18888 ...
简单的super类使用 1 2 3 4 5 6 7 8 9 10 11 12 13 14 class A(object): def __init__(self): print(self.__class__, 'A init') class B1(A): def __init__(self): # 效果等同于 super(B1, self).__init__() # 参考 class B2 super().__init__() print(self.__class__, 'B1 init') if __name__ == '__main__': print(B1.mro()) B1() 代码运行的效果: 单继承时 super 调用的效果 此时对 B1 来说,它的 mro 调用链是:B1 –> A –> object ...
MRO 的全称是:Method Resolution Order,从 Python2.3 开始,Python 使用 C3 算法来实现 Python 类继承时的线性解析逻辑。 Python 使用 C3 作为 MRO 算法的起因 C3 算法主要解决的问题 ...
使用 Matplotlib 绘制函数曲线的编码范式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 from __future__ import annotations import matplotlib.pyplot as plt import numpy as np # ==================== 1. 数据生成 ==================== # 生成 x 轴数据(示例:从 0 到 2π,均匀取 200 个点) x = np.linspace(0 , 2 * np.pi , 200) # 生成多个 y 函数(可根据需要添加/修改) # 同时定义每个函数曲线的绘图风格 y_functions = { "sin(x)": { 'data': np.sin(x) , 'style':{ 'color':'blue' , 'linestyle':'-' , 'linewidth':1 } , } , "cos(x)": { 'data': np.cos(x) , 'style':{ 'color':'red' , 'linestyle':'-' , 'linewidth':2 } , } , "0.5*sin(2x)":{ 'data': 0.5 * np.sin(2 * x) , 'style':{ 'color':'green' , 'linestyle':':' , 'linewidth':3 , 'alpha':0.7 } , } , "exp(-x)": { 'data': np.exp(-x) , 'style':{ 'color':'purple' , 'linestyle':'-.' , 'linewidth':4 , 'marker':'o' } , } , } # ==================== 2. 创建画布和坐标系 ==================== # 参数说明: # fig-size: (宽, 高) 英寸 # dpi: 分辨率 fig , ax = plt.subplots(figsize = (10 , 6) , dpi = 100) # ==================== 3. 绘制多条曲线 ==================== # 循环绘制所有曲线 for curve_func in y_functions: label = curve_func y = y_functions[ curve_func ][ 'data' ] style = y_functions[ curve_func ][ 'style' ] ax.plot(x , y , label = label , # 图例标签 **style , # 解包样式字典 ) # ==================== 4. 添加图表元素 ==================== ax.legend(loc = 'upper right') # 显示图例 ax.set_title("Multiple Function Curves" , fontsize = 14 , pad = 20) # 标题 ax.set_xlabel("x-axis" , fontsize = 12) # x轴标签 ax.set_ylabel("y-axis" , fontsize = 12) # y轴标签 # ==================== 5. 自定义样式 ==================== ax.grid(True , linestyle = '--' , alpha = 0.6) # 显示网格线 ax.set_xlim(0 , 2 * np.pi) # 设置x轴范围 ax.set_ylim(-1.2 , 1.5) # 设置y轴范围 ax.tick_params(axis = 'both' , labelsize = 10) # 刻度标签大小 # ==================== 6. 显示/保存 ==================== plt.tight_layout() # 自动调整子图间距 plt.show() ...