/usr/share/pyshared/zope/cachedescriptors/property.py is in python-zope.cachedescriptors 3.5.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | ##############################################################################
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
##############################################################################
"""Cached properties
See the CachedProperty class.
$Id: property.py 75652 2007-05-09 13:11:30Z zagy $
"""
ncaches = 0l
class CachedProperty(object):
"""Cached Properties.
"""
def __init__(self, func, *names):
global ncaches
ncaches += 1
self.data = (func, names,
"_v_cached_property_key_%s" % ncaches,
"_v_cached_property_value_%s" % ncaches)
def __get__(self, inst, class_):
if inst is None:
return self
func, names, key_name, value_name = self.data
key = names and [getattr(inst, name) for name in names]
value = getattr(inst, value_name, self)
if value is not self:
# We have a cached value
if key == getattr(inst, key_name, self):
# Cache is still good!
return value
# We need to compute and cache the value
value = func(inst)
setattr(inst, key_name, key)
setattr(inst, value_name, value)
return value
class Lazy(object):
"""Lazy Attributes.
"""
def __init__(self, func, name=None):
if name is None:
name = func.__name__
self.data = (func, name)
def __get__(self, inst, class_):
if inst is None:
return self
func, name = self.data
value = func(inst)
inst.__dict__[name] = value
return value
class readproperty(object):
def __init__(self, func):
self.func = func
def __get__(self, inst, class_):
if inst is None:
return self
func = self.func
return func(inst)
class cachedIn(object):
"""Cached property with given cache attribute."""
def __init__(self, attribute_name):
self.attribute_name = attribute_name
def __call__(self, func):
def get(instance):
try:
value = getattr(instance, self.attribute_name)
except AttributeError:
value = func(instance)
setattr(instance, self.attribute_name, value)
return value
return property(get)
|