~python-miniobject-team/python-miniobject/trunk

« back to all changes in this revision

Viewing changes to _miniobject_py.py

  • Committer: Martin Pool
  • Date: 2008-12-09 20:02:10 UTC
  • Revision ID: mbp@sourcefrog.net-20081209200210-h77ci8jmbi9sy6wq
Fix bizarrely wrong comparison methods

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
        # TypeError is what's raised by tuples
55
55
        raise TypeError("attribute assignment not supported by frozen_mini_class")
56
56
 
57
 
    def __ne__(self, other):
58
 
        return not self.__eq__(other)
59
 
 
60
 
    def __hash__(self):
61
 
        return hash(self._cls._name) ^ hash(self._get_values_tuple())
62
 
 
63
57
 
64
58
class _FrozenMiniInstance(_FrozenMiniBase):
65
59
 
69
63
    def __len__(self):
70
64
        raise TypeError("len not supported by frozen_mini_class")
71
65
 
 
66
    def __cmp__(self, other):
 
67
        if isinstance(other, self.__class__):
 
68
            return cmp(self._get_values_tuple(), other._get_values_tuple())
 
69
        else:
 
70
            return NotImplemented
 
71
 
72
72
    def __hash__(self):
73
 
        # compares equal to a tuple, disregarding the name
74
 
        return hash(self._get_values_tuple())
75
 
 
76
 
    def __eq__(self, other):
77
 
        return (isinstance(other, self.__class__)
78
 
            and other._cls == self._cls
79
 
            and other.__dict__ == self.__dict__)
 
73
        return hash(self._cls._name) ^ hash(self._get_values_tuple())
80
74
 
81
75
 
82
76
class _FrozenIndexedMiniInstance(_FrozenMiniBase):
83
77
 
 
78
    def __cmp__(self, other):
 
79
        if isinstance(other, self.__class__):
 
80
            return cmp(self._get_values_tuple(), other._get_values_tuple())
 
81
        elif isinstance(other, tuple):
 
82
            return cmp(self._get_values_tuple(), other)
 
83
        else:
 
84
            return NotImplemented
 
85
 
84
86
    def __getitem__(self, index):
85
87
        if type(index) != int:
86
88
            raise TypeError("can't index %r by %r" % (self, index))
90
92
            raise IndexError("index %r not in %r" % (index, self._cls))
91
93
        return getattr(self, item_name)
92
94
 
 
95
    def __hash__(self):
 
96
        # compares equal to a tuple, disregarding the name
 
97
        return hash(self._get_values_tuple())
 
98
 
93
99
    def __len__(self):
94
100
        return len(self._cls._ivar_names)
95
101
 
96
 
    def __cmp__(self, other):
97
 
        if isinstance(other, self.__class__):
98
 
            return cmp(self._get_values_tuple(), other._get_values_tuple())
99
 
        elif isinstance(other, tuple):
100
 
            return cmp(other, self._get_values_tuple())
101
 
        else:
102
 
            return NotImplemented