Browse Source

Use a simpler version of the cached_property decorator.

From https://github.com/pydanny/cached-propertyi. This fixes an issue with
docstrings.
pull/136/head
Simon Conseil 12 years ago
parent
commit
e2cfbb08f1
  1. 6
      sigal/gallery.py
  2. 49
      sigal/utils.py

6
sigal/gallery.py

@ -147,16 +147,16 @@ class Image(Media):
type = 'image'
extensions = ('.jpg', '.jpeg', '.png')
@cached_property()
@cached_property
def date(self):
return self.exif and self.exif.get('dateobj', None) or None
@cached_property()
@cached_property
def exif(self):
return (get_exif_tags(self.raw_exif)
if self.raw_exif and self.ext in ('.jpg', '.jpeg') else None)
@cached_property()
@cached_property
def raw_exif(self):
try:
return (get_exif_data(self.src_path)

49
sigal/utils.py

@ -99,36 +99,21 @@ def call_subprocess(cmd):
class cached_property(object):
'''Decorator for read-only properties evaluated only once.
© 2011 Christopher Arndt, MIT License
https://wiki.python.org/moin/PythonDecoratorLibrary#Cached_Properties
The value is cached in the '_cache' attribute of the object instance that
has the property getter method wrapped by this decorator. The '_cache'
attribute value is a dictionary which has a key for every property of the
object which is wrapped by this decorator. Each entry in the cache is
created only when the property is accessed for the first time and is a
two-element tuple with the last computed property value and the last time
it was updated in seconds since the epoch.
'''
def __call__(self, fget, doc=None):
self.fget = fget
self.__doc__ = doc or fget.__doc__
self.__name__ = fget.__name__
self.__module__ = fget.__module__
return self
def __get__(self, inst, owner):
try:
value = inst._cache[self.__name__]
except (KeyError, AttributeError):
value = self.fget(inst)
try:
cache = inst._cache
except AttributeError:
cache = inst._cache = {}
cache[self.__name__] = value
""" A property that is only computed once per instance and then replaces
itself with an ordinary attribute. Deleting the attribute resets the
property.
Source:
https://github.com/pydanny/cached-property (BSD Licensed)
https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
"""
def __init__(self, func):
self.__doc__ = getattr(func, '__doc__')
self.func = func
def __get__(self, obj, cls):
if obj is None:
return self
value = obj.__dict__[self.func.__name__] = self.func(obj)
return value

Loading…
Cancel
Save