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 unary_op 24 from native import int_neg, int_not, is_int, float_neg 25 26 # These functions defer method lookup by wrapping the attribute access in 27 # lambda functions. Thus, the appropriate methods are defined locally, but no 28 # attempt to obtain them is made until the generic function is called. 29 30 # Unary operator functions. 31 32 def invert(a): 33 return unary_op(a, lambda a: a.__invert__) 34 35 def neg(a): 36 if is_int(a): 37 return int_neg(a) 38 elif a.__class__ is float: 39 return float_neg(a) 40 return unary_op(a, lambda a: a.__neg__) 41 42 def not_(a): 43 if is_int(a): 44 return int_not(a) 45 return not a 46 47 def pos(a): 48 if is_int(a) or a.__class__ is float: 49 return a 50 return unary_op(a, lambda a: a.__pos__) 51 52 # vim: tabstop=4 expandtab shiftwidth=4