NumberDef: Store 'expr' field, a string for things that will get evaled but a static...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Fri, 19 Mar 2010 04:33:46 +0000 (21:33 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Fri, 19 Mar 2010 04:33:46 +0000 (21:33 -0700)
bulletml/expr.py

index fc71d0ddb40415446bf0c5505205dc1a1907c9db..b72fa224f7ed49bb2f166582f0ee85176852e003 100644 (file)
@@ -37,7 +37,7 @@ class NumberDef(object):
             expr = expr.string
         except AttributeError:
             pass
-        self.string = expr
+        self.string = str(expr)
         repl = lambda match: "params[%d]" % (int(match.group()[1:]) - 1)
         expr = re.sub(r"\$\d+", repl, expr.lower())
         self.__expr = expr.replace("$rand", "random()").replace("$rank", "rank")
@@ -50,6 +50,9 @@ class NumberDef(object):
                 if not isinstance(value, (int, float)):
                     raise TypeError(expr)
                 self._value = None
+                self.expr = self.string
+            else:
+                self.expr = self._value
         except Exception:
             raise ExprError(expr)
         self.__expr = compile(self.__expr, __file__, "eval")
@@ -62,7 +65,7 @@ class NumberDef(object):
         return eval(self.__expr, self.GLOBALS, variables)
 
     def __repr__(self):
-        return "%s(%r)" % (type(self).__name__, self.string)
+        return "%s(%r)" % (type(self).__name__, self.expr)
 
 class INumberDef(NumberDef):
     """A NumberDef, but returns rounded integer results."""