-
guest说:
用fastapi+sqlalchemy+pydantic+mysql搭建一个项目,展示出合理的目录结构
├─app
│ ├─main.py
│ ├─models.py
│ ├─schema.py
│ ├─crud.py
│ └─utils.py
├─config
│ └─config.py
├─migrations
│ ├─env.py
│ ├─script.py.mako
│ ├─versions
│ └─README
├─tests
│ ├─conftest.py
│ └─test_main.py
└─.env -
guest说:
用fastapi+sqlalchemy+pydantic+mysql搭建一个简单的学生信息系统,要求代码合理,目录结构合理
import uvicorn
from fastapi import FastAPI, Request, Response
from pydantic import BaseModel
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker初始化数据库连接:
engine = create_engine('mysql+pymysql://root:123456@127.0.0.1:3306/test')
创建对象的基类:
Base = declarative_base()
定义Student对象:
class Student(Base):
# 表的名字:
tablename = 'student'# 表的结构: id = Column(Integer, primary_key=True, autoincrement=True) name = Column(String(20)) age = Column(Integer) gender = Column(String(20))
初始化数据库连接:
Base.metadata.create_all(engine)
创建DBSession类型:
DBSession = sessionmaker(bind=engine)
实例化FastAPI
app = FastAPI()
定义Request Body
class StudentInfo(BaseModel):
name: str
age: int
gender: str获取所有学生信息
@app.get('/students')
def get_students():
session = DBSession()
try:
student_list = []
student_info = session.query(Student).all()
for student in student_info:
student_list.append({
'id': student.id,
'name': student.name,
'age': student.age,
'gender': student.gender
})
return student_list
except Exception as e:
return {'error': str(e)}
finally:
session.close()添加学生信息
@app.post('/students')
def add_students(student_info: StudentInfo):
session = DBSession()
try:
student = Student(
name=student_info.name,
age=student_info.age,
gender=student_info.gender
)
session.add(student)
session.commit()
session.refresh(student)
return {
'id': student.id,
'name': student.name,
'age': student.age,
'gender': student.gender
}
except Exception as e:
return {'error': str(e)}
finally:
session.close()更新学生信息
@app.put('/students/{id}')
def update_students(id: int, student_info: StudentInfo):
session = DBSession()
try:
student = session.query(Student).filter(Student.id == id).one()
student.name = student_info.name
student.age = student_info.age
student.gender = student_info.gender
session.commit()
return {
'id': student.id,
'name': student.name,
'age': student.age,
'gender': student.gender
}
except Exception as e:
return {'error': str(e)}
finally:
session.close()删除学生信息
@app.delete('/students/{id}')
def delete_students(id: int):
session = DBSession()
try:
student = session.query(Student).filter(Student.id == id).one()
session.delete(student)
session.commit()
return {'success': True}
except Exception as e:
return {'error': str(e)}
finally:
session.close()if name == 'main':
uvicorn.run(app, host="127.0.0.1", port=8000) -
guest说:
用fastapi搭建一个blog系统的目录结构
├── app │ ├── api │ │ ├── auth.py │ │ ├── blog.py │ │ └── users.py │ ├── config.py │ ├── __init__.py │ ├── main.py │ ├── models │ │ ├── auth.py │ │ ├── blog.py │ │ └── users.py │ ├── schemas │ │ ├── auth.py │ │ ├── blog.py │ │ └── users.py │ ├── tests │ │ ├── auth.py │ │ ├── conftest.py │ │ ├── blog.py │ │ └── users.py │ └── utils │ ├── auth.py │ └── users.py ├── docker-compose.yml ├── Dockerfile ├── main.py ├── Pipfile ├── Pipfile.lock └── README.md
-
peng111说:
MST一B800和MST一B100车库门电机区别
MST一B800和MST一B100车库门电机的区别:
-
功率不同:MST一B800的电机功率为800W,而MST一B100的电机功率为100W。
-
品牌不同:MST一B800的电机由德国MST品牌生产,而MST一B100的电机由德国Sommer品牌生产。
-
价格不同:MST一B800的电机价格更高,而MST一B100的电机价格更低。
-
加速度不同:MST一B800的电机拥有更高的加速度,而MST一B100的电机拥有更低的加速度。
-
速度不同:MST一B800的电机具有更快的速度,而MST一B100的电机具有更慢的速度。
-
-
eatmelon说:
请给出一元三次方程的求根公式并证明
一元三次方程的求根公式为:
$$x=\frac{1}{3a}\left[ -b \pm \sqrt{b^2-3ac}\right]-\frac{a}{3}$$
证明:
令一元三次方程为:
$$ax^3+bx^2+cx+d=0$$
利用完全平方公式,可以把它变形为:
$$a\left(x^2+\frac{b}{a}x+\frac{c}{a}\right)^2- \frac{b^2-3ac}{3a^2}x-\left(\frac{2b^3-9abc}{27a^3}+\frac{d}{a}\right)=0$$
设
$$u=x^2+\frac{b}{a}x+\frac{c}{a}$$
则一元三次方程可变形为:
$$a u^2- \frac{b^2-3ac}{3a^2}x-\left(\frac{2b^3-9abc}{27a^3}+\frac{d}{a}\right)=0$$
设
$$v=\frac{b^2-3ac}{3a^2}$$
则:
$$a u^2-vx-\left(\frac{2b^3-9abc}{27a^3}+\frac{d}{a}\right)=0$$
此时,可以看出,这是一个一元二次方程:
$$a u^2-vx-w=0$$
解之,得:
$$u=\frac{1}{2a}\left[ -v \pm \sqrt{v^2-4aw}\right]$$
把$u$带入$x^2+\frac{b}{a}x+\frac{c}{a}$,得:
$$x=\frac{1}{3a}\left[ -b \pm \sqrt{b^2-3ac}\right]-\frac{a}{3}$$
可以看出,上述求根公式是正确的。