Package memtools :: Package storages
[hide private]

Source Code for Package memtools.storages

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  # 
 4  #       __init__.py 
 5  # 
 6  #       Copyright 2010 Pablo Alejandro Costesich <pcostesi@alu.itba.edu.ar> 
 7  # 
 8  #       Redistribution and use in source and binary forms, with or without 
 9  #       modification, are permitted provided that the following conditions are 
10  #       met: 
11  # 
12  #       * Redistributions of source code must retain the above copyright 
13  #         notice, this list of conditions and the following disclaimer. 
14  #       * Redistributions in binary form must reproduce the above 
15  #         copyright notice, this list of conditions and the following disclaimer 
16  #         in the documentation and/or other materials provided with the 
17  #         distribution. 
18  #       * Neither the name of the Dev Team nor the names of its 
19  #         contributors may be used to endorse or promote products derived from 
20  #         this software without specific prior written permission. 
21  # 
22  #       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
23  #       "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
24  #       LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
25  #       A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
26  #       OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
27  #       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
28  #       LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
29  #       DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
30  #       THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
31  #       (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
32  #       OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
33  # 
34   
35  """ 
36  Memory decorator and utilites to speed up access to computationally expensive 
37  functions. 
38  """ 
39   
40  import random, time 
41  from memtools.protocols import Memory 
42   
43   
44  # 
45  #   AUXILIARY CLASSES 
46  # 
47   
48   
49 -class NotSet(object):
50 """ Empty class used to differenciate None from unsetted values in k-v 51 storages that return None in both cases, like python-memcached. 52 """ 53 pass
54 55
56 -class OutOfBounds(Exception):
57 pass
58 59
60 -class Alzheimer(Memory):
61 '''This is a mock class. Forget it.''' 62
63 - def __init__(self, disease=False, *args, **kwargs):
64 self._client = {} 65 if disease: 66 t = Thread(target=self.__disease) 67 t.start()
68
69 - def __setitem__(self, key, value):
70 self._client.__setitem__(key, value)
71
72 - def __getitem__(self, key):
73 return self._client.__getitem__(key)
74
75 - def __delitem__(self, key):
76 self._client.__delitem__(key)
77
78 - def keys(self, string):
79 a = [] 80 for i in self._client.keys(): 81 if i.startswith(string[:-1]): 82 a.append(i) 83 return a
84
85 - def __disease(self):
86 while True: 87 rand = random.uniform(1, 3600) 88 time.sleep(rand) 89 try: 90 del self._client[self._client.keys()[int(random.uniform(0, 91 len(self._client)))]] 92 except: 93 pass
94