Package libxyz :: Package core :: Module queue
[hide private]
[frames] | no frames]

Source Code for Module libxyz.core.queue

 1  #-*- coding: utf8 -* 
 2  # 
 3  # Max E. Kuznecov ~syhpoon <syhpoon@syhpoon.name> 2008 
 4  # 
 5  # This file is part of XYZCommander. 
 6  # XYZCommander is free software: you can redistribute it and/or modify 
 7  # it under the terms of the GNU Lesser Public License as published by 
 8  # the Free Software Foundation, either version 3 of the License, or 
 9  # (at your option) any later version. 
10  # XYZCommander is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
13  # GNU Lesser Public License for more details. 
14  # You should have received a copy of the GNU Lesser Public License 
15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
16   
17  from libxyz.exceptions import XYZValueError 
18   
19 -class Queue(list):
20 """ 21 Fixed-sized list 22 """ 23
24 - def __init__(self, maxsize):
25 super(Queue, self).__init__() 26 27 try: 28 self.maxsize = int(maxsize) 29 except ValueError: 30 raise XYZValueError(_(u"Max-size must be of integer type"))
31 32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 33
34 - def push(self, item):
35 """ 36 Push a new item to queue. If queue already contains maxsize elements 37 replace the oldest one. 38 """ 39 40 if self.maxsize <= 0: 41 return 42 elif len(self) == self.maxsize: 43 del(self[0]) 44 45 self.append(item)
46 47 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 48
49 - def pop(self):
50 """ 51 Pop item from the beginning of the queue 52 Raise IndexError if queue is empty 53 """ 54 55 return super(Queue,self).pop(0)
56 57 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 58
59 - def clear(self):
60 """ 61 Clear queue 62 """ 63 64 del self[:]
65 66 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 67
68 - def tail(self):
69 """ 70 Return tail element 71 """ 72 73 _len = len(self) 74 75 if _len: 76 return self[_len - 1] 77 else: 78 return None
79