标签:python cme orm book web 不能 mat format 识别
# -*- 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)
# -*- 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
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
# -*- 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
# -*- 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)
# -*- 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
# -*- 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
# -*- 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
# -*- coding=utf-8 -*- DEBUG = True
# -*- 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)
标签:python cme orm book web 不能 mat format 识别
原文地址:https://www.cnblogs.com/wangmingtao/p/9295972.html