INumberDef: Pre-int-round static values.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 17 Mar 2010 04:52:22 +0000 (21:52 -0700)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 17 Mar 2010 04:52:22 +0000 (21:52 -0700)
bulletml/expr.py

index 3c80b0652619556ceb6961a8ddb413a0cbca4b06..fc71d0ddb40415446bf0c5505205dc1a1907c9db 100644 (file)
@@ -43,21 +43,21 @@ class NumberDef(object):
         self.__expr = expr.replace("$rand", "random()").replace("$rank", "rank")
         try:
             try:
-                self.__value = eval(self.__expr, dict(__builtins__={}))
+                self._value = eval(self.__expr, dict(__builtins__={}))
             except NameError:
                 variables = dict(rank=1, params=[0] * 99)
                 value = eval(self.__expr, self.GLOBALS, variables)
                 if not isinstance(value, (int, float)):
                     raise TypeError(expr)
-                self.__value = None
+                self._value = None
         except Exception:
             raise ExprError(expr)
         self.__expr = compile(self.__expr, __file__, "eval")
 
     def __call__(self, params, rank):
         """Evaluate the expression and return its value."""
-        if self.__value is not None:
-            return self.__value
+        if self._value is not None:
+            return self._value
         variables = { 'rank': rank, 'params': params }
         return eval(self.__expr, self.GLOBALS, variables)
 
@@ -66,5 +66,12 @@ class NumberDef(object):
 
 class INumberDef(NumberDef):
     """A NumberDef, but returns rounded integer results."""
+    def __init__(self, expr):
+        super(INumberDef, self).__init__(expr)
+        if self._value is not None:
+            self._value = int(round(self._value))
+
     def __call__(self, params, rank):
+        if self._value is not None:
+            return self._value
         return int(round(super(INumberDef, self).__call__(params, rank)))