zope.cachedescriptors


Namezope.cachedescriptors JSON
Version 5.0 PyPI version JSON
download
home_pagehttp://github.com/zopefoundation/zope.cachedescriptors
SummaryMethod and property caching decorators
upload_time2023-03-27 08:46:05
maintainer
docs_urlNone
authorZope Foundation and Contributors
requires_python>=3.7
licenseZPL 2.1
keywords persistent caching decorator method property
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ===========================
 ``zope.cachedescriptors``
===========================

.. image:: https://img.shields.io/pypi/v/zope.cachedescriptors.svg
        :target: https://pypi.org/project/zope.cachedescriptors/
        :alt: Latest release

.. image:: https://img.shields.io/pypi/pyversions/zope.cachedescriptors.svg
        :target: https://pypi.org/project/zope.cachedescriptors/
        :alt: Supported Python versions

.. image:: https://github.com/zopefoundation/zope.cachedescriptors/actions/workflows/tests.yml/badge.svg
        :target: https://github.com/zopefoundation/zope.cachedescriptors/actions/workflows/tests.yml

.. image:: https://readthedocs.org/projects/zopehookable/badge/?version=latest
        :target: http://zopehookable.readthedocs.io/en/latest/
        :alt: Documentation Status

.. image:: https://coveralls.io/repos/github/zopefoundation/zope.cachedescriptors/badge.svg?branch=master
        :target: https://coveralls.io/github/zopefoundation/zope.cachedescriptors?branch=master

Cached descriptors cache their output.  They take into account
instance attributes that they depend on, so when the instance
attributes change, the descriptors will change the values they
return.

Cached descriptors cache their data in ``_v_`` attributes, so they are
also useful for managing the computation of volatile attributes for
persistent objects.

Persistent descriptors:

- ``property``

  A simple computed property.

  See ``src/zope/cachedescriptors/property.rst``.

- ``method``

  Idempotent method.  The return values are cached based on method
  arguments and on any instance attributes that the methods are
  defined to depend on.

  .. note::

     Only a cache based on arguments has been implemented so far.

  See ``src/zope/cachedescriptors/method.rst``.

===================
 Cached Properties
===================

Cached properties are computed properties that cache their computed
values.  They take into account instance attributes that they depend
on, so when the instance attributes change, the properties will change
the values they return.

CachedProperty
==============

Cached properties cache their data in ``_v_`` attributes, so they are
also useful for managing the computation of volatile attributes for
persistent objects. Let's look at an example:

    >>> from zope.cachedescriptors import property
    >>> import math

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.CachedProperty('x', 'y')
    ...     def radius(self):
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> point = Point(1.0, 2.0)

If we ask for the radius the first time:

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

We see that the radius function is called, but if we ask for it again:

    >>> '%.2f' % point.radius
    '2.24'

The function isn't called.  If we change one of the attribute the
radius depends on, it will be recomputed:

    >>> point.x = 2.0
    >>> '%.2f' % point.radius
    computing radius
    '2.83'

But changing other attributes doesn't cause recomputation:

    >>> point.q = 1
    >>> '%.2f' % point.radius
    '2.83'

Note that we don't have any non-volitile attributes added:

    >>> names = [name for name in point.__dict__ if not name.startswith('_v_')]
    >>> names.sort()
    >>> names
    ['q', 'x', 'y']

For backwards compatibility, the same thing can alternately be written
without using decorator syntax:

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     def radius(self):
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)
    ...     radius = property.CachedProperty(radius, 'x', 'y')

    >>> point = Point(1.0, 2.0)

If we ask for the radius the first time:

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

We see that the radius function is called, but if we ask for it again:

    >>> '%.2f' % point.radius
    '2.24'

The function isn't called.  If we change one of the attribute the
radius depends on, it will be recomputed:

    >>> point.x = 2.0
    >>> '%.2f' % point.radius
    computing radius
    '2.83'

Documentation and the ``__name__`` are preserved if the attribute is accessed through
the class. This allows Sphinx to extract the documentation.

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.CachedProperty('x', 'y')
    ...     def radius(self):
    ...         '''The length of the line between self.x and self.y'''
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> print(Point.radius.__doc__)
    The length of the line between self.x and self.y
    >>> print(Point.radius.__name__)
    radius

