20180711-flask高级编程-循环引用

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中模块只会被导入一次

猜你喜欢

转载自www.cnblogs.com/wangmingtao/p/9295972.html