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