]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/util/nummedobj.py
revert previous commit to fix attribution (vanity)
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / util / nummedobj.py
1 from allmydata.util import dictutil
2
3 class NummedObj(object):
4     """
5     This is useful for nicer debug printouts.  Instead of objects of the same class being
6     distinguished from one another by their memory address, they each get a unique number, which
7     can be read as "the first object of this class", "the second object of this class", etc.  This
8     is especially useful because separate runs of a program will yield identical debug output,
9     (assuming that the objects get created in the same order in each run).  This makes it possible
10     to diff outputs from separate runs to see what changed, without having to ignore a difference
11     on every line due to different memory addresses of objects.
12     """
13     objnums = dictutil.NumDict() # key: class names, value: highest used object number
14
15     def __init__(self, klass=None):
16         """
17         @param klass: in which class are you counted?  If default value of `None', then self.__class__ will be used.
18         """
19         if klass is None:
20             klass = self.__class__
21         self._classname = klass.__name__
22
23         NummedObj.objnums.inc(self._classname)
24         self._objid = NummedObj.objnums[self._classname]
25
26     def __repr__(self):
27         return "<%s #%d>" % (self._classname, self._objid,)
28
29     def __lt__(self, other):
30         return (self._objid, self._classname,) < (other._objid, other._classname,)
31
32     def __le__(self, other):
33         return (self._objid, self._classname,) <= (other._objid, other._classname,)
34
35     def __eq__(self, other):
36         return (self._objid, self._classname,) == (other._objid, other._classname,)
37
38     def __ne__(self, other):
39         return (self._objid, self._classname,) != (other._objid, other._classname,)
40
41     def __gt__(self, other):
42         return (self._objid, self._classname,) > (other._objid, other._classname,)
43
44     def __ge__(self, other):
45         return (self._objid, self._classname,) >= (other._objid, other._classname,)
46
47     def __hash__(self):
48         return id(self)