1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/operator.py Sun May 30 21:09:08 2010 +0200
1.3 @@ -0,0 +1,95 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Operator support.
1.8 +
1.9 +Copyright (C) 2010 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +def binary_op(a, b, left_name, right_name):
1.26 +
1.27 + """
1.28 + A single parameterised function providing the binary operator mechanism for
1.29 + arguments 'a' and 'b' using methods of the given 'left_name' and
1.30 + 'right_name'.
1.31 + """
1.32 +
1.33 + # First, try and get a method for the left argument, and then call it with
1.34 + # the right argument.
1.35 +
1.36 + try:
1.37 + fn = getattr(a, left_name)
1.38 + except AttributeError:
1.39 + pass
1.40 + else:
1.41 + result = fn(b)
1.42 + if result is not NotImplemented:
1.43 + return result
1.44 +
1.45 + # Otherwise, try and get a method for the right argument, and then call it
1.46 + # with the left argument.
1.47 +
1.48 + try:
1.49 + fn = getattr(b, right_name)
1.50 + except AttributeError:
1.51 + pass
1.52 + else:
1.53 + result = fn(a)
1.54 + if result is not NotImplemented:
1.55 + return result
1.56 +
1.57 + # Where no methods were available, or if neither method could support the
1.58 + # operation, raise an exception.
1.59 +
1.60 + raise TypeError
1.61 +
1.62 +def add(a, b):
1.63 + return binary_op(a, b, "__add__", "__radd__")
1.64 +
1.65 +def and_(a, b):
1.66 + return binary_op(a, b, "__and__", "__rand__")
1.67 +
1.68 +def div(a, b):
1.69 + return binary_op(a, b, "__div__", "__rdiv__")
1.70 +
1.71 +def floordiv(a, b):
1.72 + return binary_op(a, b, "__floordiv__", "__rfloordiv__")
1.73 +
1.74 +def lshift(a, b):
1.75 + return binary_op(a, b, "__lshift__", "__rlshift__")
1.76 +
1.77 +def mod(a, b):
1.78 + return binary_op(a, b, "__mod__", "__rmod__")
1.79 +
1.80 +def mul(a, b):
1.81 + return binary_op(a, b, "__mul__", "__rmul__")
1.82 +
1.83 +def or(a, b):
1.84 + return binary_op(a, b, "__or__", "__ror__")
1.85 +
1.86 +def pow(a, b):
1.87 + return binary_op(a, b, "__pow__", "__rpow__")
1.88 +
1.89 +def rshift(a, b):
1.90 + return binary_op(a, b, "__rshift__", "__rrshift__")
1.91 +
1.92 +def sub(a, b):
1.93 + return binary_op(a, b, "__sub__", "__rsub__")
1.94 +
1.95 +def xor(a, b):
1.96 + return binary_op(a, b, "__xor__", "__rxor__")
1.97 +
1.98 +# vim: tabstop=4 expandtab shiftwidth=4