From 77559364e25f0a759347ce790f1fad2511c4f3ee Mon Sep 17 00:00:00 2001 From: halliday Date: Fri, 26 Jan 2024 13:35:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=86=E5=88=86=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/DBConfig.py | 2 +- bin/fileDataCreating.py | 21 ++++++++++++++++----- etc/.gitkeep | 0 etc/{test_config.yml => db_config.yml} | 9 ++++++--- etc/file_config.yml | 11 +++++++++++ 5 files changed, 34 insertions(+), 9 deletions(-) delete mode 100644 etc/.gitkeep rename etc/{test_config.yml => db_config.yml} (76%) create mode 100644 etc/file_config.yml diff --git a/bin/DBConfig.py b/bin/DBConfig.py index 2bb5b5a..0379c09 100644 --- a/bin/DBConfig.py +++ b/bin/DBConfig.py @@ -10,7 +10,7 @@ from datetime import datetime # 定义公共的部分 path = Path(__file__) etc_dir = path.parent.parent / "etc" -config_file_patch = etc_dir / "test_config.yml" +config_file_patch = etc_dir / "db_config.yml" # 获取配置文件 diff --git a/bin/fileDataCreating.py b/bin/fileDataCreating.py index 0b8282d..3bc1766 100644 --- a/bin/fileDataCreating.py +++ b/bin/fileDataCreating.py @@ -3,6 +3,7 @@ import _load import faker_data import csv +import yaml from schedule import every, repeat, run_pending from pathlib import Path @@ -20,10 +21,20 @@ from random import choice """ #TODO(MH):目前这些参数是写死的,后续看使用情况,也可以去读配置文件的 - # 定义公共的部分 path = Path(__file__) +etc_dir = path.parent.parent / "etc" +config_file_patch = etc_dir / "file_config.yml" + +# 获取配置文件 +with open(config_file_patch, "r", encoding='utf-8') as fy: + config = yaml.safe_load(fy) + +# 默认为当前的目录 data_dir = path.parent.parent / "data" +if 'DATADIR' in config.keys() and config['DATADIR']: + data_dir = Path(config['DATADIR']) + # 初始化目录 if not data_dir.exists(): # shutil.rmtree(data_dir) @@ -59,13 +70,13 @@ def new(): faker_data.save_data_csv(finfo[4], lines=100) -# 每2秒插入10条数据 +# 每2秒插入N条数据 @repeat(every(2).seconds) def inserting(): finfo = file_info() if finfo[4].exists(): print('insert...') - datas = faker_data.faker_data(lines=200)[1:] + datas = faker_data.faker_data(lines=config['InsertRows'])[1:] with open(finfo[4], 'a+', encoding='utf-8', newline='') as file_csv: writer = csv.writer(file_csv, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) @@ -85,10 +96,10 @@ def deleting_data(): if len(files) > 1: file = choice(files[:-1]) print(str(file) + "start delete data ....") - # 删除掉前100条数据 + # 删除掉前N条数据 with open(file, 'rb') as fr: data = fr.readlines() - new_data = data[100:] + new_data = data[config['DeleteRows']:] # 少于100条的不删除 if len(new_data) > 100: with open(file, 'wb') as fw: diff --git a/etc/.gitkeep b/etc/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/etc/test_config.yml b/etc/db_config.yml similarity index 76% rename from etc/test_config.yml rename to etc/db_config.yml index db71fb6..796af9b 100644 --- a/etc/test_config.yml +++ b/etc/db_config.yml @@ -1,6 +1,7 @@ #数据库配置文件 -# 数据库类型,目前仅支持MySQL和Oracle +# 通用配置 +# 数据库类型,目前仅支持MySQL和Oracle,注意大小写 DBType: Oracle # 数据库地址 IP: "10.10.29.41" @@ -11,7 +12,9 @@ DBUSER: "test" DBPASSWD: "test123" # 库名 DATABASES: "TEST" -# 服务名-Oracle -SERVER: "orcl" # 每秒中插入多少条数据 InsertRows: 200 + + +# 服务名-Oracle +SERVER: "orcl" diff --git a/etc/file_config.yml b/etc/file_config.yml new file mode 100644 index 0000000..cb63a61 --- /dev/null +++ b/etc/file_config.yml @@ -0,0 +1,11 @@ +#生成文件的配置文件 +vsersion : 1.3 + +# 生成csv输出的目录,默认为程序的运行目录。需要自定义就打开配置 +#DATADIR : "D:\\data" + +# 每次插入多少条数据 +InsertRows: 200 +# 每次删除多少条数据 +DeleteRows: 100 +