1 #!/usr/bin/env python 2 3 """ 4 Operator support. 5 6 Copyright (C) 2010, 2013, 2015, 2016, 2017, 7 2019 Paul Boddie <paul@boddie.org.uk> 8 9 This program is free software; you can redistribute it and/or modify it under 10 the terms of the GNU General Public License as published by the Free Software 11 Foundation; either version 3 of the License, or (at your option) any later 12 version. 13 14 This program is distributed in the hope that it will be useful, but WITHOUT 15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 17 details. 18 19 You should have received a copy of the GNU General Public License along with 20 this program. If not, see <http://www.gnu.org/licenses/>. 21 """ 22 23 from operator.core import binary_op, is_, is_not 24 from native import int_add, int_div, int_mod, int_mul, int_pow, int_sub, \ 25 int_lshift, int_rshift, \ 26 int_and, int_not, int_or, int_xor, \ 27 is_int, \ 28 float_add, float_div, float_mul, float_pow, float_sub 29 30 # These functions defer method lookup by wrapping the attribute access in 31 # lambda functions. Thus, the appropriate methods are defined locally, but no 32 # attempt to obtain them is made until the generic function is called. 33 34 # Binary operator functions. 35 36 def add(a, b): 37 if is_int(a) and is_int(b): 38 return int_add(a, b) 39 elif a.__class__ is float and b.__class__ is float: 40 return float_add(a, b) 41 return binary_op(a, b, lambda a: a.__add__, lambda b: b.__radd__) 42 43 def and_(a, b): 44 if is_int(a) and is_int(b): 45 return int_and(a, b) 46 return binary_op(a, b, lambda a: a.__and__, lambda b: b.__rand__) 47 48 def contains(a, b): 49 return in_(b, a) 50 51 def div(a, b): 52 if is_int(a) and is_int(b): 53 return int_div(a, b) 54 elif a.__class__ is float and b.__class__ is float: 55 return float_div(a, b) 56 return binary_op(a, b, lambda a: a.__div__, lambda b: b.__rdiv__) 57 58 def floordiv(a, b): 59 return binary_op(a, b, lambda a: a.__floordiv__, lambda b: b.__rfloordiv__) 60 61 def in_(a, b): 62 return b.__contains__(a) 63 64 def not_in(a, b): 65 return not b.__contains__(a) 66 67 def lshift(a, b): 68 if is_int(a) and is_int(b): 69 return int_lshift(a, b) 70 return binary_op(a, b, lambda a: a.__lshift__, lambda b: b.__rlshift__) 71 72 def mod(a, b): 73 if is_int(a) and is_int(b): 74 return int_mod(a, b) 75 return binary_op(a, b, lambda a: a.__mod__, lambda b: b.__rmod__) 76 77 def mul(a, b): 78 if is_int(a) and is_int(b): 79 return int_mul(a, b) 80 elif a.__class__ is float and b.__class__ is float: 81 return float_mul(a, b) 82 return binary_op(a, b, lambda a: a.__mul__, lambda b: b.__rmul__) 83 84 def or_(a, b): 85 if is_int(a) and is_int(b): 86 return int_or(a, b) 87 return binary_op(a, b, lambda a: a.__or__, lambda b: b.__ror__) 88 89 def pow(a, b): 90 if is_int(a) and is_int(b): 91 return int_pow(a, b) 92 elif a.__class__ is float and b.__class__ is float: 93 return float_pow(a, b) 94 return binary_op(a, b, lambda a: a.__pow__, lambda b: b.__rpow__) 95 96 def rshift(a, b): 97 if is_int(a) and is_int(b): 98 return int_rshift(a, b) 99 return binary_op(a, b, lambda a: a.__rshift__, lambda b: b.__rrshift__) 100 101 def sub(a, b): 102 if is_int(a) and is_int(b): 103 return int_sub(a, b) 104 elif a.__class__ is float and b.__class__ is float: 105 return float_sub(a, b) 106 return binary_op(a, b, lambda a: a.__sub__, lambda b: b.__rsub__) 107 108 def xor(a, b): 109 if is_int(a) and is_int(b): 110 return int_xor(a, b) 111 return binary_op(a, b, lambda a: a.__xor__, lambda b: b.__rxor__) 112 113 # vim: tabstop=4 expandtab shiftwidth=4