(アノテーション)

Description

How to use annotation design pattern to store arbitary values on Python objects (Plone site, HTTP request) for storage and caching purposes.

Introduction

Annotations is conflict-free way to stick attributes on arbitary Python objects.

Plone uses annotations for

  • Storing field data in Archetypes (Annotation storage)
  • Caching values on HTTP request object (plone.memoize cache decorators)
  • Storing settings information in portal object (various add-on products)

See zope.annotation package <http://pypi.python.org/pypi/zope.annotation/3.4.1>.

HTTP リクエストの例

Store cached values on HTTP request during the life cycle of one request processing. This allows you to cache computed values if the computation function is called from the different, unrelated, code paths.

from zope.annotation.interfaces import IAnnotations

    # Non-conflicting key
    KEY = "mypackage.something"

    annotations = IAnnotations(request)

    value = annotations.get(KEY, None)
    if value is None:
            # Compute value and store it on request object for further look-ups
            value = annotations[KEY] = something()

目次

前のトピックへ

(メール送信)

次のトピックへ

(id のノーマライズ)

このページ