2021-10-03 13:36:35 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
|
|
|
|
from random import choice
|
|
|
|
|
|
|
|
|
2021-10-09 04:30:38 +00:00
|
|
|
def complete_variable_configs(
|
|
|
|
variables, global_config: dict = {}, configs: dict = {}
|
|
|
|
) -> dict:
|
|
|
|
"""Completes variables configurations with the global configuration
|
2021-10-03 13:36:35 +00:00
|
|
|
|
2021-10-03 14:52:54 +00:00
|
|
|
:param variables: list of random variables to generate
|
|
|
|
:param global_config: global parameters
|
|
|
|
:param configs: global parameters
|
2021-10-03 13:36:35 +00:00
|
|
|
:return: complete variable scope
|
|
|
|
|
|
|
|
:example:
|
2021-10-03 14:52:54 +00:00
|
|
|
>>> completed = complete_variable_configs(["a", "b", "c", "d"],
|
|
|
|
... global_config={"rejected": [], "min_max": (-10, 10)},
|
|
|
|
... configs={
|
|
|
|
... "a": {"rejected": [0, 1]},
|
2021-10-03 13:36:35 +00:00
|
|
|
... "b": {"min_max": (-5, 0)},
|
2021-10-03 14:52:54 +00:00
|
|
|
... "c": {"rejected": [2], "min_max": (0, 5)}
|
|
|
|
... })
|
2021-10-09 04:30:38 +00:00
|
|
|
>>> completed["a"] == {'rejected': [0, 1], 'min_max': (-10, 10)}
|
2021-10-03 14:52:54 +00:00
|
|
|
True
|
|
|
|
>>> completed["b"] == {'rejected': [], 'min_max': (-5, 0)}
|
|
|
|
True
|
|
|
|
>>> completed['c'] == {'rejected': [2], 'min_max': (0, 5)}
|
|
|
|
True
|
|
|
|
>>> completed['d'] == {'rejected': [], 'min_max': (-10, 10)}
|
2021-10-03 13:36:35 +00:00
|
|
|
True
|
|
|
|
"""
|
2021-10-03 14:52:54 +00:00
|
|
|
complete_configs = configs.copy()
|
|
|
|
for variable in variables:
|
2021-10-03 13:36:35 +00:00
|
|
|
try:
|
2021-10-03 14:52:54 +00:00
|
|
|
complete_configs[variable]
|
2021-10-03 13:36:35 +00:00
|
|
|
except KeyError:
|
2021-10-03 14:52:54 +00:00
|
|
|
complete_configs[variable] = global_config
|
2021-10-03 13:36:35 +00:00
|
|
|
else:
|
2021-10-03 14:52:54 +00:00
|
|
|
complete_configs[variable] = dict(global_config, **configs[variable])
|
|
|
|
return complete_configs
|
2021-10-03 13:36:35 +00:00
|
|
|
|
2021-10-09 04:30:38 +00:00
|
|
|
|
2021-10-03 13:36:35 +00:00
|
|
|
def random_generator(
|
2021-10-09 04:30:38 +00:00
|
|
|
variables: list[str],
|
|
|
|
conditions: list[str] = [],
|
|
|
|
global_config: dict = {},
|
|
|
|
configs: dict = {},
|
|
|
|
) -> dict[str, int]:
|
|
|
|
"""Generate random variables
|
2021-10-03 13:36:35 +00:00
|
|
|
|
2021-10-03 14:52:54 +00:00
|
|
|
:param variables: list of random variables to generate
|
2021-10-03 13:36:35 +00:00
|
|
|
:param conditions: condition over variables
|
2021-10-03 14:52:54 +00:00
|
|
|
:param global_config: global parameters
|
|
|
|
:param configs: global parameters
|
2021-10-03 13:36:35 +00:00
|
|
|
:return: dictionnary of generated variables
|
|
|
|
|
|
|
|
:example:
|
2021-10-09 04:30:38 +00:00
|
|
|
>>> gene = random_generator(["a", "b"],
|
2021-10-03 13:36:35 +00:00
|
|
|
... ["a > 0"],
|
2021-10-03 14:52:54 +00:00
|
|
|
... {"rejected": [0], "min_max":(-10, 10)},
|
2021-10-03 13:36:35 +00:00
|
|
|
... {"a": {"rejected": [0, 1]},
|
2021-10-03 14:52:54 +00:00
|
|
|
... "b": {"min_max": (-5, 0)},
|
|
|
|
... })
|
2021-10-03 13:36:35 +00:00
|
|
|
>>> gene["a"] > 0
|
|
|
|
True
|
|
|
|
>>> gene["a"] != 0
|
|
|
|
True
|
|
|
|
>>> gene["b"] < 0
|
|
|
|
True
|
2021-10-09 04:30:38 +00:00
|
|
|
>>> gene = random_generator(["a", "b"],
|
2021-10-03 13:36:35 +00:00
|
|
|
... ["a % b == 0"],
|
2021-10-03 14:52:54 +00:00
|
|
|
... {"rejected": [0, 1], "min_max":(-10, 10)}
|
|
|
|
... )
|
2021-10-03 13:36:35 +00:00
|
|
|
>>> gene["a"] not in [0, 1]
|
|
|
|
True
|
|
|
|
>>> gene["b"] in list(range(-10, 11))
|
|
|
|
True
|
|
|
|
>>> gene["a"] % gene["b"]
|
|
|
|
0
|
|
|
|
"""
|
2021-10-09 04:30:38 +00:00
|
|
|
complete_scope = complete_variable_configs(variables, global_config, configs)
|
2021-10-03 13:36:35 +00:00
|
|
|
choices_list = {
|
|
|
|
v: list(
|
|
|
|
set(
|
|
|
|
range(
|
|
|
|
complete_scope[v]["min_max"][0], complete_scope[v]["min_max"][1] + 1
|
|
|
|
)
|
|
|
|
).difference(complete_scope[v]["rejected"])
|
|
|
|
)
|
2021-10-03 14:52:54 +00:00
|
|
|
for v in variables
|
2021-10-03 13:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# quantity_choices = reduce(lambda x,y : x*y,
|
|
|
|
# [len(choices_list[v]) for v in choices_list])
|
|
|
|
# TODO: améliorer la méthode de rejet avec un cache |dim. mai 12 17:04:11 CEST 2019
|
|
|
|
|
2021-10-03 14:52:54 +00:00
|
|
|
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
2021-10-03 13:36:35 +00:00
|
|
|
|
|
|
|
while not all([eval(c, __builtins__, generate_variable) for c in conditions]):
|
2021-10-03 14:52:54 +00:00
|
|
|
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
2021-10-03 13:36:35 +00:00
|
|
|
|
|
|
|
return generate_variable
|