It is possible to specify a CachedProperty that has no dependencies.
For backwards compatibility this can be written in a few different ways::

    >>> class Point:
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.CachedProperty
    ...     def no_deps_no_parens(self):
    ...         print("No deps, no parens")
    ...         return 1
    ...
    ...     @property.CachedProperty()
    ...     def no_deps(self):
    ...         print("No deps")
    ...         return 2
    ...
    ...     def no_deps_old_style(self):
    ...         print("No deps, old style")
    ...         return 3
    ...     no_deps_old_style = property.CachedProperty(no_deps_old_style)


    >>> point = Point(1.0, 2.0)
    >>> point.no_deps_no_parens
    No deps, no parens
    1
    >>> point.no_deps_no_parens
    1
    >>> point.no_deps
    No deps
    2
    >>> point.no_deps
    2
    >>> point.no_deps_old_style
    No deps, old style
    3
    >>> point.no_deps_old_style
    3


Lazy Computed Attributes
========================

The `property` module provides another descriptor that supports a
slightly different caching model: lazy attributes.  Like cached
proprties, they are computed the first time they are used. however,
they aren't stored in volatile attributes and they aren't
automatically updated when other attributes change.  Furthermore, the
store their data using their attribute name, thus overriding
themselves. This provides much faster attribute access after the
attribute has been computed. Let's look at the previous example using
lazy attributes:

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.Lazy
    ...     def radius(self):
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> point = Point(1.0, 2.0)

If we ask for the radius the first time:

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

We see that the radius function is called, but if we ask for it again:

    >>> '%.2f' % point.radius
    '2.24'

The function isn't called.  If we change one of the attribute the
radius depends on, it still isn't called:

    >>> point.x = 2.0
    >>> '%.2f' % point.radius
    '2.24'

If we want the radius to be recomputed, we have to manually delete it:

    >>> del point.radius

    >>> point.x = 2.0
    >>> '%.2f' % point.radius
    computing radius
    '2.83'

Note that the radius is stored in the instance dictionary:

    >>> '%.2f' % point.__dict__['radius']
    '2.83'

The lazy attribute needs to know the attribute name.  It normally
deduces the attribute name from the name of the function passed. If we
want to use a different name, we need to pass it:

    >>> def d(point):
    ...     print('computing diameter')
    ...     return 2*point.radius

    >>> Point.diameter = property.Lazy(d, 'diameter')
    >>> '%.2f' % point.diameter
    computing diameter
    '5.66'

Documentation and the ``__name__`` are preserved if the attribute is accessed through
the class. This allows Sphinx to extract the documentation.

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.Lazy
    ...     def radius(self):
    ...         '''The length of the line between self.x and self.y'''
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> print(Point.radius.__doc__)
    The length of the line between self.x and self.y
    >>> print(Point.radius.__name__)
    radius

The documentation of the attribute when accessed through the
instance will be the same as the return-value:

   >>> p = Point(1.0, 2.0)
   >>> p.radius.__doc__ == float.__doc__
   computing radius
   True

This is the same behaviour as the standard Python ``property``
decorator.

readproperty
============

readproperties are like lazy computed attributes except that the
attribute isn't set by the property:


    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.readproperty
    ...     def radius(self):
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> point = Point(1.0, 2.0)

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

But you *can* replace the property by setting a value. This is the major
difference to the builtin `property`:

    >>> point.radius = 5
    >>> point.radius
    5

Documentation and the ``__name__`` are preserved if the attribute is accessed through
the class. This allows Sphinx to extract the documentation.

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.readproperty
    ...     def radius(self):
    ...         '''The length of the line between self.x and self.y'''
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> print(Point.radius.__doc__)
    The length of the line between self.x and self.y
    >>> print(Point.radius.__name__)
    radius

cachedIn
========

The `cachedIn` property allows to specify the attribute where to store the
computed value:

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.cachedIn('_radius_attribute')
    ...     def radius(self):
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> point = Point(1.0, 2.0)

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

    >>> '%.2f' % point.radius
    '2.24'

The radius is cached in the attribute with the given name, `_radius_attribute`
in this case:

    >>> '%.2f' % point._radius_attribute
    '2.24'

When the attribute is removed the radius is re-calculated once. This allows
invalidation:

    >>> del point._radius_attribute

    >>> '%.2f' % point.radius
    computing radius
    '2.24'

    >>> '%.2f' % point.radius
    '2.24'

