Package memtools :: Module protocols
[hide private]

Source Code for Module memtools.protocols

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3  # 
  4  #       protocols.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      This file defines the required protocols for most memory tools used 
 37      in this package. 
 38   
 39  """ 
 40   
 41   
 42  from functools import wraps 
 43  from memtools.pattern import Memorized, memorize 
 44   
 45   
46 -class Memory(object):
47 """ 48 Memory objects are key-value gateways to a database or any other kind 49 of memory storage. They behave the same way dictionaries do, which 50 makes it easier to mock them. 51 52 Required methods are __getitem__, __setitem__ and __delitem__. get(), 53 set() are mapped to __getitem__ and __setitem__. 54 55 A convenience method __call__ is defined to use objects of this class 56 as decorators. Doing so will apply the memorize pattern to the 57 function. You can find more information in the corresponding docstring 58 in __call__. 59 60 """ 61
62 - def __getitem__(self, key):
63 raise NotImplementedError
64
65 - def __setitem__(self, key, value):
66 raise NotImplementedError
67
68 - def __delitem__(self, key):
69 raise NotImplementedError
70
71 - def __call__(self, f):
72 """ 73 This is a convenience function that provides the Memory Pattern as 74 a decorator and wraps it in a way transparent to the end user 75 (i.e.: they will not know whether the function has been decorated 76 or not). This is acomplished by using the "wrap" decorator in 77 functools. 78 79 """ 80 memo = Memorized(f, self) 81 wraps(f)(memo) 82 return memo
83
84 - def get(self, key):
85 return self.__getitem__(key)
86
87 - def set(self, key, value):
88 self.__setitem__(key, value)
89 90
91 -class MemoryPool(Memory):
92 """ 93 A MemoryPool object provides an extended way of using Memory gateways. 94 It holds more than one connection open, so it should be theoretically 95 faster than ad-hoc connections. However, it does not necessarily imply 96 that the objects in the pool are connected to different servers. 97 98 It *must* behave like a Memory gateway too, masking the 99 lock/hold/return operations inside the MemoryPool, providing a complete 100 replacement for normal Memory objects. 101 """ 102
103 - def count(self):
104 raise NotImplementedError
105
106 - def grow(self, number=1):
107 raise NotImplementedError
108
109 - def shrink(self, number=1):
110 raise NotImplementedError
111