utils.py
changeset 10933 830f1ea52789
parent 10691 af266f27c4d5
equal deleted inserted replaced
10932:cb217b2b3463 10933:830f1ea52789
   135     for child in getattr(entity, rtype):
   135     for child in getattr(entity, rtype):
   136         if child.eid in _seen:
   136         if child.eid in _seen:
   137             continue
   137             continue
   138         for subchild in transitive_closure_of(child, rtype, _seen):
   138         for subchild in transitive_closure_of(child, rtype, _seen):
   139             yield subchild
   139             yield subchild
   140 
       
   141 
       
   142 class SizeConstrainedList(list):
       
   143     """simple list that makes sure the list does not get bigger than a given
       
   144     size.
       
   145 
       
   146     when the list is full and a new element is added, the first element of the
       
   147     list is removed before appending the new one
       
   148 
       
   149     >>> l = SizeConstrainedList(2)
       
   150     >>> l.append(1)
       
   151     >>> l.append(2)
       
   152     >>> l
       
   153     [1, 2]
       
   154     >>> l.append(3)
       
   155     >>> l
       
   156     [2, 3]
       
   157     """
       
   158     def __init__(self, maxsize):
       
   159         self.maxsize = maxsize
       
   160 
       
   161     def append(self, element):
       
   162         if len(self) == self.maxsize:
       
   163             del self[0]
       
   164         super(SizeConstrainedList, self).append(element)
       
   165 
       
   166     def extend(self, sequence):
       
   167         super(SizeConstrainedList, self).extend(sequence)
       
   168         keepafter = len(self) - self.maxsize
       
   169         if keepafter > 0:
       
   170             del self[:keepafter]
       
   171 
       
   172     __iadd__ = extend
       
   173 
   140 
   174 
   141 
   175 class RepeatList(object):
   142 class RepeatList(object):
   176     """fake a list with the same element in each row"""
   143     """fake a list with the same element in each row"""
   177     __slots__ = ('_size', '_item')
   144     __slots__ = ('_size', '_item')