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          self.maxsize = 0
     28          self.set_size(maxsize)
     29
     30      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     31
     32 -    def set_size(self, size):
     33          """
     34          Set queue size
     35          """
     36
     37          try:
     38              maxsize = int(size)
     39
     40              assert maxsize >= 0
     41          except (ValueError, AssertionError):
     42              raise XYZValueError(
     43                  _(u"Max-size must be a positive integer number"))
     44          else:
     45              self.maxsize = maxsize
     46
     47      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     48
     49 -    def push(self, item):
     50          """
     51          Push a new item to queue. If queue already contains maxsize elements
     52          replace the oldest one.
     53          """
     54
     55          _len = len(self)
     56
     57          if self.maxsize <= 0:
     58              return
     59          elif _len > self.maxsize:
     60              m = _len - self.maxsize + 1
     61              self[0:] = self[m:_len]
     62          elif _len == self.maxsize:
     63              del(self[0])
     64
     65          self.append(item)
     66
     67      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     68
     69 -    def pop(self):
     70          """
     71          Pop item from the beginning of the queue
     72          Raise IndexError if queue is empty
     73          """
     74
     75          return super(Queue,self).pop(0)
     76
     77      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     78
     79 -    def clear(self):
     80          """
     81          Clear queue
     82          """
     83
     84          del self[:]
     85
     86      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     87
     88 -    def tail(self):
     89          """
     90          Return tail element
     91          """
     92
     93          _len = len(self)
     94
     95          if _len:
     96              return self[_len - 1]
     97          else:
     98              return None
     99