def evaluate(self, context): if self._op == '=': true = boolean.true false = boolean.false else: true = boolean.false false = boolean.true lrt = self._left.evaluate(context) rrt = self._right.evaluate(context) lType = type(lrt) rType = type(rrt) if lType == types.ListType == rType: #Node set to node set for right_curr in rrt: right_curr = Conversions.StringValue(right_curr) for left_curr in lrt: if right_curr == Conversions.StringValue(left_curr): return true return false elif lType == types.ListType or rType == types.ListType: func = None if lType == types.ListType: set = lrt val = rrt else: set = rrt val = lrt if type(val) in NumberTypes: func = Conversions.NumberValue elif boolean.IsBooleanType(val): func = Conversions.BooleanValue elif type(val) == types.StringType: func = Conversions.StringValue else: #Deal with e.g. RTFs val = Conversions.StringValue(val) func = Conversions.StringValue for n in set: if func(n) == val: return true return false if boolean.IsBooleanType(lrt) or boolean.IsBooleanType(rrt): rt = Conversions.BooleanValue(lrt) == Conversions.BooleanValue(rrt) elif lType in NumberTypes or rType in NumberTypes: rt = Conversions.NumberValue(lrt) == Conversions.NumberValue(rrt) else: rt = Conversions.StringValue(lrt) == Conversions.StringValue(rrt) if rt: # Due to the swapping of true/false, true might evaluate to 0 # We cannot compact this to 'rt and true or false' return true return false
def range(context, lo, hi): doc = context.node while doc.parentNode: doc = doc.parentNode lo = Conversions.NumberValue(lo) hi = Conversions.NumberValue(hi) nodeset = [] for number in xrange(lo, hi): nodeset.append(doc.createTextNode(str(number))) return nodeset
def evaluate(self, context): '''returns a number''' if self._leftLit: lrt = self._left else: lrt = self._left.evaluate(context) lrt = Conversions.NumberValue(lrt) if self._rightLit: rrt = self._right else: rrt = self._right.evaluate(context) rrt = Conversions.NumberValue(rrt) return lrt + (rrt * self._sign)
def __init__(self, sign, left, right): self._sign = sign self._leftLit = 0 self._rightLit = 0 if isinstance(left, ParsedLiteralExpr): self._leftLit = 1 self._left = Conversions.NumberValue(left.evaluate(None)) else: self._left = left if isinstance(right, ParsedLiteralExpr): self._rightLit = 1 self._right = Conversions.NumberValue(right.evaluate(None)) else: self._right = right return
def Substring(context, st, start, end=None): """Function: <string> substring(<string>, <number>, <number>?)""" st = Conversions.StringValue(st) start = Conversions.NumberValue(start) if start is NaN: return '' start = int(round(start)) start = start > 1 and start - 1 or 0 if end is None: return st[start:] end = Conversions.NumberValue(end) if start is NaN: return st[start:] end = int(round(end)) return st[start:start + end]
def __init__(self, opcode, left, right): self._op = opcode if isinstance(left, ParsedLiteralExpr): self._left = Conversions.NumberValue(left.evaluate(None)) self._leftLit = 1 else: self._left = left self._leftLit = 0 if isinstance(right, ParsedLiteralExpr): self._right = Conversions.NumberValue(right.evaluate(None)) self._rightLit = 1 else: self._right = right self._rightLit = 0
def evaluate(self, context): if self._leftLit: lrt = self._left else: lrt = Conversions.NumberValue(self._left.evaluate(context)) if self._rightLit: rrt = self._right else: rrt = Conversions.NumberValue(self._right.evaluate(context)) if self._op == 0: rt = (lrt < rrt) elif self._op == 1: rt = (lrt <= rrt) elif self._op == 2: rt = (lrt > rrt) elif self._op == 3: rt = (lrt >= rrt) return rt and boolean.true or boolean.false
def Ceiling(context, number): """Function: <number> ceiling(<number>)""" number = Conversions.NumberValue(number) #if type(number) in g_stringTypes: # number = string.atof(number) if int(number) == number: return number elif number > 0: return int(number) + 1 else: return int(number)
def Floor(context, number): """Function: <number> floor(<number>)""" number = Conversions.NumberValue(number) #if type(number) in g_stringTypes: # number = string.atof(number) if int(number) == number: return number elif number < 0: return int(number) - 1 else: return int(number)
def evaluate(self, context): '''returns a number''' lrt = self._left.evaluate(context) lrt = Conversions.NumberValue(lrt) rrt = self._right.evaluate(context) rrt = Conversions.NumberValue(rrt) res = 0 if self._op == 0: res = lrt * rrt elif self._op == 1: if rrt == 0: res = NaN else: res = lrt / rrt elif self._op == 2: if rrt == 0: res = NaN else: res = lrt % rrt return res
def Round(context, number): """Function: <number> round(<number>)""" number = Conversions.NumberValue(number) return round(number, 0)
def Sum(context, nodeSet): """Function: <number> sum(<node-set>)""" nns = map(lambda x: Conversions.NumberValue(x), nodeSet) return reduce(lambda x, y: x + y, nns, 0)
def Number(context, object=None): """Function: <number> number(<object>?)""" if object is None: object = [context.node] return Conversions.NumberValue(object)
def evaluate(self, context): '''returns a number''' exp = self._exp.evaluate(context) exp = Conversions.NumberValue(exp) rt = exp * -1.0 return rt