1 from allmydata.util import dictutil
3 class NummedObj(object):
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.
13 objnums = dictutil.NumDict() # key: class names, value: highest used object number
15 def __init__(self, klass=None):
17 @param klass: in which class are you counted? If default value of `None', then self.__class__ will be used.
20 klass = self.__class__
21 self._classname = klass.__name__
23 NummedObj.objnums.inc(self._classname)
24 self._objid = NummedObj.objnums[self._classname]
27 return "<%s #%d>" % (self._classname, self._objid,)
29 def __lt__(self, other):
30 return (self._objid, self._classname,) < (other._objid, other._classname,)
32 def __le__(self, other):
33 return (self._objid, self._classname,) <= (other._objid, other._classname,)
35 def __eq__(self, other):
36 return (self._objid, self._classname,) == (other._objid, other._classname,)
38 def __ne__(self, other):
39 return (self._objid, self._classname,) != (other._objid, other._classname,)
41 def __gt__(self, other):
42 return (self._objid, self._classname,) > (other._objid, other._classname,)
44 def __ge__(self, other):
45 return (self._objid, self._classname,) >= (other._objid, other._classname,)