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 int_float 30 31 # These functions defer method lookup by wrapping the attribute access in 32 # lambda functions. Thus, the appropriate methods are defined locally, but no 33 # attempt to obtain them is made until the generic function is called. 34 35 # Binary operator functions. 36 37 def add(a, b): 38 if is_int(a) and is_int(b): 39 return int_add(a, b) 40 elif a.__class__ is float and b.__class__ is float: 41 return float_add(a, b) 42 return binary_op(a, b, lambda a: a.__add__, lambda b: b.__radd__) 43 44 def and_(a, b): 45 if is_int(a) and is_int(b): 46 return int_and(a, b) 47 return binary_op(a, b, lambda a: a.__and__, lambda b: b.__rand__) 48 49 def contains(a, b): 50 return in_(b, a) 51 52 def div(a, b): 53 if is_int(a) and is_int(b): 54 return int_div(a, b) 55 elif a.__class__ is float and b.__class__ is float: 56 return float_div(a, b) 57 return binary_op(a, b, lambda a: a.__div__, lambda b: b.__rdiv__) 58 59 def floordiv(a, b): 60 return binary_op(a, b, lambda a: a.__floordiv__, lambda b: b.__rfloordiv__) 61 62 def in_(a, b): 63 return b.__contains__(a) 64 65 def not_in(a, b): 66 return not b.__contains__(a) 67 68 def lshift(a, b): 69 if is_int(a) and is_int(b): 70 return int_lshift(a, b) 71 return binary_op(a, b, lambda a: a.__lshift__, lambda b: b.__rlshift__) 72 73 def mod(a, b): 74 if is_int(a) and is_int(b): 75 return int_mod(a, b) 76 return binary_op(a, b, lambda a: a.__mod__, lambda b: b.__rmod__) 77 78 def mul(a, b): 79 if is_int(a) and is_int(b): 80 return int_mul(a, b) 81 elif a.__class__ is float and b.__class__ is float: 82 return float_mul(a, b) 83 return binary_op(a, b, lambda a: a.__mul__, lambda b: b.__rmul__) 84 85 def or_(a, b): 86 if is_int(a) and is_int(b): 87 return int_or(a, b) 88 return binary_op(a, b, lambda a: a.__or__, lambda b: b.__ror__) 89 90 def pow(a, b): 91 if is_int(a) and is_int(b): 92 return int_pow(a, b) 93 elif a.__class__ is float: 94 if is_int(b): 95 return float_pow(a, int_float(b)) 96 if b.__class__ is float: 97 return float_pow(a, b) 98 return binary_op(a, b, lambda a: a.__pow__, lambda b: b.__rpow__) 99 100 def rshift(a, b): 101 if is_int(a) and is_int(b): 102 return int_rshift(a, b) 103 return binary_op(a, b, lambda a: a.__rshift__, lambda b: b.__rrshift__) 104 105 def sub(a, b): 106 if is_int(a) and is_int(b): 107 return int_sub(a, b) 108 elif a.__class__ is float and b.__class__ is float: 109 return float_sub(a, b) 110 return binary_op(a, b, lambda a: a.__sub__, lambda b: b.__rsub__) 111 112 def xor(a, b): 113 if is_int(a) and is_int(b): 114 return int_xor(a, b) 115 return binary_op(a, b, lambda a: a.__xor__, lambda b: b.__rxor__) 116 117 # vim: tabstop=4 expandtab shiftwidth=4