pyxmpp.expdict

Package pyxmpp :: Module expdict |
[hide private]

# Source Code for Module pyxmpp.expdict

      1  #
      2  # (C) Copyright 2003-2004 Jacek Konieczny <jajcus@jajcus.net>
      3  #
      4  # This program is free software; you can redistribute it and/or modify
      5  # it under the terms of the GNU Lesser General Public License Version
      6  # 2.1 as published by the Free Software Foundation.
      7  #
      8  # This program is distributed in the hope that it will be useful,
      9  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  # GNU Lesser General Public License for more details.
     12  #
     13  # You should have received a copy of the GNU Lesser General Public
     14  # License along with this program; if not, write to the Free Software
     15  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     16  #
     17
     18  """Dictionary with item expiration."""
     19
     20  __revision__="$Id: expdict.py 600 2005-08-19 10:45:03Z jajcus $"
     21  __docformat__="restructuredtext en"
     22
     23  import time
     24  import threading
     25
     26 -class ExpiringDictionary(dict):
     27      """An extension to standard Python dictionary objects which implements item
     28      expiration.
     29
     30      Each item in ExpiringDictionary has its expiration time assigned, after
     31      which the item is removed from the mapping.
     32
     33      :Ivariables:
     34          - `_timeouts`: a dictionary with timeout values and timeout callback for
     35            stored objects.
     36          - `_default_timeout`: the default timeout value (in seconds from now).
     37          - `_lock`: access synchronization lock.
     38      :Types:
     39          - `_timeouts`: `dict`
     40          - `_default_timeout`: `int`
     41          - `_lock`: `threading.RLock`"""
     42
     43      __slots__=['_timeouts','_default_timeout','_lock']
     44
     45 -    def __init__(self,default_timeout=300):
     46          """Initialize an `ExpiringDictionary` object.
     47
     48          :Parameters:
     49              - `default_timeout`: default timeout value for stored objects.
     50          :Types:
     51              - `default_timeout`: `int`"""
     52          dict.__init__(self)
     53          self._timeouts={}
     54          self._default_timeout=default_timeout
     55          self._lock=threading.RLock()
     56
     57 -    def __delitem__(self,key):
     58          self._lock.acquire()
     59          try:
     60              del self._timeouts[key]
     61              return dict.__delitem__(self,key)
     62          finally:
     63              self._lock.release()
     64
     65 -    def __getitem__(self,key):
     66          self._lock.acquire()
     67          try:
     68              self._expire_item(key)
     69              return dict.__getitem__(self,key)
     70          finally:
     71              self._lock.release()
     72
     73 -    def __setitem__(self,key,value):
     74          return self.set_item(key,value)
     75
     76 -    def set_item(self,key,value,timeout=None,timeout_callback=None):
     77          """Set item of the dictionary.
     78
     79          :Parameters:
     80              - `key`: the key.
     81              - `value`: the object to store.
     82              - `timeout`: timeout value for the object (in seconds from now).
     83              - `timeout_callback`: function to be called when the item expires.
     84                The callback should accept none, one (the key) or two (the key
     85                and the value) arguments.
     86          :Types:
     87              - `key`: any hashable value
     88              - `value`: any python object
     89              - `timeout`: `int`
     90              - `timeout_callback`: callable"""
     91          self._lock.acquire()
     92          try:
     93              if not timeout:
     94                  timeout=self._default_timeout
     95              self._timeouts[key]=(time.time()+timeout,timeout_callback)
     96              return dict.__setitem__(self,key,value)
     97          finally:
     98              self._lock.release()
     99
    100 -    def expire(self):
    101          """Do the expiration of dictionary items.
    102
    103          Remove items that expired by now from the dictionary."""
    104          self._lock.acquire()
    105          try:
    106              for k in self._timeouts.keys():
    107                  self._expire_item(k)
    108          finally:
    109              self._lock.release()
    110
    111 -    def _expire_item(self,key):
    112          """Do the expiration of a dictionary item.
    113
    114          Remove the item if it has expired by now.
    115
    116          :Parameters:
    117              - `key`: key to the object.
    118          :Types:
    119              - `key`: any hashable value"""
    120          (timeout,callback)=self._timeouts[key]
    121          if timeout<=time.time():
    122              if callback:
    123                  try:
    124                      callback(key,dict.__getitem__(self,key))
    125                  except TypeError:
    126                      try:
    127                          callback(key)
    128                      except TypeError:
    129                          callback()
    130              del self[key]
    131
    132  # vi: sts=4 et sw=4
    133