From f181a0c6cf796f22c8b792cc8d7632bb2e352b20 Mon Sep 17 00:00:00 2001 From: MarkusJoe Date: Tue, 5 Apr 2022 20:48:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E8=AE=A1=E6=97=B6?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/db/__init__.py | 3 ++- app/decorators.py | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/decorators.py diff --git a/app/db/__init__.py b/app/db/__init__.py index a2da361..4412bf0 100644 --- a/app/db/__init__.py +++ b/app/db/__init__.py @@ -9,8 +9,10 @@ import os import sys import requests +from ..decorators import time_it +@time_it def download(): res = requests.get('https://markusjoe.github.io/static_file_hosting/data.sqlite') with open('./app/db/data.sqlite', 'wb') as fp: @@ -22,7 +24,6 @@ if __name__ != '__main__': print('数据库文件未找到, 正在下载中') try: download() - print('下载完成') except Exception as error: print('下载失败, 错误原因:', error) print('请手动下载数据库文件到 ./app/db/data.sqlite') diff --git a/app/decorators.py b/app/decorators.py new file mode 100644 index 0000000..d1d4dfc --- /dev/null +++ b/app/decorators.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +# -- coding:utf-8 -- +# @Author: markushammered@gmail.com +# @Development Tool: PyCharm +# @Create Time: 2022/4/5 +# @File Name: decorators.py + +import time +from functools import wraps + + +def time_it(func): + @wraps(func) + def wrapper(*args, **kwargs): + print('数据库文件开始下载') + start = time.time() + result = func(*args, **kwargs) + end = time.time() + print('下载结束, 耗时 {} 秒'.format(round(end - start, 2))) + return result + + return wrapper + + +