Documentation is preserved if the attribute is accessed through
the class. This allows Sphinx to extract the documentation.

    >>> class Point:
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @property.cachedIn('_radius_attribute')
    ...     def radius(self):
    ...         '''The length of the line between self.x and self.y'''
    ...         print('computing radius')
    ...         return math.sqrt(self.x**2 + self.y**2)

    >>> print(Point.radius.__doc__)
    The length of the line between self.x and self.y

==============
 Method Cache
==============

cachedIn
========

The `cachedIn` property allows to specify the attribute where to store the
computed value:

    >>> import math
    >>> from zope.cachedescriptors import method

    >>> class Point(object):
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @method.cachedIn('_cache')
    ...     def distance(self, x, y):
    ...         """Compute the distance"""
    ...         print('computing distance')
    ...         return math.hypot(self.x - x, self.y - y)
    ...
    >>> point = Point(1.0, 2.0)

The value is computed once:

    >>> point.distance(2, 2)
    computing distance
    1.0
    >>> point.distance(2, 2)
    1.0


Using different arguments calculates a new distance:

    >>> point.distance(5, 2)
    computing distance
    4.0
    >>> point.distance(5, 2)
    4.0


The data is stored at the given `_cache` attribute:

    >>> isinstance(point._cache, dict)
    True

    >>> sorted(point._cache.items())
    [(((2, 2), ()), 1.0), (((5, 2), ()), 4.0)]


It is possible to exlicitly invalidate the data:

    >>> point.distance.invalidate(point, 5, 2)
    >>> point.distance(5, 2)
    computing distance
    4.0

Invalidating keys which are not in the cache, does not result in an error:

    >>> point.distance.invalidate(point, 47, 11)

The documentation of the function is preserved (whether through the
instance or the class), allowing Sphinx to extract it::

    >>> print(point.distance.__doc__)
    Compute the distance
    >>> print(point.distance.__name__)
    distance

    >>> print(Point.distance.__doc__)
    Compute the distance
    >>> print(Point.distance.__name__)
    distance

It is possible to pass in a factory for the cache attribute. Create another
Point class:


    >>> class MyDict(dict):
    ...     pass
    >>> class Point(object):
    ...
    ...     def __init__(self, x, y):
    ...         self.x, self.y = x, y
    ...
    ...     @method.cachedIn('_cache', MyDict)
    ...     def distance(self, x, y):
    ...         print('computing distance')
    ...         return math.sqrt((self.x - x)**2 + (self.y - y)**2)
    ...
    >>> point = Point(1.0, 2.0)
    >>> point.distance(2, 2)
    computing distance
    1.0

Now the cache is a MyDict instance:

    >>> isinstance(point._cache, MyDict)
    True

=========
 Changes
=========

5.0 (2023-03-27)
================

- Add support for Python 3.11.

- Drop support for Python 2.7, 3.5, 3.6.


4.4 (2022-09-07)
================

- Drop support for Python 3.4.

- Add support for Python 3.7, 3.8, 3.9, 3.10.


4.3.1 (2017-12-09)
==================

- Fix test which will break in the upcoming Python 3.7 release.


4.3.0 (2017-07-27)
==================

- Add support for Python 3.6.

- Drop support for Python 3.3.


4.2.0 (2016-09-05)
==================

- Add support for Python 3.5.

- Drop support for Python 2.6 and 3.2.

- The properties from the ``property`` module all preserve the
  documentation string of the underlying function, and all except
  ``cachedIn`` preserve everything that ``functools.update_wrapper``
  preserves.

- ``property.CachedProperty`` is usable as a decorator, with or
  without dependent attribute names.

- ``method.cachedIn`` preserves the documentation string of the
  underlying function, and everything else that ``functools.wraps`` preserves.

4.1.0 (2014-12-26)
==================

- Add support for PyPy and PyPy3.

- Add support for Python 3.4.

- Add support for testing on Travis.


4.0.0 (2013-02-13)
==================

- Drop support for Python 2.4 and 2.5.

- Add support for Python 3.2 and 3.3.


3.5.1 (2010-04-30)
==================

- Remove undeclared testing dependency on zope.testing.

3.5.0 (2009-02-10)
==================

- Remove dependency on ZODB by allowing to specify storage factory for
  ``zope.cachedescriptors.method.cachedIn`` which is now ``dict`` by default.
  If you need to use BTree instead, you must pass it as ``factory`` argument
  to the ``zope.cachedescriptors.method.cachedIn`` decorator.

