From c8cd7bbc0c87c16ec357cb8b2290efa4a71d113e Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:00:10 +0800 Subject: [PATCH 1/6] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/042=20restful/tf-35-app.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-app.py | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py deleted file mode 100644 index 848cbd6..0000000 --- a/基本结构/042 restful/tf-35-app.py +++ /dev/null @@ -1,23 +0,0 @@ -from flask import Flask, request -from cppy.cp_util import * - -''' -使用Flask构建一个简单的服务端 -该服务端能够接收POST请求,并返回词频统计结果 -先运行该代码搭建起服务端,然后运行request文件进行词频统计 -''' -app = Flask(__name__) - -@app.route('/word_frequency', methods=['POST']) -def word_frequency(): - # 获取POST请求中的JSON数据 - data = request.get_json() - data = data['file'] - # 统计词频 - word_freq = get_frequencies(data) - word_freq = sort_dict(word_freq) - return word_freq - -if __name__ == '__main__': - app.run(debug=True) - From ada14b9a7b04e46619608dd487724528e79a82fd Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:00:58 +0800 Subject: [PATCH 2/6] =?UTF-8?q?restful=20=E6=9C=8D=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-app.py | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py new file mode 100644 index 0000000..7bbf6f8 --- /dev/null +++ b/基本结构/042 restful/tf-35-app.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +from flask import Flask, request, jsonify, abort +from functools import lru_cache +from cppy.cp_util import * + +app = Flask(__name__) + +# 模拟数据库 +books_db = [] + +# 用于缓存用户数据库的装饰器 +@lru_cache(maxsize=None) +def get_books_db(): + return books_db + +#查询所有资源 +@app.route('/books', methods=['GET']) +def get_books(): + return jsonify(get_books_db()) + +#查询某个资源 +@app.route('/books/', methods=['GET']) +def get_book(book_id): + book = next((book for book in get_books_db() if book['id'] == book_id), None) + if book is None: + abort(404) + return jsonify(book['content']) + + +# 创建或更新新资源 +@app.route('/books/', methods=['PUT']) +def update_book(book_id): + global books_db + book_to_update = request.json + print(book_to_update) + books_db = get_books_db() + + book = next((book for book in books_db if book['id'] == book_id), None) + + if book is None: + books_db.append(book_to_update) + else: + book.update(book_to_update) + + return jsonify(books_db), 200 + +#操作一个资源 +@app.route('/books//word_frequency', methods=['GET']) +def word_frequency(book_id): + global books_db + book = next((book for book in get_books_db() if book['id'] == book_id), None) + filepath = book['content'] + word_list = extract_file_words(filepath) + word_frequency = get_frequencies(word_list) + word_frequency = sort_dict(word_frequency) + print_word_freqs(word_frequency) + # return jsonify({'message': f'User {book_id} deleted'}), 200 + return jsonify(word_frequency), 200 + +if __name__ == '__main__': + app.run(debug=True) + From 0b9d4a63d68a53a619d921561989bd5a1107bb1f Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:01:13 +0800 Subject: [PATCH 3/6] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/042=20restful/tf-35-request.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-request.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 基本结构/042 restful/tf-35-request.py diff --git a/基本结构/042 restful/tf-35-request.py b/基本结构/042 restful/tf-35-request.py deleted file mode 100644 index 235a88e..0000000 --- a/基本结构/042 restful/tf-35-request.py +++ /dev/null @@ -1,10 +0,0 @@ -import requests -from cppy.cp_util import * - -url = 'http://127.0.0.1:5000/word_frequency' -data_list = extract_file_words(testfilepath) -data = { - 'file': data_list -} -response = requests.post(url, json=data) -print(response.json()[:10]) From ffdae7d32944d91208a2c0572a288960ed5e1c21 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:01:48 +0800 Subject: [PATCH 4/6] =?UTF-8?q?restful=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-request.py | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 基本结构/042 restful/tf-35-request.py diff --git a/基本结构/042 restful/tf-35-request.py b/基本结构/042 restful/tf-35-request.py new file mode 100644 index 0000000..8fa98c1 --- /dev/null +++ b/基本结构/042 restful/tf-35-request.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +import requests +from cppy.cp_util import * + +# 查询资源,得到空列表 +url = 'http://127.0.0.1:5000//books' +response = requests.get(url) +print(response.json()) +time.sleep(2) + +# - 创建一个1号资源 +print('创建一个1号资源') +book_1 = {"id": 1, "title": "Python编程:从入门到实践", "content": testfilepath} +url = 'http://127.0.0.1:5000/books/1' +response = requests.put(url,json=book_1) +time.sleep(2) + +# - 创建一个2号资源(修改testfilepaht变量) +print('创建一个2号资源') +testfilepath = testfilepath.replace('Prey.txt','Pride-and-Prejudice.txt') +book_2 = {"id": 2, "title": "深入浅出计算机组成原理", "content": testfilepath} +url = 'http://127.0.0.1:5000/books/2' +response = requests.put(url,json=book_2) +time.sleep(2) + +# - 创建一个3号资源(修改testfilepaht变量)正好有3个文件 +print('创建一个3号资源') +testfilepath = testfilepath.replace('Pride-and-Prejudice.txt','test.txt') +book_3 = {"id": 3, "title": "算法导论", "content": testfilepath} +url = 'http://127.0.0.1:5000/books/3' +response = requests.put(url,json=book_3) +time.sleep(2) + +# - 查询资源,看到结果 +print('查询资源,看到结果') +url = 'http://127.0.0.1:5000//books' +response = requests.get(url) +print(response.json()) +time.sleep(2) + +# - 操作1号资源,得到词频 +print('操作1号资源,得到词频') +url = 'http://127.0.0.1:5000/books/1/word_frequency' +response = requests.get(url) +print_word_freqs(response.json()) \ No newline at end of file From fdf6166100adecca1ba848360b58f9166c05902a Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:13:55 +0800 Subject: [PATCH 5/6] =?UTF-8?q?Delete=20'=E5=9F=BA=E6=9C=AC=E7=BB=93?= =?UTF-8?q?=E6=9E=84/042=20restful/tf-35-app.py'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-app.py | 62 --------------------------- 1 file changed, 62 deletions(-) delete mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py deleted file mode 100644 index 7bbf6f8..0000000 --- a/基本结构/042 restful/tf-35-app.py +++ /dev/null @@ -1,62 +0,0 @@ -# -*- coding: utf-8 -*- -from flask import Flask, request, jsonify, abort -from functools import lru_cache -from cppy.cp_util import * - -app = Flask(__name__) - -# 模拟数据库 -books_db = [] - -# 用于缓存用户数据库的装饰器 -@lru_cache(maxsize=None) -def get_books_db(): - return books_db - -#查询所有资源 -@app.route('/books', methods=['GET']) -def get_books(): - return jsonify(get_books_db()) - -#查询某个资源 -@app.route('/books/', methods=['GET']) -def get_book(book_id): - book = next((book for book in get_books_db() if book['id'] == book_id), None) - if book is None: - abort(404) - return jsonify(book['content']) - - -# 创建或更新新资源 -@app.route('/books/', methods=['PUT']) -def update_book(book_id): - global books_db - book_to_update = request.json - print(book_to_update) - books_db = get_books_db() - - book = next((book for book in books_db if book['id'] == book_id), None) - - if book is None: - books_db.append(book_to_update) - else: - book.update(book_to_update) - - return jsonify(books_db), 200 - -#操作一个资源 -@app.route('/books//word_frequency', methods=['GET']) -def word_frequency(book_id): - global books_db - book = next((book for book in get_books_db() if book['id'] == book_id), None) - filepath = book['content'] - word_list = extract_file_words(filepath) - word_frequency = get_frequencies(word_list) - word_frequency = sort_dict(word_frequency) - print_word_freqs(word_frequency) - # return jsonify({'message': f'User {book_id} deleted'}), 200 - return jsonify(word_frequency), 200 - -if __name__ == '__main__': - app.run(debug=True) - From 28f60e8216b276e0a4d5c042a86b57ce5e46b3c2 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:14:35 +0800 Subject: [PATCH 6/6] =?UTF-8?q?restful=20=E6=9C=8D=E5=8A=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-app.py | 76 +++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 基本结构/042 restful/tf-35-app.py diff --git a/基本结构/042 restful/tf-35-app.py b/基本结构/042 restful/tf-35-app.py new file mode 100644 index 0000000..661e7de --- /dev/null +++ b/基本结构/042 restful/tf-35-app.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +from flask import Flask, request, jsonify, abort +from functools import lru_cache +from cppy.cp_util import * +from functools import cache + +app = Flask(__name__) + +# 模拟数据库 +books_db = [] + +# 用于缓存用户数据库的装饰器 +@lru_cache(maxsize=None) +def get_books_db(): + return books_db + +#查询所有资源 +@app.route('/books', methods=['GET']) +def get_books(): + return jsonify(get_books_db()) + +#查询某个资源 +@app.route('/books/', methods=['GET']) +def get_book(book_id): + book = next((book for book in get_books_db() if book['id'] == book_id), None) + if book is None: + abort(404) + return jsonify(book['content']) + + +# 创建或更新新资源 +@app.route('/books/', methods=['PUT']) +def update_book(book_id): + global books_db + book_to_update = request.json + print(book_to_update) + books_db = get_books_db() + + book = next((book for book in books_db if book['id'] == book_id), None) + + if book is None: + # 如果资源不存在,创建新资源 + books_db.append(book_to_update) + else: + # 如果资源存在,更新资源 + book.update(book_to_update) + # 清除缓存的数据库 + cache.delete(get_books_db) + + return jsonify(books_db), 200 + +#操作一个资源 +@app.route('/books//word_frequency', methods=['GET']) +def word_frequency(book_id): + global books_db + book = next((book for book in get_books_db() if book['id'] == book_id), None) + filepath = book['content'] + word_list = extract_file_words(filepath) + word_frequency = get_frequencies(word_list) + word_frequency = sort_dict(word_frequency) + print_word_freqs(word_frequency) + return jsonify(word_frequency), 200 + +@app.route('/books/', methods=['DELETE']) +def delete_book(book_id): + global books_db + books_db = [book for book in books_db if book['id'] != book_id] + + if len(books_db) == len([l for l in books_db if l['id'] == book_id]): + abort(404) # 用户不存在 + + return jsonify({'message': f'book {book_id} deleted'}), 200 + +if __name__ == '__main__': + app.run(debug=True) +