-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataSpawner.py
59 lines (49 loc) · 2.02 KB
/
DataSpawner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import pathlib
import random
from TemplateParser import parser as template_parser
def list_template(template_used):
def read_template(p: pathlib.Path):
raw_list = p.read_text().split("\n")
return [line for line in raw_list if len(line.strip()) > 0]
template_dct = {}
name_list = []
if template_used is None:
for path in pathlib.Path("templates").rglob("*.template"):
template_dct[path.name] = read_template(path)
name_list.append(path.name)
else:
for path in template_used:
path = pathlib.Path(path)
template_dct[path.name] = read_template(path)
name_list.append(path.name)
return template_dct, name_list
def gen_data(count, print_title=True, template_used=None, endless=False):
"""
生成供测评的 ASM 语句,返回一个生成器
:param endless: 是否在结尾主动插入死循环,默认为 False
:param template_used: 允许使用的模板文件列表,None 表示无限制
:param count: 数据组数
:param print_title: 是否打印标题信息(默认为 True)
:return: generator of ((index, template_file_name), asm_str)
"""
templ_dct, name_lst = list_template(template_used)
cycle, rest = divmod(count, len(name_lst))
name_lst = name_lst * cycle + random.choices(name_lst, k=rest)
random.shuffle(name_lst)
temp_path = pathlib.Path("temporary")
temp_path.mkdir(exist_ok=True)
def generator():
for i, name in enumerate(name_lst):
if print_title:
print(f"///// {i + 1} / {count}: {name} /////")
asm_path = temp_path / "test.asm"
tail = ["Endless"] if endless else []
asm_path.write_text(template_parser(templ_dct[name] + tail))
yield
return generator()
if __name__ == "__main__":
pass
# temp_path = pathlib.Path("temporary")
# temp_path.mkdir(exist_ok=True)
# for _ in gen_data(11):
# pass