Lichen

Annotated lib/operator/comparison.py

901:a616c0c86058
2019-05-20 Paul Boddie Demonstrate undesirable side-effects caused by escaping temporary objects. temporary-object-experiment
paul@6 1
#!/usr/bin/env python
paul@6 2
paul@6 3
"""
paul@6 4
Operator support.
paul@6 5
paul@873 6
Copyright (C) 2010, 2013, 2015, 2016, 2017,
paul@873 7
              2019 Paul Boddie <paul@boddie.org.uk>
paul@6 8
paul@6 9
This program is free software; you can redistribute it and/or modify it under
paul@6 10
the terms of the GNU General Public License as published by the Free Software
paul@6 11
Foundation; either version 3 of the License, or (at your option) any later
paul@6 12
version.
paul@6 13
paul@6 14
This program is distributed in the hope that it will be useful, but WITHOUT
paul@6 15
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@6 16
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@6 17
details.
paul@6 18
paul@6 19
You should have received a copy of the GNU General Public License along with
paul@6 20
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@6 21
"""
paul@6 22
paul@6 23
from operator.core import binary_op
paul@873 24
from native import int_eq, int_ge, int_gt, int_le, int_lt, int_ne, is_int, \
paul@873 25
                   float_eq, float_ge, float_gt, float_le, float_lt, float_ne
paul@6 26
paul@6 27
# These functions defer method lookup by wrapping the attribute access in
paul@6 28
# lambda functions. Thus, the appropriate methods are defined locally, but no
paul@6 29
# attempt to obtain them is made until the generic function is called.
paul@6 30
paul@6 31
# Comparison functions.
paul@6 32
paul@6 33
def eq(a, b):
paul@762 34
    if is_int(a) and is_int(b):
paul@762 35
        return int_eq(a, b)
paul@873 36
    elif a.__class__ is float and b.__class__ is float:
paul@873 37
        return float_eq(a, b)
paul@278 38
    return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__, False)
paul@6 39
paul@6 40
def ge(a, b):
paul@762 41
    if is_int(a) and is_int(b):
paul@762 42
        return int_ge(a, b)
paul@873 43
    elif a.__class__ is float and b.__class__ is float:
paul@873 44
        return float_ge(a, b)
paul@6 45
    return binary_op(a, b, lambda a: a.__ge__, lambda b: b.__le__)
paul@6 46
paul@6 47
def gt(a, b):
paul@762 48
    if is_int(a) and is_int(b):
paul@762 49
        return int_gt(a, b)
paul@873 50
    elif a.__class__ is float and b.__class__ is float:
paul@873 51
        return float_gt(a, b)
paul@6 52
    return binary_op(a, b, lambda a: a.__gt__, lambda b: b.__lt__)
paul@6 53
paul@6 54
def le(a, b):
paul@762 55
    if is_int(a) and is_int(b):
paul@762 56
        return int_le(a, b)
paul@873 57
    elif a.__class__ is float and b.__class__ is float:
paul@873 58
        return float_le(a, b)
paul@6 59
    return binary_op(a, b, lambda a: a.__le__, lambda b: b.__ge__)
paul@6 60
paul@6 61
def lt(a, b):
paul@762 62
    if is_int(a) and is_int(b):
paul@762 63
        return int_lt(a, b)
paul@873 64
    elif a.__class__ is float and b.__class__ is float:
paul@873 65
        return float_lt(a, b)
paul@6 66
    return binary_op(a, b, lambda a: a.__lt__, lambda b: b.__gt__)
paul@6 67
paul@6 68
def ne(a, b):
paul@762 69
    if is_int(a) and is_int(b):
paul@762 70
        return int_ne(a, b)
paul@873 71
    elif a.__class__ is float and b.__class__ is float:
paul@873 72
        return float_ne(a, b)
paul@278 73
    return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__, True)
paul@6 74
paul@6 75
# vim: tabstop=4 expandtab shiftwidth=4