improved generate_data() method in parameter_json.py to not generate duplicates

This commit is contained in:
JakubR 2021-05-23 17:00:07 +02:00
parent c7845a0d1f
commit fa249cf484

View File

@ -1,6 +1,7 @@
import json import json
import mine_parameters as param import mines.disarming.mine_parameters as param
import os import os
from random import choice
# this module is self contained, used to generate a json file # this module is self contained, used to generate a json file
@ -21,17 +22,27 @@ def param_presents():
def generate_data(file_name: str, lines: int): def generate_data(file_name: str, lines: int):
file = open(os.path.join(DIR_DATA, file_name), "w") file = open(os.path.join(DIR_DATA, file_name), "w")
data_set = set()
for x in range(100000):
new_params = json.dumps(param.MineParameters().jsonifyable_dict())
data_set.add(new_params)
for x in range(lines): for x in range(lines):
new_params = json.dumps(param.MineParameters().jsonifyable_dict()) if len(data_set) == 0:
file.write(new_params) break
random_params = data_set.pop()
file.write(random_params)
file.write("\n") file.write("\n")
del data_set
file.close() file.close()
def main(): def main():
generate_data("params3.txt", 20000) generate_data("params4.txt", 1000)
if __name__ == "__main__": if __name__ == "__main__":