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 24 from native import int_eq, int_ge, int_gt, int_le, int_lt, int_ne, is_int, \ 25 float_eq, float_ge, float_gt, float_le, float_lt, float_ne 26 27 # These functions defer method lookup by wrapping the attribute access in 28 # lambda functions. Thus, the appropriate methods are defined locally, but no 29 # attempt to obtain them is made until the generic function is called. 30 31 # Comparison functions. 32 33 def eq(a, b): 34 if is_int(a) and is_int(b): 35 return int_eq(a, b) 36 elif a.__class__ is float and b.__class__ is float: 37 return float_eq(a, b) 38 return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__, False) 39 40 def ge(a, b): 41 if is_int(a) and is_int(b): 42 return int_ge(a, b) 43 elif a.__class__ is float and b.__class__ is float: 44 return float_ge(a, b) 45 return binary_op(a, b, lambda a: a.__ge__, lambda b: b.__le__) 46 47 def gt(a, b): 48 if is_int(a) and is_int(b): 49 return int_gt(a, b) 50 elif a.__class__ is float and b.__class__ is float: 51 return float_gt(a, b) 52 return binary_op(a, b, lambda a: a.__gt__, lambda b: b.__lt__) 53 54 def le(a, b): 55 if is_int(a) and is_int(b): 56 return int_le(a, b) 57 elif a.__class__ is float and b.__class__ is float: 58 return float_le(a, b) 59 return binary_op(a, b, lambda a: a.__le__, lambda b: b.__ge__) 60 61 def lt(a, b): 62 if is_int(a) and is_int(b): 63 return int_lt(a, b) 64 elif a.__class__ is float and b.__class__ is float: 65 return float_lt(a, b) 66 return binary_op(a, b, lambda a: a.__lt__, lambda b: b.__gt__) 67 68 def ne(a, b): 69 if is_int(a) and is_int(b): 70 return int_ne(a, b) 71 elif a.__class__ is float and b.__class__ is float: 72 return float_ne(a, b) 73 return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__, True) 74 75 # vim: tabstop=4 expandtab shiftwidth=4