Skip to content

Commit d3ac5f8

Browse files
committed
add a perform_delay_with_sleep performer, and do some minor doc tweaks
1 parent 71fb692 commit d3ac5f8

File tree

4 files changed

+35
-18
lines changed

4 files changed

+35
-18
lines changed

effect/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@
1515
sync_perform,
1616
sync_performer)
1717
from ._intents import (
18-
Delay, ParallelEffects, parallel, parallel_all_errors,
19-
Constant, Error, FirstError, Func,
18+
Delay, perform_delay_with_sleep,
19+
ParallelEffects, parallel, parallel_all_errors, FirstError,
20+
Constant, Error, Func,
2021
base_dispatcher)
2122
from ._dispatcher import ComposedDispatcher, TypeDispatcher
2223

2324

2425
__all__ = [
25-
"Effect", "perform", "NoPerformerFoundError",
26-
"NotSynchronousError", "sync_perform", "sync_performer",
27-
"Delay", "ParallelEffects", "parallel", "parallel_all_errors",
28-
"Constant", "Error", "FirstError", "Func",
26+
# Order here affects the order that these things show up in the API docs.
27+
"Effect", "sync_perform", "sync_performer",
2928
"base_dispatcher",
3029
"TypeDispatcher", "ComposedDispatcher",
30+
"Delay", "perform_delay_with_sleep",
31+
"ParallelEffects", "parallel", "parallel_all_errors",
32+
"Constant", "Error", "Func",
3133
"catch", "raise_",
34+
"NoPerformerFoundError", "NotSynchronousError", "perform",
35+
"FirstError",
3236
]

effect/_base.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ class NoPerformerFoundError(Exception):
8686

8787
def perform(dispatcher, effect):
8888
"""
89-
Perform an effect and invoke callbacks bound to it.
89+
Perform an effect and invoke callbacks bound to it. You probably don't want
90+
to use this. Instead, use :func:`sync_perform` (or, if you're using
91+
Twisted, see the `txeffect`_ library).
9092
9193
The dispatcher will be called with the intent, and is expected to return a
9294
performer (another callable). See :obj:`TypeDispatcher` and
@@ -110,10 +112,6 @@ def perform(dispatcher, effect):
110112
callback. In the case of exceptions, the next error-callback will be called
111113
with a ``sys.exc_info()``-style tuple.
112114
113-
Note that this function does _not_ return the final result of the effect.
114-
You may instead want to use :func:`.sync_perform` or
115-
:func:`effect.twisted.perform`.
116-
117115
:returns: None
118116
119117
.. [#dispatcher] The dispatcher is passed because some performers need to

effect/_intents.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
66
The :obj:`base_dispatcher` object in this module is a dispatcher which provides
77
standard performers for intents which really only have one reasonable way to be
8-
performed, sunch as :class:`Func`, :class:`Error`, and
9-
:class:`Constant`.
10-
11-
Other intents, such as :class:`ParallelEffects` and :class:`Delay`, need to
12-
have a performer specified elsewhere, since the performers are reliant on
13-
choices made by the application author. :module:`effect.twisted` provides a
14-
Twisted-specific dispatcher for these.
8+
performed, sunch as :class:`Func`, :class:`Error`, and :class:`Constant`.
159
"""
1610

1711

1812
from __future__ import print_function, absolute_import
1913

14+
import time
15+
2016
import attr
2117

2218
from ._base import Effect
@@ -118,6 +114,12 @@ class Delay(object):
118114
delay = attr.ib()
119115

120116

117+
@sync_performer
118+
def perform_delay_with_sleep(dispatcher, intent):
119+
"""Perform a :obj:`Delay` by calling ``time.sleep``."""
120+
time.sleep(intent.delay)
121+
122+
121123
@attr.s
122124
class Constant(object):
123125
"""

effect/test_intents.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from ._intents import (
1515
base_dispatcher,
1616
Constant, perform_constant,
17+
Delay, perform_delay_with_sleep,
1718
Error, perform_error,
1819
Func, perform_func,
1920
FirstError,
@@ -32,12 +33,14 @@ def test_perform_constant():
3233
Effect(intent))
3334
assert result == "foo"
3435

36+
3537
def test_perform_error():
3638
"""perform_error raises the exception of an Error."""
3739
intent = Error(ValueError("foo"))
3840
with raises(ValueError):
3941
sync_perform(TypeDispatcher({Error: perform_error}), Effect(intent))
4042

43+
4144
def test_perform_func():
4245
"""perform_func calls the function given in a Func."""
4346
intent = Func(lambda: "foo")
@@ -53,13 +56,23 @@ def test_perform_func_args_kwargs():
5356
result = sync_perform(TypeDispatcher({Func: perform_func}), Effect(intent))
5457
assert result == ((1, 2), {'key': 3})
5558

59+
5660
def test_first_error_str():
5761
"""FirstErrors have a pleasing format."""
5862
fe = FirstError(exc_info=(ValueError, ValueError('foo'), None),
5963
index=150)
6064
assert str(fe) == '(index=150) ValueError: foo'
6165

6266

67+
def test_perform_delay_with_sleep(monkeypatch):
68+
""":func:`perform_delay_with_sleep` calls time.sleep."""
69+
calls = []
70+
monkeypatch.setattr('time.sleep', calls.append)
71+
disp = TypeDispatcher({Delay: perform_delay_with_sleep})
72+
sync_perform(disp, Effect(Delay(3.7)))
73+
assert calls == [3.7]
74+
75+
6376
class ParallelAllErrorsTests(TestCase):
6477
"""Tests for :func:`parallel_all_errors`."""
6578

0 commit comments

Comments
 (0)