1 #!/usr/bin/env python 2 3 """ 4 Operator support. 5 6 Copyright (C) 2010 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 def binary_op(a, b, left_accessor, right_accessor): 23 24 """ 25 A single parameterised function providing the binary operator mechanism for 26 arguments 'a' and 'b' using accessors given as 'left_accessor' and 27 'right_accessor' which provide the methods for the operands. 28 """ 29 30 # First, try and get a method for the left argument, and then call it with 31 # the right argument. 32 33 try: 34 fn = left_accessor(a) 35 except AttributeError: 36 pass 37 else: 38 result = fn(b) 39 if result is not NotImplemented: 40 return result 41 42 # Otherwise, try and get a method for the right argument, and then call it 43 # with the left argument. 44 45 try: 46 fn = right_accessor(b) 47 except AttributeError: 48 pass 49 else: 50 result = fn(a) 51 if result is not NotImplemented: 52 return result 53 54 # Where no methods were available, or if neither method could support the 55 # operation, raise an exception. 56 57 raise TypeError 58 59 # Concrete operator functions. 60 # These functions defer method lookup by wrapping the attribute access in 61 # lambda functions. Thus, the appropriate methods are defined locally, but no 62 # attempt to obtain them is made until the generic function is called. 63 64 # NOTE: The compiler should make it possible for these to call the generic 65 # NOTE: operator implementation with no additional call overhead. 66 67 def add(a, b): 68 return binary_op(a, b, lambda a: a.__add__, lambda b: b.__radd__) 69 70 def and_(a, b): 71 return binary_op(a, b, lambda a: a.__and__, lambda b: b.__rand__) 72 73 def div(a, b): 74 return binary_op(a, b, lambda a: a.__div__, lambda b: b.__rdiv__) 75 76 def floordiv(a, b): 77 return binary_op(a, b, lambda a: a.__floordiv__, lambda b: b.__rfloordiv__) 78 79 def lshift(a, b): 80 return binary_op(a, b, lambda a: a.__lshift__, lambda b: b.__rlshift__) 81 82 def mod(a, b): 83 return binary_op(a, b, lambda a: a.__mod__, lambda b: b.__rmod__) 84 85 def mul(a, b): 86 return binary_op(a, b, lambda a: a.__mul__, lambda b: b.__rmul__) 87 88 def or(a, b): 89 return binary_op(a, b, lambda a: a.__or__, lambda b: b.__ror__) 90 91 def pow(a, b): 92 return binary_op(a, b, lambda a: a.__pow__, lambda b: b.__rpow__) 93 94 def rshift(a, b): 95 return binary_op(a, b, lambda a: a.__rshift__, lambda b: b.__rrshift__) 96 97 def sub(a, b): 98 return binary_op(a, b, lambda a: a.__sub__, lambda b: b.__rsub__) 99 100 def xor(a, b): 101 return binary_op(a, b, lambda a: a.__xor__, lambda b: b.__rxor__) 102 103 # vim: tabstop=4 expandtab shiftwidth=4