From 3b5c01e5cc007f59e8a57c7e582df465d9d17386 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Thu, 20 Aug 2020 17:36:38 +0200 Subject: [PATCH] Feat: random list generator --- mapytex/__init__.py | 2 +- mapytex/calculus/__init__.py | 1 + mapytex/calculus/core/__init__.py | 1 + mapytex/calculus/core/random/__init__.py | 38 ++++++++++++++++++++++-- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/mapytex/__init__.py b/mapytex/__init__.py index 326dfc5..7670ee8 100644 --- a/mapytex/__init__.py +++ b/mapytex/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 -from .calculus import Expression, Integer, Decimal +from .calculus import Expression, Integer, Decimal, list_generator # Expression.set_render('tex') diff --git a/mapytex/calculus/__init__.py b/mapytex/calculus/__init__.py index d1aef78..e748aea 100644 --- a/mapytex/calculus/__init__.py +++ b/mapytex/calculus/__init__.py @@ -31,6 +31,7 @@ Expression is the classe wich handle all calculus. It can randomly generate or i """ from .API import Expression, Integer, Decimal +from .core import list_generator from decimal import getcontext #getcontext().prec = 2 diff --git a/mapytex/calculus/core/__init__.py b/mapytex/calculus/core/__init__.py index 1e8d6c5..9605e36 100644 --- a/mapytex/calculus/core/__init__.py +++ b/mapytex/calculus/core/__init__.py @@ -66,6 +66,7 @@ from .tree import Tree, AssocialTree from .compute import compute from .typing import typing, TypingError from .renders import tree2txt, tree2tex +from .random import list_generator # ----------------------------- diff --git a/mapytex/calculus/core/random/__init__.py b/mapytex/calculus/core/random/__init__.py index 76f9709..5455e5e 100644 --- a/mapytex/calculus/core/random/__init__.py +++ b/mapytex/calculus/core/random/__init__.py @@ -47,8 +47,8 @@ Tree with RdLeaf replaced by generated values >>> leafs ['a', 'a*k'] >>> rd_varia = extract_rv(leafs) ->>> rd_varia # doctest: +SKIP -{'a', 'k'} +>>> sorted(list(rd_varia)) +['a', 'k'] >>> generated = random_generator(rd_varia, conditions=['a%2+1']) >>> generated # doctest: +SKIP {'a': 7, 'k': 4} @@ -61,8 +61,18 @@ Tree with RdLeaf replaced by generated values > 7 > 28 +List generator +-------------- + +This function ignores tree structure and works with lists + +>>> values = list_generator(["a", "a*b", "b", "c"], conditions=["b%c==1"]) +>>> values # doctest: +SKIP +{'a': -8, 'a*b': -40, 'b': 5, 'c': 4} """ +__all__ = ["generator"] + from random import choice from functools import reduce from .leaf import RdLeaf @@ -244,3 +254,27 @@ def build_variable_scope(rd_variables, rejected, min_max, variables_scope): except KeyError: complete_scope[v]["min_max"] = min_max return complete_scope + + +def list_generator(var_list, conditions=[], rejected=[0], min_max=(-10, 10), variables_scope={}): + """ Generate random computed values from the list + + :param rd_variables: list of random variables to generate (can be computed value - "a*b") + :param conditions: condition over variables + :param rejected: Rejected values for the generator (default [0]) + :param min_max: (min, max) limits in between variables will be generated + :param variables_scope: rejected and min_max define for individual variables + :return: dictionnary of generated variables + + :example: + >>> values = list_generator(["a", "a*b", "b", "c"]) + >>> values # doctest: +SKIP + >>> values["a"] * values["b"] == values["a*b"] + True + >>> values["a*b"] # doctest: +SKIP + >>> values["a"] * values["b"] # doctest: +SKIP + """ + rv = extract_rv(var_list) + rv_gen = random_generator(rv, conditions, rejected, min_max, variables_scope) + generated = compute_leafs(var_list, rv_gen) + return generated