Форум сайта python.su
import os from flask import Flask, request from werkzeug.exceptions import BadRequest app = Flask(__name__) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_DIR = os.path.join(BASE_DIR, "data") def build_query(it, teg, znachenie): res = map(lambda v: v.strip(), it) if teg == "filter": res = filter(lambda v, txt=znachenie: txt in v, res) if teg == "map": arg = int(znachenie) res = map(lambda v, idx=arg: v.split(" ")[idx], res) if teg == "unique": res = set(res) if teg == "sort": reverse = znachenie == "desc" res = sorted(res, reverse=reverse) if teg == "limit": arg = int(znachenie) res = list(res)[:arg] return res @app.route("/perform_query") def perform_query(): try: teg1 = request.args["teg1"] teg2 = request.args["teg2"] znachenie1 = request.args["znachenie1"] znachenie2 = request.args["znachenie2"] file_name = request.args["file_name"] except KeyError: raise BadRequest file_path = os.path.join(DATA_DIR, file_name) if not os.path.exists(file_path): return BadRequest(description=f"{file_name} was not found") with open(file_path) as last: res = build_query(last, teg1, znachenie1) res = build_query(res, teg2, znachenie2) content = '\n'.join(res) print(content) return app.response_class(content, content_type="text/plain") if __name__ == '__main__': app.run(debug=True)
Отредактировано bul_sup (Май 9, 2022 01:47:55)
Офлайн