设计出python_Python: 实际项目中抽象出的小项目设计
題圖.png
Python: 一周筆記
本文根據實際項目中的一部分api 設計抽象出來,實例化成一個簡單小例子,暫且叫作「學生管理系統」。
這個系統主要完成下面增刪改查的功能:
包括:
學校信息的管理
教師信息的管理
學生信息的管理
根據Api請求的動作:
POST: 增加信息
GET: 查詢信息
PUT: 更新信息
DELETE: 刪除信息
可以以下面一個簡單的實例看這個系統完成什么樣的工作:
from flask import Flask
app = Flask(__name__)
@app.route('/api.example.com', methods=['POST'])
def hello():
return make_response(jsonify(resource=request.json), 200)
if __name__ == "__main__":
app.run()
發送curl 命令:
curl -i http://127.0.0.1:5000/api.example.com -H "Content-Type: application/json" -d '{"usage": "for example", "method": "post", "author": "xiewei"}'
返回結果:
{
"resource": {
"author": "xiewei",
"method": "post",
"usage": "for example"
}
}
其他的動作類似的發送api 請求,完成相應的動作,真實的項目往往后加上數據庫的操作。
整個流程主要包括:
restful api 設計
數據表設計
開發:目錄的結構、一致性
結果展示
1. Restful API 設計
主要包括URL 設計和 狀態碼的設計:
CODE
DES
200
成功
404
錯誤信息
500
服務器錯誤信息
POST:
Method
URL
Content-Type
Des
POST
/api..example.com//creation
application/json
create info
school_post: "/api.school.example.com/v1/creation"
teacher_post: "/api.teacher.example.com/v1/creation"
student_post: "/api.student.example.com/v1/creation"
GET:
Method
URL
Content-Type
Des
GET
/api..example.com//
application/json
get info
school_get: "/api.school.example.com/v1/name"
teacher_get: "/api.teacher.example.com/v1/name"
student_get: "/api.student.example.com/v1/name"
PUT:
Method
URL
Content-Type
Des
PUT
/api..example.com//
application/json
update info
school_put: "/api.school.example.com/v1/name"
teacher_put: "/api.teacher.example.com/v1/name"
student_put: "/api.student.example.com/v1/name"
DELETE:
Method
URL
Content-Type
Des
DELETE
/api..example.com//
application/json
delete info
school_delete: "/api.school.example.com/v1/name"
teacher_delete: "/api.teacher.example.com/v1/name"
student_delete: "/api.student.example.com/v1/name"
2. 數據庫設計
主要包括:表設計,字段類型等的設計,鑒于篇幅有限只展示School 表:
School:
Field
Type
NULL
Key
Default
Extra
Des
id
Integer
Fasle
PRI
NULL
auto_inc
id
name
string
False
NULL
info_name
student_number
Integer
Flase
NULL
number of student
grade
string
False
NULL
grade include
teacher_number
Integer
False
NULL
number of teacher
principal
string
False
NULL
name of principal
vice_principal
string
False
NULL
name of vice_prin
vice_principal_number
Integer
False
NULL
number
slogan
string
False
NULL
slogan
3. 開發
目錄結構:考慮兩個因素
耦合性
擴展性
數據庫操作和業務操作分開,提供接口:
├─api
│ ├─res
│ │ ├─v1
├─cmd
├─data
├─db
├─info
├─models
├─tests
│ ├─api
│ │ ├─res
│ ├─common
└─util
api:數據庫API, flask_app api
cmd: 創建數據庫操作
data: sqlite 數據表文件
db: 具體數據庫增刪改查
info: api 傳遞的body的具體實例
models: 數據庫表結構定義
tests: 測試
util: 提供項目公共函數方法
數據庫API:
主要使用sqlalchemy 的增刪改查進行封裝:
- add
- query
- update
- delete
以add 實例:
def add(self, dbt, **body):
try:
self.session.add(dbt(**body))
self.session.commit()
return 'OK'
except Exception as e:
self.session.rollback()
return str(e)
flask_api:
主要使用flask 定義不同動作的接口:
- POST
- GET
- DELETE
- UPDATE
以POST示例:
@app.route('/api..example.com//creation', methods=['POST'])
def post(res, ver):
try:
body = request.json
try:
resource = get_import(res, ver)
if resource:
ack, rt = resource.post(body)
if ack == 'OK':
post_rt = make_response(jsonify(resource=rt), 200)
else:
post_rt = make_response(jsonify(error=str(rt)), 404)
return post_rt
except Exception as e:
return 'body is not valid json'
except Exception as e:
return make_response(str(e), 500)
4. 工具
restful api 發送請求命令工具:
POSTMAN: chrome 瀏覽器插件
RESTClient: 火狐瀏覽器插件
服務器端:用curl 命令吧
POSTMAN 示例圖:
postman.png
5. 結果展示
發送請求(api) –》 數據庫增刪改查(數據持久化) –》 返回Json 格式的數據和狀態碼(展示)
curl -i http://127.0.0.1:5000/api.school.example.com/v1/creation -H "Content-Type: application/json" -d '{"name": "xiewei22","student_number": 1982,"grade": "[G1, G2, G3]","teacher_number": 102,"principal": "xiewei2","vice_principal": "wenlimin2","vice_principal_number": 12,"slogan": "friendly2"}'
result:
{
"resource": [
{
"grade": "[G1, G2, G3]",
"id": 1,
"name": "xiewei22",
"principal": "xiewei2",
"slogan": "friendly2",
"student_number": 1982,
"teacher_number": 102,
"vice_principal": "wenlimin2",
"vice_principal_number": 12
}
]
}
數據庫 school 表中插入這條數據,并把插入的數據信息展示出來。
school_post.png
code:
下一篇根據這個項目進行講解如何進行測試:并講解python 測試。
unittest
mock
tox
coverage
nose
總結
以上是生活随笔為你收集整理的设计出python_Python: 实际项目中抽象出的小项目设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python函数调用位置_Python:
- 下一篇: c 数据压缩算法_CCSDS图像压缩算法