Implement random generation methods on rpnSymbol

This commit is contained in:
Yann Weber 2018-04-24 23:32:22 +02:00
commit 9681a4b38a
2 changed files with 25 additions and 3 deletions

View file

@ -1,2 +1,4 @@
##@brief GTE Genetic Turmits Evolver
from . import turmit
from . import rpnlib

View file

@ -50,6 +50,10 @@ class RpnSymbol(object):
def __init__(self, value, optype = VALUE):
''' @brief RpnSymbol constructor
@param value int : positiv integer moded given optype
@param optype int : one of @ref OPERATION @ref VALUE @ref VARIABLE
'''
self.optype = optype
self.value = value
if optype == self.OPERATION:
@ -58,6 +62,7 @@ class RpnSymbol(object):
self.value = list(_var_list.keys())[value % len(_var_list)]
def __str__(self):
''' @brief Return a string representation of current symbol '''
if self.optype == self.OPERATION:
return _op_list[self.value][0].__name__.upper()
elif self.optype == self.VALUE:
@ -66,10 +71,25 @@ class RpnSymbol(object):
return self.value.upper()
@classmethod
def random(cls):
optype = [cls.OPERATION, cls.VALUE, cls.VARIABLE]
optype = optype[random.randint(0,2)]
def random(cls, optype=None):
''' @brief Return a randomly generated symbol '''
if optype is None:
optype = [cls.OPERATION, cls.VALUE, cls.VARIABLE]
optype = optype[random.randint(0,2)]
return cls(random.randint(0, 0xFFFF), optype)
@classmethod
def rand_op(cls):
''' @return Random operation '''
return cls.random(cls.OPERATION)
@classmethod
def rand_var(cls):
''' @retrun Random variable '''
return cls.random(cls.VARIABLE)
@classmethod
def rand_value(cls):
''' @return Random value '''
return cls.random(cls.VALUE)