diff options
| author | Steve Piercy <web@stevepiercy.com> | 2016-04-24 20:39:37 -0700 |
|---|---|---|
| committer | Steve Piercy <web@stevepiercy.com> | 2016-04-24 20:39:37 -0700 |
| commit | a470ff601182fed93446b8bfe4bc8707a8455949 (patch) | |
| tree | c5a5c55be51872fec6bef5e7934d56d76ec8c0ed /docs | |
| parent | 8f556a5568faa5f2df17d6cafc0900074bb0acfe (diff) | |
| download | pyramid-a470ff601182fed93446b8bfe4bc8707a8455949.tar.gz pyramid-a470ff601182fed93446b8bfe4bc8707a8455949.tar.bz2 pyramid-a470ff601182fed93446b8bfe4bc8707a8455949.zip | |
add sphinx doctests for hooks.rst
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/narr/hooks.rst | 62 |
1 files changed, 58 insertions, 4 deletions
diff --git a/docs/narr/hooks.rst b/docs/narr/hooks.rst index b776f99e8..150eca944 100644 --- a/docs/narr/hooks.rst +++ b/docs/narr/hooks.rst @@ -300,13 +300,38 @@ added as a property and its result is cached per-request by setting ``reify=True``. This way, we eliminate the overhead of running the function multiple times. +.. testsetup:: group1 + + from pyramid.config import Configurator + + + def total(request, *args): + return sum(args) + + + def prop(request): + print("getting the property") + return "the property" + + + + config = Configurator() + config.add_request_method(total) + config.add_request_method(prop, reify=True) + config.commit() + + from pyramid.scripting import prepare + request = prepare(registry=config.registry)["request"] + +.. doctest:: group1 + >>> request.total(1, 2, 3) 6 >>> request.prop getting the property - the property + 'the property' >>> request.prop - the property + 'the property' To not cache the result of ``request.prop``, set ``property=True`` instead of ``reify=True``. @@ -338,13 +363,42 @@ Here is an example of passing a class to ``Configurator.add_request_method``: We attach and cache an object named ``extra`` to the ``request`` object. +.. testsetup:: group2 + + from pyramid.config import Configurator + from pyramid.decorator import reify + + class ExtraStuff(object): + + def __init__(self, request): + self.request = request + + def total(self, *args): + return sum(args) + + # use @property if you don't want to cache the result + @reify + def prop(self): + print("getting the property") + return "the property" + + config = Configurator() + config.add_request_method(ExtraStuff, 'extra', reify=True) + config.commit() + + from pyramid.scripting import prepare + request = prepare(registry=config.registry)["request"] + +.. doctest:: group2 + >>> request.extra.total(1, 2, 3) 6 >>> request.extra.prop getting the property - the property + 'the property' >>> request.extra.prop - the property + 'the property' + .. index:: single: response factory |
