From bf75a44b852c5b2f9ad74fbfd11e2907a94f76a4 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 18 Mar 2010 21:33:46 -0700 Subject: [PATCH] NumberDef: Store 'expr' field, a string for things that will get evaled but a static number for times when it can be computed at compile time. Allow numbers to be passed to the constructor. --- bulletml/expr.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bulletml/expr.py b/bulletml/expr.py index fc71d0d..b72fa22 100644 --- a/bulletml/expr.py +++ b/bulletml/expr.py @@ -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.""" -- 2.30.2