flask高级编程-循环引用

本文介绍使用Flask框架进行路由配置及视图函数编写的方法,包括如何定义URL参数、视图函数的设计原则、代码优化技巧等内容,并通过具体示例展示了不同场景下的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

16、 路径加了<>,就会被识别为一个参数,而不是固定的url字符串
  16.1 编程原则:视图函数里面要尽可能间接,函数要见名知意,不能将细节全部写到视图函数里面,那样是强迫让所有看代码的人都来看细节,不对
# -*- coding=utf-8 -*-
 
from flask import Flask, make_response
 
from helper import is_isbn_or_key
 
app = Flask(__name__)
app.config.from_object('config')
 
 
@app.route('/book/search/<q>/<page>')
def hello(q, page):
isbn_or_key = is_isbn_or_key(q)
pass
 
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=app.config['DEBUG'], port=5000)
fisher.py
# -*- coding=utf-8 -*-
 
def is_isbn_or_key(word):
isbn_or_key = 'key'
if len(word) == 13 and word.isdigit():
isbn_or_key = 'isbn'
short_word = word.replace('-', '')
if '-' in q and len(short_word) == 10 and short_word.isdigit():
isbn_or_key = 'isbn'
return isbn_or_key
helper.py

  16.2 简化代码,if else

# -*- coding=utf-8 -*-
 
import requests
 
 
class HTTP:
def get(self, url, return_json=True):
r = requests.get(url)
if r.status_code != 200:
return {} if return_json else ''
return r.json() if return_json else r.text
httper.py
  16.3 get方法传进来self,但是函数中没有用到
# -*- coding=utf-8 -*-
 
import requests
 
 
class HTTP:
@staticmethod
def get(url, return_json=True):
r = requests.get(url)
if r.status_code != 200:
return {} if return_json else ''
return r.json() if return_json else r.text
httper.py
  16.4 staticmethod与classmethod
    都表示静态,classmethod是类的方法,如果没有用到类里面的相关变量,没有必要用到classmethod
  16.5 clss HTTP与class HTTP(object)
    python3中写不写object没有区别;python2中有区别
  16.6 alt+enter键,自动导入类
# -*- coding=utf-8 -*-
import json
 
from flask import Flask
 
from helper import is_isbn_or_key
from yushu_book import YuShuBook
 
app = Flask(__name__)
app.config.from_object('config')
 
 
@app.route('/book/search/<q>/<page>')
def hello(q, page):
isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = YuShuBook.search_by_isbn(q)
else:
result = YuShuBook.search_by_keyword(q)
return json.dumps(result), 200, {'content-type': 'application/json'}
 
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=app.config['DEBUG'], port=5000)
fisher.py
# -*- coding=utf-8 -*-
 
from http import HTTP
 
 
class YuShuBook:
isbn_url = 'http://t.yushu.im/v2/book/isbn/{}'
keyword_url = 'http://t.yushu.im/v2/book/search?q={}&count={}&start={}'
 
@classmethod
def search_by_isbn(cls, isbn):
url = cls.isbn_url.format(isbn)
result = HTTP.get(url)
return result
 
@classmethod
def search_by_keyword(cls, keyword, count=15, start=0):
url = cls.isbn_url.format(keyword, count, start)
result = HTTP.get(url)
return result
yushu_book.py
# -*- coding=utf-8 -*-
 
def is_isbn_or_key(word):
isbn_or_key = 'key'
if len(word) == 13 and word.isdigit():
isbn_or_key = 'isbn'
short_word = word.replace('-', '')
if '-' in q and len(short_word) == 10 and short_word.isdigit():
isbn_or_key = 'isbn'
return isbn_or_key
helper.py
# -*- coding=utf-8 -*-
 
import requests
 
 
class HTTP:
@staticmethod
def get(url, return_json=True):
r = requests.get(url)
if r.status_code != 200:
return {} if return_json else ''
return r.json() if return_json else r.text
httper.py
# -*- coding=utf-8 -*-
 
DEBUG = True
config.py
  16.7 return jsonify(result)相当于return json.dumps(result), 200, {'content-type': 'application/json'}
# -*- coding=utf-8 -*-
import json
 
from flask import Flask, jsonify
 
from helper import is_isbn_or_key
from yushu_book import YuShuBook
 
app = Flask(__name__)
app.config.from_object('config')
 
 
@app.route('/book/search/<q>/<page>')
def hello(q, page):
isbn_or_key = is_isbn_or_key(q)
if isbn_or_key == 'isbn':
result = YuShuBook.search_by_isbn(q)
else:
result = YuShuBook.search_by_keyword(q)
return jsonify(result)
 
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=app.config['DEBUG'], port=5000)
fisher.py
  16.8 循环引用
    fisher.py from app.web import book
    book.py from fisher import app
    fisher模块执行两次,原因,第一次作为入口主文件被执行;第二次作为导入模块被执行
    book执行一次,原因,python中模块只会被导入一次

转载于:https://www.cnblogs.com/wangmingtao/p/9295972.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值