Kaynağa Gözat

Add a random symbol generator + add some variables

Yann Weber 6 yıl önce
ebeveyn
işleme
b45e6fe081
1 değiştirilmiş dosya ile 17 ekleme ve 2 silme
  1. 17
    2
      gte/turmit.py

+ 17
- 2
gte/turmit.py Dosyayı Görüntüle

1
 import collections
1
 import collections
2
 import inspect
2
 import inspect
3
+import random
3
 
4
 
4
 _op_list = collections.OrderedDict()
5
 _op_list = collections.OrderedDict()
5
 _var_list = collections.OrderedDict()
6
 _var_list = collections.OrderedDict()
6
 
7
 
8
+_var_list['x'] = 0
9
+_var_list['y'] = 0
10
+_var_list['r'] = 0
11
+_var_list['g'] = 0
12
+_var_list['b'] = 0
13
+
7
 def RpnOp(method):
14
 def RpnOp(method):
8
     ''' @brief Decorator for RPN operation that autodetect argument count
15
     ''' @brief Decorator for RPN operation that autodetect argument count
9
         
16
         
48
         if optype == self.OPERATION:
55
         if optype == self.OPERATION:
49
             self.value = list(_op_list.keys())[value % len(_op_list)]
56
             self.value = list(_op_list.keys())[value % len(_op_list)]
50
         elif optype == self.VARIABLE:
57
         elif optype == self.VARIABLE:
51
-            self.value %= len(_var_list)
58
+            self.value = list(_var_list.keys())[value % len(_var_list)]
52
 
59
 
53
     def __str__(self):
60
     def __str__(self):
54
         if self.optype == self.OPERATION:
61
         if self.optype == self.OPERATION:
56
         elif self.optype == self.VALUE:
63
         elif self.optype == self.VALUE:
57
             return '0x%04X' % self.value
64
             return '0x%04X' % self.value
58
         else:
65
         else:
59
-            return _var_list[self.value]
66
+            return self.value.upper()
67
+
68
+    @classmethod
69
+    def random(cls):
70
+        optype = [cls.OPERATION, cls.VALUE, cls.VARIABLE]
71
+        optype = optype[random.randint(0,2)]
72
+        return cls(random.randint(0, 0xFFFF), optype)
73
+
74
+
60
 
75
 
61
 class Turmit(object):
76
 class Turmit(object):
62
     ''' @brief Represent a turmit that act given an RPN expression with an
77
     ''' @brief Represent a turmit that act given an RPN expression with an

Loading…
İptal
Kaydet