Package lacewing :: Module idpool
[frames] | no frames]

Source Code for Module lacewing.idpool

 1  """ 
 2  Manage pool of IDs. 
 3  """ 
 4   
 5  import itertools 
 6   
7 -class IDPool(object):
8 _newIds = None 9 _freeIds = None 10
11 - def __init__(self, start = 0):
12 """ 13 Initializes a new pool, counting from start 14 """ 15 self._freeIds = [] 16 self._newIds = itertools.count(start)
17
18 - def pop(self):
19 """ 20 Take out an ID from the pool, and wait for it to be 21 put back again (see L{putBack}) 22 @return: A new ID from the pool. 23 """ 24 if self._freeIds: 25 return self._freeIds.pop() 26 else: 27 return self._newIds.next()
28
29 - def putBack(self, id):
30 """ 31 Puts back a previously popped ID. 32 """ 33 self._freeIds.append(id)
34