Moved cached_property to compressor.utils.cache.
This commit is contained in:
2
LICENSE
2
LICENSE
@@ -76,7 +76,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
utils.cached_property extracted from Celery
|
||||
utils.cache.cached_property extracted from Celery
|
||||
-------------------------------------------
|
||||
Copyright (c) 2009-2011, Ask Solem and contributors.
|
||||
All rights reserved.
|
||||
|
||||
@@ -12,7 +12,8 @@ from compressor.conf import settings
|
||||
from compressor.exceptions import CompressorError, UncompressableFileError
|
||||
from compressor.filters import CompilerFilter
|
||||
from compressor.storage import default_storage
|
||||
from compressor.utils import get_class, cached_property, staticfiles
|
||||
from compressor.utils import get_class, staticfiles
|
||||
from compressor.utils.cache import cached_property
|
||||
|
||||
class Compressor(object):
|
||||
"""
|
||||
|
||||
@@ -60,72 +60,6 @@ def walk(root, topdown=True, onerror=None, followlinks=False):
|
||||
for link_dirpath, link_dirnames, link_filenames in walk(p):
|
||||
yield (link_dirpath, link_dirnames, link_filenames)
|
||||
|
||||
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)
|
||||
|
||||
|
||||
"""Advanced string formatting for Python >= 2.4.
|
||||
|
||||
An implementation of the advanced string formatting (PEP 3101).
|
||||
|
||||
66
compressor/utils/cache.py
Normal file
66
compressor/utils/cache.py
Normal file
@@ -0,0 +1,66 @@
|
||||
|
||||
|
||||
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