Fix: format with black
This commit is contained in:
parent
a267acd7b3
commit
aad2395a3a
@ -10,7 +10,9 @@
|
||||
from random import choice
|
||||
|
||||
|
||||
def complete_variable_configs(variables, global_config:dict={}, configs:dict={})->dict:
|
||||
def complete_variable_configs(
|
||||
variables, global_config: dict = {}, configs: dict = {}
|
||||
) -> dict:
|
||||
"""Completes variables configurations with the global configuration
|
||||
|
||||
:param variables: list of random variables to generate
|
||||
@ -47,7 +49,10 @@ def complete_variable_configs(variables, global_config:dict={}, configs:dict={})
|
||||
|
||||
|
||||
def random_generator(
|
||||
variables:list[str], conditions:list[str]=[], global_config:dict={}, configs:dict={},
|
||||
variables: list[str],
|
||||
conditions: list[str] = [],
|
||||
global_config: dict = {},
|
||||
configs: dict = {},
|
||||
) -> dict[str, int]:
|
||||
"""Generate random variables
|
||||
|
||||
@ -81,9 +86,7 @@ def random_generator(
|
||||
>>> gene["a"] % gene["b"]
|
||||
0
|
||||
"""
|
||||
complete_scope = complete_variable_configs(
|
||||
variables, global_config, configs
|
||||
)
|
||||
complete_scope = complete_variable_configs(variables, global_config, configs)
|
||||
choices_list = {
|
||||
v: list(
|
||||
set(
|
||||
@ -105,5 +108,3 @@ def random_generator(
|
||||
generate_variable = {v: choice(choices_list[v]) for v in variables}
|
||||
|
||||
return generate_variable
|
||||
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
|
||||
def extract_letters(words: list[str]) -> set[str]:
|
||||
"""Extracts unique letters from a list of words
|
||||
|
||||
@ -32,4 +31,3 @@ def eval_words(words:list[str], values:dict[str,int]) -> dict[str, int]:
|
||||
{'a': 2, 'a*k': 6}
|
||||
"""
|
||||
return {word: eval(word, values) for word in words}
|
||||
|
||||
|
@ -111,8 +111,7 @@ def filter_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks
|
||||
|
||||
class FilterRandom(object):
|
||||
|
||||
""" Integer random generator which filter then choose candidate
|
||||
"""
|
||||
"""Integer random generator which filter then choose candidate"""
|
||||
|
||||
# TODO: Faire un cache pour éviter de reconstruire les listes à chaque fois |ven. déc. 21 19:07:42 CET 2018
|
||||
|
||||
|
@ -53,9 +53,7 @@ def look_for_rdleaf(target):
|
||||
|
||||
|
||||
class RdLeaf:
|
||||
""" Random leaf
|
||||
|
||||
"""
|
||||
"""Random leaf"""
|
||||
|
||||
def __init__(self, name):
|
||||
self._name = name
|
||||
|
@ -4,6 +4,7 @@ from .grammar import extract_letters, eval_words
|
||||
from .generate import random_generator
|
||||
from .str2 import rdstr2
|
||||
|
||||
|
||||
class RandomTree(MutableTree):
|
||||
"""MutableTree that accept {a} syntax for random generation
|
||||
|
||||
@ -61,7 +62,6 @@ class RandomTree(MutableTree):
|
||||
rd_leafs.append(leaf.name)
|
||||
return rd_leafs
|
||||
|
||||
|
||||
@property
|
||||
def random_value(self) -> set[str]:
|
||||
"""Get set of random values to generate
|
||||
@ -77,7 +77,6 @@ class RandomTree(MutableTree):
|
||||
"""
|
||||
return extract_letters(self.random_leaves)
|
||||
|
||||
|
||||
def eval_random_leaves(self, leaves_value: dict[str, int]):
|
||||
"""Given random leaves value get the tree
|
||||
|
||||
@ -93,14 +92,18 @@ class RandomTree(MutableTree):
|
||||
> 2
|
||||
> 6
|
||||
"""
|
||||
|
||||
def replace(leaf):
|
||||
try:
|
||||
return leaf.replace(leaves_value)
|
||||
except AttributeError:
|
||||
return leaf
|
||||
|
||||
return self.map_on_leaf(replace).map_on_leaf(moify)
|
||||
|
||||
def generate(self, conditions:list[str]=[], global_config:dict={} , configs:dict={}) -> Tree:
|
||||
def generate(
|
||||
self, conditions: list[str] = [], global_config: dict = {}, configs: dict = {}
|
||||
) -> Tree:
|
||||
"""Generate a random version of self
|
||||
|
||||
:param conditions: list of conditions
|
||||
@ -108,6 +111,8 @@ class RandomTree(MutableTree):
|
||||
:param configs: specific configuration for each generated values
|
||||
|
||||
"""
|
||||
generated_values = random_generator(self.random_value, conditions, global_config, configs)
|
||||
generated_values = random_generator(
|
||||
self.random_value, conditions, global_config, configs
|
||||
)
|
||||
leaves = eval_words(self.random_leaves, generated_values)
|
||||
return self.eval_random_leaves(leaves)
|
||||
|
@ -1,11 +1,17 @@
|
||||
|
||||
from ...core.operator import is_operator
|
||||
from functools import partial
|
||||
from ...core.str2 import concurent_broadcast, lookforNumbers, pparser, missing_times, lookfor
|
||||
from ...core.str2 import (
|
||||
concurent_broadcast,
|
||||
lookforNumbers,
|
||||
pparser,
|
||||
missing_times,
|
||||
lookfor,
|
||||
)
|
||||
from ...core.coroutine import STOOOP
|
||||
from ...core.MO import moify_cor
|
||||
from .leaf import look_for_rdleaf
|
||||
|
||||
|
||||
def rdstr2(sink):
|
||||
"""Return a pipeline which parse random expression and with sink as endpoint
|
||||
|
||||
@ -22,8 +28,7 @@ def rdstr2(sink):
|
||||
|
||||
def pipeline(expression):
|
||||
str2_corout = look_for_rdleaf(
|
||||
lookforNumbers(operator_corout(
|
||||
missing_times(moify_cor(pparser(sink)))))
|
||||
lookforNumbers(operator_corout(missing_times(moify_cor(pparser(sink)))))
|
||||
)
|
||||
|
||||
for i in expression.replace(" ", ""):
|
||||
@ -33,4 +38,3 @@ def rdstr2(sink):
|
||||
return a
|
||||
|
||||
return pipeline
|
||||
|
||||
|
@ -6,7 +6,13 @@ DEFAUTL_CONFIG = {
|
||||
"min_max": (-10, 10),
|
||||
}
|
||||
|
||||
def expression_generator(template:str, conditions:list[str]=[], global_config:dict={}, configs:dict={}):
|
||||
|
||||
def expression_generator(
|
||||
template: str,
|
||||
conditions: list[str] = [],
|
||||
global_config: dict = {},
|
||||
configs: dict = {},
|
||||
):
|
||||
"""Generate a random expression
|
||||
|
||||
:param template: the template of the expression
|
||||
@ -31,5 +37,7 @@ def expression_generator(template:str, conditions:list[str]=[], global_config:di
|
||||
|
||||
"""
|
||||
rd_tree = RandomTree.from_str(template)
|
||||
generated_tree = rd_tree.generate(conditions, dict(DEFAUTL_CONFIG, **global_config), configs)
|
||||
generated_tree = rd_tree.generate(
|
||||
conditions, dict(DEFAUTL_CONFIG, **global_config), configs
|
||||
)
|
||||
return Expression._post_processing(generated_tree)
|
||||
|
@ -17,7 +17,13 @@ DEFAUTL_CONFIG = {
|
||||
"min_max": (-10, 10),
|
||||
}
|
||||
|
||||
def list_generator(var_list:list[str], conditions:list[str]=[], global_config:dict={}, configs:dict={})->list[int]:
|
||||
|
||||
def list_generator(
|
||||
var_list: list[str],
|
||||
conditions: list[str] = [],
|
||||
global_config: dict = {},
|
||||
configs: dict = {},
|
||||
) -> list[int]:
|
||||
"""Generate random computed values from the list
|
||||
|
||||
:param rd_variables: list of random variables to generate (can be computed value - "a*b")
|
||||
@ -51,6 +57,8 @@ def list_generator(var_list:list[str], conditions:list[str]=[], global_config:di
|
||||
True
|
||||
"""
|
||||
rv = extract_letters(var_list)
|
||||
rv_gen = random_generator(rv, conditions, dict(DEFAUTL_CONFIG, **global_config), configs)
|
||||
rv_gen = random_generator(
|
||||
rv, conditions, dict(DEFAUTL_CONFIG, **global_config), configs
|
||||
)
|
||||
generated = eval_words(var_list, rv_gen)
|
||||
return [generated[v] for v in var_list]
|
||||
|
Loading…
Reference in New Issue
Block a user