src/ztfy/utils/property.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 134 72e3d386f4f6
child 186 28ecb13e485c
equal deleted inserted replaced
147:044dc196ec8a 148:d3668ecd9137
       
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2012 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 
       
    17 # import standard packages
       
    18 
       
    19 # import Zope3 interfaces
       
    20 
       
    21 # import local interfaces
       
    22 
       
    23 # import Zope3 packages
       
    24 
       
    25 # import local packages
       
    26 
       
    27 
       
    28 class cached(object):
       
    29     """Custom property decorator to define a property or function
       
    30     which is calculated only once
       
    31        
       
    32     When applied on a function, caching is based on input arguments
       
    33     """
       
    34 
       
    35     def __init__(self, function):
       
    36         self._function = function
       
    37         self._cache = {}
       
    38 
       
    39     def __call__(self, *args):
       
    40         try:
       
    41             return self._cache[args]
       
    42         except KeyError:
       
    43             self._cache[args] = self._function(*args)
       
    44             return self._cache[args]
       
    45 
       
    46     def expire(self, *args):
       
    47         del self._cache[args]
       
    48 
       
    49 
       
    50 class cached_property(object):
       
    51     """A read-only @property decorator that is only evaluated once. The value is cached
       
    52     on the object itself rather than the function or class; this should prevent
       
    53     memory leakage.
       
    54     """
       
    55     def __init__(self, fget, doc=None):
       
    56         self.fget = fget
       
    57         self.__doc__ = doc or fget.__doc__
       
    58         self.__name__ = fget.__name__
       
    59         self.__module__ = fget.__module__
       
    60 
       
    61     def __get__(self, obj, cls):
       
    62         if obj is None:
       
    63             return self
       
    64         obj.__dict__[self.__name__] = result = self.fget(obj)
       
    65         return result