- Remove zpkg-related file.

- Clean up package description and documentation a bit.

- Change package mailing list address to zope-dev at zope.org, as
  zope3-dev at zope.org is now retired.

3.4.0 (2007-08-30)
==================

Initial release as an independent package

            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/zopefoundation/zope.cachedescriptors",
    "name": "zope.cachedescriptors",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": "",
    "keywords": "persistent caching decorator method property",
    "author": "Zope Foundation and Contributors",
    "author_email": "zope-dev@zope.dev",
    "download_url": "https://files.pythonhosted.org/packages/43/57/ad4078768d4c6557b1a0df5a3fb117bd1d54785b4a0763648304b3388560/zope.cachedescriptors-5.0.tar.gz",
    "platform": null,
    "description": "===========================\n ``zope.cachedescriptors``\n===========================\n\n.. image:: https://img.shields.io/pypi/v/zope.cachedescriptors.svg\n        :target: https://pypi.org/project/zope.cachedescriptors/\n        :alt: Latest release\n\n.. image:: https://img.shields.io/pypi/pyversions/zope.cachedescriptors.svg\n        :target: https://pypi.org/project/zope.cachedescriptors/\n        :alt: Supported Python versions\n\n.. image:: https://github.com/zopefoundation/zope.cachedescriptors/actions/workflows/tests.yml/badge.svg\n        :target: https://github.com/zopefoundation/zope.cachedescriptors/actions/workflows/tests.yml\n\n.. image:: https://readthedocs.org/projects/zopehookable/badge/?version=latest\n        :target: http://zopehookable.readthedocs.io/en/latest/\n        :alt: Documentation Status\n\n.. image:: https://coveralls.io/repos/github/zopefoundation/zope.cachedescriptors/badge.svg?branch=master\n        :target: https://coveralls.io/github/zopefoundation/zope.cachedescriptors?branch=master\n\nCached descriptors cache their output.  They take into account\ninstance attributes that they depend on, so when the instance\nattributes change, the descriptors will change the values they\nreturn.\n\nCached descriptors cache their data in ``_v_`` attributes, so they are\nalso useful for managing the computation of volatile attributes for\npersistent objects.\n\nPersistent descriptors:\n\n- ``property``\n\n  A simple computed property.\n\n  See ``src/zope/cachedescriptors/property.rst``.\n\n- ``method``\n\n  Idempotent method.  The return values are cached based on method\n  arguments and on any instance attributes that the methods are\n  defined to depend on.\n\n  .. note::\n\n     Only a cache based on arguments has been implemented so far.\n\n  See ``src/zope/cachedescriptors/method.rst``.\n\n===================\n Cached Properties\n===================\n\nCached properties are computed properties that cache their computed\nvalues.  They take into account instance attributes that they depend\non, so when the instance attributes change, the properties will change\nthe values they return.\n\nCachedProperty\n==============\n\nCached properties cache their data in ``_v_`` attributes, so they are\nalso useful for managing the computation of volatile attributes for\npersistent objects. Let's look at an example:\n\n    >>> from zope.cachedescriptors import property\n    >>> import math\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.CachedProperty('x', 'y')\n    ...     def radius(self):\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> point = Point(1.0, 2.0)\n\nIf we ask for the radius the first time:\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\nWe see that the radius function is called, but if we ask for it again:\n\n    >>> '%.2f' % point.radius\n    '2.24'\n\nThe function isn't called.  If we change one of the attribute the\nradius depends on, it will be recomputed:\n\n    >>> point.x = 2.0\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.83'\n\nBut changing other attributes doesn't cause recomputation:\n\n    >>> point.q = 1\n    >>> '%.2f' % point.radius\n    '2.83'\n\nNote that we don't have any non-volitile attributes added:\n\n    >>> names = [name for name in point.__dict__ if not name.startswith('_v_')]\n    >>> names.sort()\n    >>> names\n    ['q', 'x', 'y']\n\nFor backwards compatibility, the same thing can alternately be written\nwithout using decorator syntax:\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     def radius(self):\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n    ...     radius = property.CachedProperty(radius, 'x', 'y')\n\n    >>> point = Point(1.0, 2.0)\n\nIf we ask for the radius the first time:\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\nWe see that the radius function is called, but if we ask for it again:\n\n    >>> '%.2f' % point.radius\n    '2.24'\n\nThe function isn't called.  If we change one of the attribute the\nradius depends on, it will be recomputed:\n\n    >>> point.x = 2.0\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.83'\n\nDocumentation and the ``__name__`` are preserved if the attribute is accessed through\nthe class. This allows Sphinx to extract the documentation.\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.CachedProperty('x', 'y')\n    ...     def radius(self):\n    ...         '''The length of the line between self.x and self.y'''\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> print(Point.radius.__doc__)\n    The length of the line between self.x and self.y\n    >>> print(Point.radius.__name__)\n    radius\n\nIt is possible to specify a CachedProperty that has no dependencies.\nFor backwards compatibility this can be written in a few different ways::\n\n    >>> class Point:\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.CachedProperty\n    ...     def no_deps_no_parens(self):\n    ...         print(\"No deps, no parens\")\n    ...         return 1\n    ...\n    ...     @property.CachedProperty()\n    ...     def no_deps(self):\n    ...         print(\"No deps\")\n    ...         return 2\n    ...\n    ...     def no_deps_old_style(self):\n    ...         print(\"No deps, old style\")\n    ...         return 3\n    ...     no_deps_old_style = property.CachedProperty(no_deps_old_style)\n\n\n    >>> point = Point(1.0, 2.0)\n    >>> point.no_deps_no_parens\n    No deps, no parens\n    1\n    >>> point.no_deps_no_parens\n    1\n    >>> point.no_deps\n    No deps\n    2\n    >>> point.no_deps\n    2\n    >>> point.no_deps_old_style\n    No deps, old style\n    3\n    >>> point.no_deps_old_style\n    3\n\n\nLazy Computed Attributes\n========================\n\nThe `property` module provides another descriptor that supports a\nslightly different caching model: lazy attributes.  Like cached\nproprties, they are computed the first time they are used. however,\nthey aren't stored in volatile attributes and they aren't\nautomatically updated when other attributes change.  Furthermore, the\nstore their data using their attribute name, thus overriding\nthemselves. This provides much faster attribute access after the\nattribute has been computed. Let's look at the previous example using\nlazy attributes:\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.Lazy\n    ...     def radius(self):\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> point = Point(1.0, 2.0)\n\nIf we ask for the radius the first time:\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\nWe see that the radius function is called, but if we ask for it again:\n\n    >>> '%.2f' % point.radius\n    '2.24'\n\nThe function isn't called.  If we change one of the attribute the\nradius depends on, it still isn't called:\n\n    >>> point.x = 2.0\n    >>> '%.2f' % point.radius\n    '2.24'\n\nIf we want the radius to be recomputed, we have to manually delete it:\n\n    >>> del point.radius\n\n    >>> point.x = 2.0\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.83'\n\nNote that the radius is stored in the instance dictionary:\n\n    >>> '%.2f' % point.__dict__['radius']\n    '2.83'\n\nThe lazy attribute needs to know the attribute name.  It normally\ndeduces the attribute name from the name of the function passed. If we\nwant to use a different name, we need to pass it:\n\n    >>> def d(point):\n    ...     print('computing diameter')\n    ...     return 2*point.radius\n\n    >>> Point.diameter = property.Lazy(d, 'diameter')\n    >>> '%.2f' % point.diameter\n    computing diameter\n    '5.66'\n\nDocumentation and the ``__name__`` are preserved if the attribute is accessed through\nthe class. This allows Sphinx to extract the documentation.\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.Lazy\n    ...     def radius(self):\n    ...         '''The length of the line between self.x and self.y'''\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> print(Point.radius.__doc__)\n    The length of the line between self.x and self.y\n    >>> print(Point.radius.__name__)\n    radius\n\nThe documentation of the attribute when accessed through the\ninstance will be the same as the return-value:\n\n   >>> p = Point(1.0, 2.0)\n   >>> p.radius.__doc__ == float.__doc__\n   computing radius\n   True\n\nThis is the same behaviour as the standard Python ``property``\ndecorator.\n\nreadproperty\n============\n\nreadproperties are like lazy computed attributes except that the\nattribute isn't set by the property:\n\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.readproperty\n    ...     def radius(self):\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> point = Point(1.0, 2.0)\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\nBut you *can* replace the property by setting a value. This is the major\ndifference to the builtin `property`:\n\n    >>> point.radius = 5\n    >>> point.radius\n    5\n\nDocumentation and the ``__name__`` are preserved if the attribute is accessed through\nthe class. This allows Sphinx to extract the documentation.\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.readproperty\n    ...     def radius(self):\n    ...         '''The length of the line between self.x and self.y'''\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> print(Point.radius.__doc__)\n    The length of the line between self.x and self.y\n    >>> print(Point.radius.__name__)\n    radius\n\ncachedIn\n========\n\nThe `cachedIn` property allows to specify the attribute where to store the\ncomputed value:\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.cachedIn('_radius_attribute')\n    ...     def radius(self):\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> point = Point(1.0, 2.0)\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\n    >>> '%.2f' % point.radius\n    '2.24'\n\nThe radius is cached in the attribute with the given name, `_radius_attribute`\nin this case:\n\n    >>> '%.2f' % point._radius_attribute\n    '2.24'\n\nWhen the attribute is removed the radius is re-calculated once. This allows\ninvalidation:\n\n    >>> del point._radius_attribute\n\n    >>> '%.2f' % point.radius\n    computing radius\n    '2.24'\n\n    >>> '%.2f' % point.radius\n    '2.24'\n\nDocumentation is preserved if the attribute is accessed through\nthe class. This allows Sphinx to extract the documentation.\n\n    >>> class Point:\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @property.cachedIn('_radius_attribute')\n    ...     def radius(self):\n    ...         '''The length of the line between self.x and self.y'''\n    ...         print('computing radius')\n    ...         return math.sqrt(self.x**2 + self.y**2)\n\n    >>> print(Point.radius.__doc__)\n    The length of the line between self.x and self.y\n\n==============\n Method Cache\n==============\n\ncachedIn\n========\n\nThe `cachedIn` property allows to specify the attribute where to store the\ncomputed value:\n\n    >>> import math\n    >>> from zope.cachedescriptors import method\n\n    >>> class Point(object):\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @method.cachedIn('_cache')\n    ...     def distance(self, x, y):\n    ...         \"\"\"Compute the distance\"\"\"\n    ...         print('computing distance')\n    ...         return math.hypot(self.x - x, self.y - y)\n    ...\n    >>> point = Point(1.0, 2.0)\n\nThe value is computed once:\n\n    >>> point.distance(2, 2)\n    computing distance\n    1.0\n    >>> point.distance(2, 2)\n    1.0\n\n\nUsing different arguments calculates a new distance:\n\n    >>> point.distance(5, 2)\n    computing distance\n    4.0\n    >>> point.distance(5, 2)\n    4.0\n\n\nThe data is stored at the given `_cache` attribute:\n\n    >>> isinstance(point._cache, dict)\n    True\n\n    >>> sorted(point._cache.items())\n    [(((2, 2), ()), 1.0), (((5, 2), ()), 4.0)]\n\n\nIt is possible to exlicitly invalidate the data:\n\n    >>> point.distance.invalidate(point, 5, 2)\n    >>> point.distance(5, 2)\n    computing distance\n    4.0\n\nInvalidating keys which are not in the cache, does not result in an error:\n\n    >>> point.distance.invalidate(point, 47, 11)\n\nThe documentation of the function is preserved (whether through the\ninstance or the class), allowing Sphinx to extract it::\n\n    >>> print(point.distance.__doc__)\n    Compute the distance\n    >>> print(point.distance.__name__)\n    distance\n\n    >>> print(Point.distance.__doc__)\n    Compute the distance\n    >>> print(Point.distance.__name__)\n    distance\n\nIt is possible to pass in a factory for the cache attribute. Create another\nPoint class:\n\n\n    >>> class MyDict(dict):\n    ...     pass\n    >>> class Point(object):\n    ...\n    ...     def __init__(self, x, y):\n    ...         self.x, self.y = x, y\n    ...\n    ...     @method.cachedIn('_cache', MyDict)\n    ...     def distance(self, x, y):\n    ...         print('computing distance')\n    ...         return math.sqrt((self.x - x)**2 + (self.y - y)**2)\n    ...\n    >>> point = Point(1.0, 2.0)\n    >>> point.distance(2, 2)\n    computing distance\n    1.0\n\nNow the cache is a MyDict instance:\n\n    >>> isinstance(point._cache, MyDict)\n    True\n\n=========\n Changes\n=========\n\n5.0 (2023-03-27)\n================\n\n- Add support for Python 3.11.\n\n- Drop support for Python 2.7, 3.5, 3.6.\n\n\n4.4 (2022-09-07)\n================\n\n- Drop support for Python 3.4.\n\n- Add support for Python 3.7, 3.8, 3.9, 3.10.\n\n\n4.3.1 (2017-12-09)\n==================\n\n- Fix test which will break in the upcoming Python 3.7 release.\n\n\n4.3.0 (2017-07-27)\n==================\n\n- Add support for Python 3.6.\n\n- Drop support for Python 3.3.\n\n\n4.2.0 (2016-09-05)\n==================\n\n- Add support for Python 3.5.\n\n- Drop support for Python 2.6 and 3.2.\n\n- The properties from the ``property`` module all preserve the\n  documentation string of the underlying function, and all except\n  ``cachedIn`` preserve everything that ``functools.update_wrapper``\n  preserves.\n\n- ``property.CachedProperty`` is usable as a decorator, with or\n  without dependent attribute names.\n\n- ``method.cachedIn`` preserves the documentation string of the\n  underlying function, and everything else that ``functools.wraps`` preserves.\n\n4.1.0 (2014-12-26)\n==================\n\n- Add support for PyPy and PyPy3.\n\n- Add support for Python 3.4.\n\n- Add support for testing on Travis.\n\n\n4.0.0 (2013-02-13)\n==================\n\n- Drop support for Python 2.4 and 2.5.\n\n- Add support for Python 3.2 and 3.3.\n\n\n3.5.1 (2010-04-30)\n==================\n\n- Remove undeclared testing dependency on zope.testing.\n\n3.5.0 (2009-02-10)\n==================\n\n- Remove dependency on ZODB by allowing to specify storage factory for\n  ``zope.cachedescriptors.method.cachedIn`` which is now ``dict`` by default.\n  If you need to use BTree instead, you must pass it as ``factory`` argument\n  to the ``zope.cachedescriptors.method.cachedIn`` decorator.\n\n- Remove zpkg-related file.\n\n- Clean up package description and documentation a bit.\n\n- Change package mailing list address to zope-dev at zope.org, as\n  zope3-dev at zope.org is now retired.\n\n3.4.0 (2007-08-30)\n==================\n\nInitial release as an independent package\n",
    "bugtrack_url": null,
    "license": "ZPL 2.1",
    "summary": "Method and property caching decorators",
    "version": "5.0",
    "split_keywords": [
        "persistent",
        "caching",
        "decorator",
        "method",
        "property"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a93b508a9a15414f5ccc9b0aa2dc13acd87159f664ff1e5058bbfd1e4853f501",
                "md5": "acf38f780e8c525f18663be594d85264",
                "sha256": "7ee05950c12c241104c9c91530f128d9d96d43d260e0b57864382ee2f3272f8b"
            },
            "downloads": -1,
            "filename": "zope.cachedescriptors-5.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "acf38f780e8c525f18663be594d85264",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 13146,
            "upload_time": "2023-03-27T08:46:03",
            "upload_time_iso_8601": "2023-03-27T08:46:03.423081Z",
            "url": "https://files.pythonhosted.org/packages/a9/3b/508a9a15414f5ccc9b0aa2dc13acd87159f664ff1e5058bbfd1e4853f501/zope.cachedescriptors-5.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4357ad4078768d4c6557b1a0df5a3fb117bd1d54785b4a0763648304b3388560",
                "md5": "3a433bb86f26c789b74fee9eefebdd5f",
                "sha256": "3157be866fc9724d077a8b5bf6c3fc21c38a4147ab664e724622dfe5faff048a"
            },
            "downloads": -1,
            "filename": "zope.cachedescriptors-5.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3a433bb86f26c789b74fee9eefebdd5f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 13250,
            "upload_time": "2023-03-27T08:46:05",
            "upload_time_iso_8601": "2023-03-27T08:46:05.992169Z",
            "url": "https://files.pythonhosted.org/packages/43/57/ad4078768d4c6557b1a0df5a3fb117bd1d54785b4a0763648304b3388560/zope.cachedescriptors-5.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-27 08:46:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "zopefoundation",
    "github_project": "zope.cachedescriptors",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "zope.cachedescriptors"
}
        
Elapsed time: 0.04898s