~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/inventory.py

  • Committer: Martin Pool
  • Date: 2005-05-19 11:07:36 UTC
  • Revision ID: mbp@sourcefrog.net-20050519110736-938c6496f0b436e5
- Define __eq__ and __ne__ for Inventory and InventoryEntry objects,
  not __cmp__.  (Apparently this is the preferred style for modern
  Python code.)

- Make those classes explicitly not hashable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
210
210
 
211
211
    from_element = classmethod(from_element)
212
212
 
213
 
    def __cmp__(self, other):
214
 
        if self is other:
215
 
            return 0
 
213
    def __eq__(self, other):
216
214
        if not isinstance(other, InventoryEntry):
217
215
            return NotImplemented
218
216
 
219
 
        return cmp(self.file_id, other.file_id) \
220
 
               or cmp(self.name, other.name) \
221
 
               or cmp(self.text_sha1, other.text_sha1) \
222
 
               or cmp(self.text_size, other.text_size) \
223
 
               or cmp(self.text_id, other.text_id) \
224
 
               or cmp(self.parent_id, other.parent_id) \
225
 
               or cmp(self.kind, other.kind)
 
217
        return (self.file_id == other.file_id) \
 
218
               and (self.name == other.name) \
 
219
               and (self.text_sha1 == other.text_sha1) \
 
220
               and (self.text_size == other.text_size) \
 
221
               and (self.text_id == other.text_id) \
 
222
               and (self.parent_id == other.parent_id) \
 
223
               and (self.kind == other.kind)
 
224
 
 
225
 
 
226
    def __ne__(self, other):
 
227
        return not (self == other)
 
228
 
 
229
    def __hash__(self):
 
230
        raise ValueError('not hashable')
226
231
 
227
232
 
228
233
 
234
239
        self.parent_id = None
235
240
        self.name = ''
236
241
 
237
 
    def __cmp__(self, other):
238
 
        if self is other:
239
 
            return 0
 
242
    def __eq__(self, other):
240
243
        if not isinstance(other, RootEntry):
241
244
            return NotImplemented
242
 
        return cmp(self.file_id, other.file_id) \
243
 
               or cmp(self.children, other.children)
 
245
        
 
246
        return (self.file_id == other.file_id) \
 
247
               and (self.children == other.children)
244
248
 
245
249
 
246
250
 
481
485
    from_element = classmethod(from_element)
482
486
 
483
487
 
484
 
    def __cmp__(self, other):
 
488
    def __eq__(self, other):
485
489
        """Compare two sets by comparing their contents.
486
490
 
487
491
        >>> i1 = Inventory()
495
499
        >>> i1 == i2
496
500
        True
497
501
        """
498
 
        if self is other:
499
 
            return 0
500
 
        
501
502
        if not isinstance(other, Inventory):
502
503
            return NotImplemented
503
504
 
504
 
        byid = self._byid
505
 
        otherids = other._byid
506
 
 
507
 
        if len(byid) != len(otherids):
 
505
        if len(self._byid) != len(other._byid):
508
506
            # shortcut: obviously not the same
509
 
            return 1                    
510
 
        
511
 
        for file_id in byid:
512
 
            if file_id not in otherids:
513
 
                return 1
514
 
            
515
 
            c = cmp(byid[file_id], otherids[file_id])
516
 
            if c: return c
517
 
 
518
 
        for file_id in otherids:
519
 
            if file_id not in byid:
520
 
                return 1
521
 
 
522
 
        return 0
 
507
            return False
 
508
 
 
509
        return self._byid == other._byid
 
510
 
 
511
 
 
512
    def __ne__(self, other):
 
513
        return not (self == other)
 
514
 
 
515
 
 
516
    def __hash__(self):
 
517
        raise ValueError('not hashable')
 
518
 
523
519
 
524
520
 
525
521
    def get_idpath(self, file_id):