Last active
December 2, 2019 18:00
-
-
Save BBarbosa/72c175f0b8de874b5396ec6382f8a751 to your computer and use it in GitHub Desktop.
Auto commands line generator from parameteres dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Auto commands line generator from parameteres dictionary. | |
Example: | |
>>> params_dict = { | |
"--model_path": ["model1.hdf5", "model2.hdf5"], | |
"--debug": True, | |
"--score": [0.1, 0.2], | |
"--iou": [0.3, 0.4] | |
} | |
>>> >>> recursive_dict_cross("python main.py", params_dict, run=False) | |
python main.py --model_path model1.hdf5 --debug True --score 0.1 --iou 0.3 | |
python main.py --model_path model1.hdf5 --debug True --score 0.1 --iou 0.4 | |
python main.py --model_path model1.hdf5 --debug True --score 0.2 --iou 0.3 | |
python main.py --model_path model1.hdf5 --debug True --score 0.2 --iou 0.4 | |
python main.py --model_path model2.hdf5 --debug True --score 0.1 --iou 0.3 | |
python main.py --model_path model2.hdf5 --debug True --score 0.1 --iou 0.4 | |
python main.py --model_path model2.hdf5 --debug True --score 0.2 --iou 0.3 | |
python main.py --model_path model2.hdf5 --debug True --score 0.2 --iou 0.4 | |
bbarbosa | |
29/11/2019 | |
""" | |
import os | |
def recursive_dict_cross(command_line, params, run=False): | |
"""Recursive dictionary cross to run system calls. | |
Args: | |
command_line (str): Command line string. | |
params (dict): Parameters dictionary. | |
run (bool, optional): Run command flag. Defaults to False. | |
Returns: | |
[type]: [description] | |
""" | |
if not params: | |
print(command_line) | |
if run: | |
os.system(command_line) | |
return [command_line] | |
key = list(params.keys())[0] | |
values = params[key] | |
params_copy = params.copy() | |
params_copy.pop(key) | |
new_command_line = "%s %s %s" | |
if isinstance(values, list): | |
commands_list = [] | |
for value in values: | |
commands_list += recursive_dict_cross(new_command_line % (command_line, key, str(value)), params_copy, run) | |
else: | |
return [] + recursive_dict_cross(new_command_line % (command_line, key, str(values)), params_copy, run) | |
return commands_list | |
def main(): | |
"""Main method.""" | |
params_dict = { | |
"--weights": "yolo", | |
"--model": ["model1", "model2"] | |
} | |
output = recursive_dict_cross("python yolo_detection/train.py", params_dict, run=False) | |
print("Output:", output) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment