Welcome, guest | Sign In | My Account | Store | Cart

here we will see an example of template comparisons based on a logical ANY object. also this recipe provides alternative implementations of the absolute maximum and minimum objects (for more details on them see PEP326 http://www.python.org/peps/pep-0326.html).

Python, 72 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#=======================================================================

__version__ = '''0.0.01'''
__sub_version__ = '''20041028004506'''
__copyright__ = '''(c) Alex A. Naanou 2003'''


#-----------------------------------------------------------------------
#------------------------------------------------------------_Compare---
class _Compare(object):
    '''
    '''
    def __init__(self, eq):
        self._eq = eq
    def __cmp__(self, other):
        return self._eq
    def __eq__(self, other):
        return self._eq == 0
    def __ne__(self, other):
        return self._eq != 0
    def __gt__(self, other):
        return self._eq > 0
    def __ge__(self, other):
        return self._eq >= 0
    def __lt__(self, other):
        return self._eq < 0
    def __le__(self, other):
        return self._eq <= 0

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# this will compare to any value as equal (almost opposite to None)
ANY = _Compare(0)

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# this is bigger than any value... (absolute maximum)
MAXIMUM = _Compare(1)

#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# this is smaller than any value... (absolute minimum)
MINIMUM = _Compare(-1)


#=======================================================================
# NOTE: the MAXIMUM and MINIMUM objects are not discussed here as they 
#       are discussed in depth in PEP326 http://www.python.org/peps/pep-0326.html
if __name__ == '__main__':


    # Example I:
    # compare two objects by their structure...
    print (ANY, (ANY, ANY)) == (1, (2, 3))

    # the above comparison is eqivalent to:
    print (lambda o: \
                type(o) is tuple \
                    and len(o) == 2 \
                    and type(o[1]) is tuple \
                    and len(o[1]) == 2
          )( (1, (2, 3)) )


    # Example II:
    # compare structure and partial value...
    print ([ANY, 123], 'string', (ANY,), ANY) == ([2, 123], 'string', (0.1,), (1, 2,))

    # now try and imagine the explicit code to do the same thing as
    # above! :))



#=======================================================================
#                                            vim:set ts=4 sw=4 nowrap :

the code here is mostly self explanatory... :)

P.S. here is a fun little piece of code that demonstrates the asymmetry of pythons comparison operations: <pre>print ANY == MAXIMUM # this is True print MAXIMUM > ANY # this is also True

print MAXIMUM == ANY # and this is False!

</pre> e.g. we can say that the term "anything" includes an absolute maximum, but the absolute maximum is larger than anything! :))

2 comments

Alain Pointdexter 16 years ago  # | flag

a simple (?) question. Great recipe!

I have a question though. How can i do the following:

assert (ANY,'x')==('a','b','c',....,'x')

My problem is that i have a list of tuples and i want to find the one that ends with 'x'

mylistoftuples=[('w',),('r','t'),....,('a','b','c')]

idx=mylistoftuples.index((ANY,'x'))

Thanks for helping

Alex Naanou (author) 14 years, 8 months ago  # | flag

sorry for not answering for so long... been a bit off ASPN for some time :)

well, in python 2.5 this just works :)

[(1,2),(3,0),(4,)].index((ANY, 0)) # returns 2
Created by Alex Naanou on Wed, 27 Oct 2004 (PSF)
Python recipes (4591)
Alex Naanou's recipes (4)

Required Modules

  • (none specified)

Other Information and Tasks