From fa249cf48421bdd877fb494a3db82c1fa78a7430 Mon Sep 17 00:00:00 2001 From: JakubR Date: Sun, 23 May 2021 17:00:07 +0200 Subject: [PATCH] improved generate_data() method in parameter_json.py to not generate duplicates --- mines/disarming/parameter_json.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/mines/disarming/parameter_json.py b/mines/disarming/parameter_json.py index 84ef269..80e3b51 100644 --- a/mines/disarming/parameter_json.py +++ b/mines/disarming/parameter_json.py @@ -1,6 +1,7 @@ import json -import mine_parameters as param +import mines.disarming.mine_parameters as param import os +from random import choice # 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): 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): - new_params = json.dumps(param.MineParameters().jsonifyable_dict()) - file.write(new_params) + if len(data_set) == 0: + break + + random_params = data_set.pop() + file.write(random_params) file.write("\n") + del data_set + file.close() def main(): - generate_data("params3.txt", 20000) + generate_data("params4.txt", 1000) if __name__ == "__main__":