5 Star 0 Fork 0

梁泽钜/resnet50

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
task_job.py 8.59 KB
一键复制 编辑 原始数据 按行查看 历史
import datetime
import json
import os
import random
import sqlite3
import time
from flask import Blueprint, request, jsonify
from flask_apscheduler import APScheduler
from werkzeug.utils import secure_filename
from tools.OsUtils import execute_cmd, list_files
job_index = Blueprint('job_index', __name__)
job_index.url_prefix = '/job'
scheduler = APScheduler()
scheduler.start()
def getRandomBank(conn):
cursor = conn.cursor()
# 创建表
sql = "select distinct number from question_bank "
cursor.execute(sql)
banks = cursor.fetchall()
range = random.randint(0, len(banks) - 1)
return banks[range][0]
def dbConnect():
print("Opened database successfully")
return sqlite3.connect('garbage.db')
# 关闭连接
def createTable(conn):
# 创建游标
cursor = conn.cursor()
# 创建表
sql = 'CREATE TABLE question(id integer PRIMARY KEY autoincrement, description text,option_a text,' \
'option_b text,option_c text,option_d text,answer integer,bank_no varchar(32))'
cursor.execute(sql)
conn.commit()
sql = 'CREATE TABLE question_bank(id integer PRIMARY KEY autoincrement,desc text,number varchar(32))'
cursor.execute(sql)
conn.commit()
def task1(a, b):
day = time.strftime("%Y-%m-%d", time.localtime())
base_path = os.path.dirname(__file__)
test_image = os.path.join(base_path, 'test_image')
image_package = os.path.join(base_path, 'static/image_package')
package_image = 'zip -q -r {}image{}.zip {}'.format(image_package, day, test_image)
execute_cmd(package_image)
rm_image = 'rm -rf {}/*'.format(test_image)
execute_cmd(rm_image)
print("collection and zip images", day)
return "success"
def task2(a, b):
print('mission_2_', a, b)
print(datetime.datetime.now())
@job_index.route('/pause', methods=['GET'])
def pause_job(): # 暂停
job_id = request.args.get('id')
scheduler.pause_job(str(job_id))
return "pause success!"
@job_index.route('/resume', methods=['GET'])
def resume_job(): # 恢复
job_id = request.args.get('id')
scheduler.resume_job(str(job_id))
return "Success!"
@job_index.route('/get_jobs', methods=['GET'])
def get_task(): # 获取
# job_id = request.args.get('id')
jobs = scheduler.get_jobs()
return jsonify(jobs)
@job_index.route('/list_files', methods=['GET'])
def list_image_files():
base_path = os.path.dirname(__file__)
static_path = os.path.join(base_path, 'static/image_package')
files = list_files(static_path)
content = ""
for file in files:
link = "/static/image_package/{}".format(file)
a_link = '<a href="{}">{}</a>'.format(link, file)
content += a_link
return content
@job_index.route('/test', methods=['GET'])
def test():
task1(1, 1)
return "success"
@job_index.route('/remove_job', methods=['GET'])
def remove_job():
job_id = request.args.get('id')
scheduler.remove_job(str(job_id))
return 'remove success'
# /add_job?id=2
@job_index.route('/add_job', methods=['GET'])
def add_task():
data = request.args.get('id')
if data == '1':
# trigger='cron' 表示是一个定时任务
scheduler.add_job(func=task1, id='1', args=(1, 1), trigger='cron', day_of_week='0-6', hour=21, minute=25,
second=00, replace_existing=True)
else:
# trigger='interval' 表示是一个循环任务,每隔多久执行一次
scheduler.add_job(func=task2, id='2', args=(2, 2), trigger='interval', seconds=10,
replace_existing=True)
return 'success'
@job_index.route('/upload', methods=['POST'])
def upload():
f = request.files['file']
filename = f.filename
allowed_file = '.' in filename and filename.rsplit('.', 1)[1] in ["csv"]
if not (f and allowed_file):
return jsonify({"error": 1001, "msg": "请检查上传的图片类型,仅限于csv"})
base_path = os.path.dirname(__file__)
upload_path = os.path.join(base_path, 'test_question', secure_filename(f.filename)) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径
f.save(upload_path)
return 'remove success'
@job_index.route('/data', methods=['GET'])
def question_data():
conn = dbConnect()
cursor = conn.cursor()
# 创建表
bank_no = getRandomBank(conn)
sql = "select * from question where bank_no='{}'".format(bank_no)
cursor.execute(sql)
questions = cursor.fetchall()
results = []
for item in questions:
result = {
"id": item[0], "question": item[1], "answers": [item[2], item[3], item[4], item[5]]
}
results.append(result)
print(results)
return jsonify(results)
def sortKey(el):
return len(el["title"])
if __name__ == '__main__':
# result = []
# with open('C:\\Users\\zhougr2\\Desktop\\test.csv', 'r', encoding="utf8") as f:
# reader = csv.reader(f)
# print(type(reader))
# result = list(reader)
# for row in result:
# print(row)
# data()
data = [{"id": "3809", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "4009", "uniacid": "7", "openid": "o_Eaq5OljJ0bzvksvbLsE-Hm2DH8", "catid": "1", "title": "\u7b14\u7b52", "create_time": "1636024096", "name": "\u53ef\u56de\u6536\u7269", "ljbg": "grace-bg-blue"}, {"id": "3808", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u888b", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3807", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u76d6", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3806", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u6746", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3805", "uniacid": "7", "openid": "", "catid": "1", "title": "\u7b14\u76d2", "create_time": "1615039982", "name": "\u53ef\u56de\u6536\u7269", "ljbg": "grace-bg-blue"}, {"id": "3800", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u58f3", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3799", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u56ca", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3798", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u5957", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3797", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u5c51", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3796", "uniacid": "7", "openid": "", "catid": "4", "title": "\u7b14\u82af", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3674", "uniacid": "7", "openid": "", "catid": "4", "title": "\u5f69\u7b14", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3911", "uniacid": "7", "openid": "", "catid": "4", "title": "\u767d\u677f\u7b14", "create_time": "1615039983", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3804", "uniacid": "7", "openid": "", "catid": "1", "title": "\u7b14\u8bb0\u672c", "create_time": "1615039982", "name": "\u53ef\u56de\u6536\u7269", "ljbg": "grace-bg-blue"}, {"id": "3670", "uniacid": "7", "openid": "", "catid": "4", "title": "\u5f69\u8272\u7b14", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "3801", "uniacid": "7", "openid": "", "catid": "1", "title": "\u7b14\u8bb0\u672c\u7eb8", "create_time": "1615039982", "name": "\u53ef\u56de\u6536\u7269", "ljbg": "grace-bg-blue"}, {"id": "3668", "uniacid": "7", "openid": "", "catid": "4", "title": "\u5f69\u8272\u94c5\u7b14", "create_time": "1615039982", "name": "\u5176\u4ed6\u5783\u573e", "ljbg": "grace-bg-gray"}, {"id": "4032", "uniacid": "7", "openid": "o_Eaq5C6smWgmAAIExsWCXr46Nl8", "catid": "1", "title": "\u7b14\u8bb0\u672c\u914d\u4ef6", "create_time": "1637658905", "name": "\u53ef\u56de\u6536\u7269", "ljbg": "grace-bg-blue"}, {"id": "3803", "uniacid": "7", "openid": "", "catid": "1", "title": "\u7b14\u8bb0\u672c\u7535\u8111", "create_time": "1615039982", "name": "\u53ef\u56de\u6536\u7269", "ljbg": "grace-bg-blue"}, {"id": "3802", "uniacid": "7", "openid": "", "catid": "2", "title": "\u7b14\u8bb0\u672c\u7535\u8111\u7535\u6c60", "create_time": "1615039982", "name": "\u6709\u5bb3\u5783\u573e", "ljbg": "grace-bg-red"}]
# data.sort(key=sortKey)
print(data)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zeju/resnet50.git
git@gitee.com:zeju/resnet50.git
zeju
resnet50
resnet50
master

搜索帮助