拆分配置文件

This commit is contained in:
halliday 2024-01-26 13:35:05 +08:00
parent 480cc74e6f
commit 77559364e2
5 changed files with 34 additions and 9 deletions

View File

@ -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"
# 获取配置文件

View File

@ -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:

View File

View File

@ -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"

11
etc/file_config.yml Normal file
View File

@ -0,0 +1,11 @@
#生成文件的配置文件
vsersion : 1.3
# 生成csv输出的目录默认为程序的运行目录。需要自定义就打开配置
#DATADIR : "D:\\data"
# 每次插入多少条数据
InsertRows: 200
# 每次删除多少条数据
DeleteRows: 100