Changeset 53

Show
Ignore:
Timestamp:
02/02/06 22:04:52 (2 years ago)
Author:
steve
Message:

Add the modulus operator (fixes #10)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/airspeed.py

    r52 r53  
    2727    operator.__eq__ = lambda a, b: a == b 
    2828    operator.__ne__ = lambda a, b: a != b 
     29    operator.mod = lambda a, b: a % b 
    2930try: 
    3031    basestring 
     
    450451 
    451452class BinaryOperator(_Element): 
    452     BINARY_OP = re.compile(r'\s*(>=|<=|<|==|!=|>|\|\||&&)\s*(.*)$', re.S) 
     453    BINARY_OP = re.compile(r'\s*(>=|<=|<|==|!=|>|%|\|\||&&)\s*(.*)$', re.S) 
    453454    OPERATORS = {'>' : operator.__gt__, '>=': operator.__ge__, 
    454455                 '<' : operator.__lt__, '<=': operator.__le__, 
    455456                 '==': operator.__eq__, '!=': operator.__ne__, 
     457                 '%' : operator.mod, 
    456458                 '||': lambda a,b : boolean_value(a) or boolean_value(b), 
    457459                 '&&': lambda a,b : boolean_value(a) and boolean_value(b)} 
  • trunk/airspeed_test.py

    r52 r53  
    531531        self.assertEquals('works', template.merge({}, loader=Loader())) 
    532532 
     533    def test_modulus_operator(self): 
     534        template = airspeed.Template('#set( $modulus = ($value % 2) )$modulus') 
     535        self.assertEquals('1', template.merge({'value': 3})) 
     536 
    533537 
    534538# 
     
    536540# 
    537541#  Report locations for template errors in strings 
    538 #  Math expressions 
     542#  Math expressions (requires operator precedence) 
    539543#  Gobbling up whitespace (tricky!) 
    540544#  Bind #macro calls at compile time? 
     
    544548#  Q. What is scope of #set ($customer.Name = 'john')  ??? 
    545549#  Scope of #set across if/elseif/else? 
    546 #  Scope of namespace for #parse etc 
    547550# 
    548551