Added cached_property to utils.
This commit is contained in:
30
LICENSE
30
LICENSE
@@ -75,3 +75,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
utils.cached_property extracted from Celery
|
||||
-------------------------------------------
|
||||
Copyright (c) 2009-2011, Ask Solem and contributors.
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
Neither the name of Ask Solem nor the names of its contributors may be used
|
||||
to endorse or promote products derived from this software without specific
|
||||
prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
||||
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
@@ -192,3 +192,70 @@ class AppSettings(object):
|
||||
|
||||
def issetting(self, (name, value)):
|
||||
return name == name.upper()
|
||||
|
||||
|
||||
class cached_property(object):
|
||||
"""Property descriptor that caches the return value
|
||||
of the get function.
|
||||
|
||||
*Examples*
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
@cached_property
|
||||
def connection(self):
|
||||
return Connection()
|
||||
|
||||
@connection.setter # Prepares stored value
|
||||
def connection(self, value):
|
||||
if value is None:
|
||||
raise TypeError("Connection must be a connection")
|
||||
return value
|
||||
|
||||
@connection.deleter
|
||||
def connection(self, value):
|
||||
# Additional action to do at del(self.attr)
|
||||
if value is not None:
|
||||
print("Connection %r deleted" % (value, ))
|
||||
"""
|
||||
|
||||
def __init__(self, fget=None, fset=None, fdel=None, doc=None):
|
||||
self.__get = fget
|
||||
self.__set = fset
|
||||
self.__del = fdel
|
||||
self.__doc__ = doc or fget.__doc__
|
||||
self.__name__ = fget.__name__
|
||||
self.__module__ = fget.__module__
|
||||
|
||||
def __get__(self, obj, type=None):
|
||||
if obj is None:
|
||||
return self
|
||||
try:
|
||||
return obj.__dict__[self.__name__]
|
||||
except KeyError:
|
||||
value = obj.__dict__[self.__name__] = self.__get(obj)
|
||||
return value
|
||||
|
||||
def __set__(self, obj, value):
|
||||
if obj is None:
|
||||
return self
|
||||
if self.__set is not None:
|
||||
value = self.__set(obj, value)
|
||||
obj.__dict__[self.__name__] = value
|
||||
|
||||
def __delete__(self, obj):
|
||||
if obj is None:
|
||||
return self
|
||||
try:
|
||||
value = obj.__dict__.pop(self.__name__)
|
||||
except KeyError:
|
||||
pass
|
||||
else:
|
||||
if self.__del is not None:
|
||||
self.__del(obj, value)
|
||||
|
||||
def setter(self, fset):
|
||||
return self.__class__(self.__get, fset, self.__del)
|
||||
|
||||
def deleter(self, fdel):
|
||||
return self.__class__(self.__get, self.__set, fdel)
|
||||
|
||||
Reference in New Issue
Block a user