import random
|
from . import Exceptions
|
|
|
class VariableGeneratorFactory:
|
@staticmethod
|
def get_generator(definition):
|
spl = definition.split()
|
type = spl[0]
|
arglist = spl[1:]
|
|
if type == "FixedVals":
|
return FixedVariableGenerator(arglist)
|
if type == "RandLinear":
|
return RandLinearVariableGenerator(arglist)
|
|
else:
|
return None
|
|
|
class FixedVariableGenerator(VariableGeneratorFactory):
|
def __init__(self, arglist):
|
|
if len(arglist) == 0:
|
raise Exceptions.WrongParameters(
|
"In FixedVals you must provide at least one element to chose from."
|
)
|
|
try:
|
self.pool = [i for i in arglist]
|
except (ValueError, TypeError):
|
raise ValueError
|
|
def __next__(self):
|
return random.choice(self.pool)
|
|
def __iter__(self):
|
return self
|
|
|
class RandLinearVariableGenerator(VariableGeneratorFactory):
|
def __init__(self, arglist):
|
try:
|
self.min = float(arglist[0])
|
self.max = float(arglist[1])
|
except (ValueError, TypeError):
|
raise ValueError
|
|
if len(arglist) != 2:
|
raise Exceptions.WrongParameters("RandLinear accepts exactly 2 arguments.")
|
if self.min >= self.max:
|
raise ValueError(
|
"In RandLinear, first argument must be lower than second argument."
|
)
|
|
def __next__(self):
|
return random.uniform(self.min, self.max)
|
|
def __iter__(self):
|
return self
|