Browse Source

Implement random generation methods on rpnSymbol

Yann Weber 6 years ago
parent
commit
9681a4b38a
2 changed files with 25 additions and 3 deletions
  1. 2
    0
      gte/__init__.py
  2. 23
    3
      gte/rpnlib.py

+ 2
- 0
gte/__init__.py View File

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

+ 23
- 3
gte/rpnlib.py View File

@@ -50,6 +50,10 @@ class RpnSymbol(object):
50 50
 
51 51
 
52 52
     def __init__(self, value, optype = VALUE):
53
+        ''' @brief RpnSymbol constructor
54
+            @param value int : positiv integer moded given optype
55
+            @param optype int : one of @ref OPERATION @ref VALUE @ref VARIABLE
56
+        '''
53 57
         self.optype = optype
54 58
         self.value = value
55 59
         if optype == self.OPERATION:
@@ -58,6 +62,7 @@ class RpnSymbol(object):
58 62
             self.value = list(_var_list.keys())[value % len(_var_list)]
59 63
 
60 64
     def __str__(self):
65
+        ''' @brief Return a string representation of current symbol '''
61 66
         if self.optype == self.OPERATION:
62 67
             return _op_list[self.value][0].__name__.upper()
63 68
         elif self.optype == self.VALUE:
@@ -66,10 +71,25 @@ class RpnSymbol(object):
66 71
             return self.value.upper()
67 72
 
68 73
     @classmethod
69
-    def random(cls):
70
-        optype = [cls.OPERATION, cls.VALUE, cls.VARIABLE]
71
-        optype = optype[random.randint(0,2)]
74
+    def random(cls, optype=None):
75
+        ''' @brief Return a randomly generated symbol '''
76
+        if optype is None:
77
+            optype = [cls.OPERATION, cls.VALUE, cls.VARIABLE]
78
+            optype = optype[random.randint(0,2)]
72 79
         return cls(random.randint(0, 0xFFFF), optype)
73 80
 
81
+    @classmethod
82
+    def rand_op(cls):
83
+        ''' @return Random operation '''
84
+        return cls.random(cls.OPERATION)
85
+
86
+    @classmethod
87
+    def rand_var(cls):
88
+        ''' @retrun Random variable '''
89
+        return cls.random(cls.VARIABLE)
74 90
 
91
+    @classmethod
92
+    def rand_value(cls):
93
+        ''' @return Random value '''
94
+        return cls.random(cls.VALUE)
75 95
 

Loading…
Cancel
Save