1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/operator/core.py Tue Aug 30 21:55:58 2016 +0200
1.3 @@ -0,0 +1,114 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Operator support.
1.8 +
1.9 +Copyright (C) 2010, 2013, 2015, 2016 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 +# Define "is" and "is not" in terms of native operations. They are imported by
1.26 +# the operator.binary module.
1.27 +
1.28 +from native import _is as is_, _is_not as is_not
1.29 +
1.30 +def binary_op(a, b, left_accessor, right_accessor):
1.31 +
1.32 + """
1.33 + A single parameterised function providing the binary operator mechanism for
1.34 + arguments 'a' and 'b' using accessors given as 'left_accessor' and
1.35 + 'right_accessor' which provide the methods for the operands.
1.36 + """
1.37 +
1.38 + # First, try and get a method for the left argument, and then call it with
1.39 + # the right argument.
1.40 +
1.41 + try:
1.42 + fn = left_accessor(a)
1.43 + except AttributeError:
1.44 + pass
1.45 + else:
1.46 + result = fn(b)
1.47 + if is_not(result, NotImplemented):
1.48 + return result
1.49 +
1.50 + # Otherwise, try and get a method for the right argument, and then call it
1.51 + # with the left argument.
1.52 +
1.53 + try:
1.54 + fn = right_accessor(b)
1.55 + except AttributeError:
1.56 + pass
1.57 + else:
1.58 + result = fn(a)
1.59 + if is_not(result, NotImplemented):
1.60 + return result
1.61 +
1.62 + # Where no methods were available, or if neither method could support the
1.63 + # operation, raise an exception.
1.64 +
1.65 + raise TypeError
1.66 +
1.67 +def unary_op(a, accessor):
1.68 +
1.69 + """
1.70 + A single parameterised function providing the unary operator mechanism for
1.71 + the argument 'a' using the given 'accessor' to provide the method for the
1.72 + operand.
1.73 + """
1.74 +
1.75 + # First, try and get a method for the argument, and then call it.
1.76 +
1.77 + try:
1.78 + fn = accessor(a)
1.79 + except AttributeError:
1.80 + pass
1.81 + else:
1.82 + result = fn()
1.83 + if is_not(result, NotImplemented):
1.84 + return result
1.85 +
1.86 + # Where no method was available, or if the method could not support the
1.87 + # operation, raise an exception.
1.88 +
1.89 + raise TypeError
1.90 +
1.91 +def augassign(a, b, augmented_accessor, left_accessor, right_accessor):
1.92 +
1.93 + """
1.94 + A single parameterised function providing the augmented assignment mechanism
1.95 + for arguments 'a' and 'b' either using 'augmented_accessor' (directly
1.96 + affecting 'a') or using 'left_accessor' and 'right_accessor' (conventional
1.97 + operator method accessors).
1.98 +
1.99 + The result of the assignment is returned.
1.100 + """
1.101 +
1.102 + # First, try and get a method that directly affects the assignment target.
1.103 +
1.104 + try:
1.105 + fn = augmented_accessor(a)
1.106 + except AttributeError:
1.107 + pass
1.108 + else:
1.109 + result = fn(b)
1.110 + if is_not(result, NotImplemented):
1.111 + return result
1.112 +
1.113 + # Otherwise, attempt a conventional binary operation.
1.114 +
1.115 + return binary_op(a, b, left_accessor, right_accessor)
1.116 +
1.117 +# vim: tabstop=4 expandtab shiftwidth=4