Skip to content

Commit 132710d

Browse files
committed
Merge pull request #42 from radix/eq-dispatcher-list
make EQDispatcher and EQFDispatcher use lists of two-tuples instead of a dict
2 parents a950a44 + 7943eda commit 132710d

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

effect/test_testing.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ class EQDispatcherTests(TestCase):
255255

256256
def test_no_intent(self):
257257
"""When the dispatcher can't match the intent, it returns None."""
258-
d = EQDispatcher({})
258+
d = EQDispatcher([])
259259
self.assertIs(d('foo'), None)
260260

261261
def test_perform(self):
262262
"""When an intent matches, performing it returns the canned result."""
263-
d = EQDispatcher({'hello': 'there'})
263+
d = EQDispatcher([('hello', 'there')])
264264
self.assertEqual(sync_perform(d, Effect('hello')), 'there')
265265

266266

@@ -269,10 +269,10 @@ class EQFDispatcherTests(TestCase):
269269

270270
def test_no_intent(self):
271271
"""When the dispatcher can't match the intent, it returns None."""
272-
d = EQFDispatcher({})
272+
d = EQFDispatcher([])
273273
self.assertIs(d('foo'), None)
274274

275275
def test_perform(self):
276276
"""When an intent matches, performing it returns the canned result."""
277-
d = EQFDispatcher({'hello': lambda i: (i, 'there')})
277+
d = EQFDispatcher([('hello', lambda i: (i, 'there'))])
278278
self.assertEqual(sync_perform(d, Effect('hello')), ('hello', 'there'))

effect/testing.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,29 @@ class EQDispatcher(object):
177177
matched against the intents being performed with a simple equality check
178178
(not a type check!).
179179
180+
The mapping must be provided as a sequence of two-tuples. We don't use a
181+
dict because we don't want to require that the intents be hashable (in
182+
practice a lot of them aren't, and it's a pain to require it). If you want
183+
to construct your mapping as a dict, you can, just pass in the result of
184+
``d.items()``.
185+
180186
e.g.::
181187
182-
>>> sync_perform(EQDispatcher({MyIntent(1, 2): 'the-result'}),
188+
>>> sync_perform(EQDispatcher([(MyIntent(1, 2), 'the-result')]),
183189
... Effect(MyIntent(1, 2)))
184190
'the-result'
185191
186192
assuming MyIntent supports ``__eq__`` by value.
187193
"""
188194
def __init__(self, mapping):
189195
"""
190-
:param mapping: Mapping of intents to results.
196+
:param list mapping: A sequence of tuples of (intent, result).
191197
"""
192198
self.mapping = mapping
193199

194200
def __call__(self, intent):
195201
# Avoid hashing, because a lot of intents aren't hashable.
196-
for k, v in self.mapping.items():
202+
for k, v in self.mapping:
197203
if k == intent:
198204
return sync_performer(lambda d, i: v)
199205

@@ -210,24 +216,30 @@ class EQFDispatcher(object):
210216
(not a type check!). The functions in the mapping will be passed only the
211217
intent and are expected to return the result or raise an exception.
212218
219+
The mapping must be provided as a sequence of two-tuples. We don't use a
220+
dict because we don't want to require that the intents be hashable (in
221+
practice a lot of them aren't, and it's a pain to require it). If you want
222+
to construct your mapping as a dict, you can, just pass in the result of
223+
``d.items()``.
224+
213225
e.g.::
214226
215227
>>> sync_perform(
216-
... EQFDispatcher({
217-
... MyIntent(1, 2): lambda i: 'the-result'}),
228+
... EQFDispatcher([(
229+
... MyIntent(1, 2), lambda i: 'the-result')]),
218230
... Effect(MyIntent(1, 2)))
219231
'the-result'
220232
221233
assuming MyIntent supports ``__eq__`` by value.
222234
"""
223235
def __init__(self, mapping):
224236
"""
225-
:param mapping: Mapping of intents to results.
237+
:param list mapping: A sequence of two-tuples of (intent, function).
226238
"""
227239
self.mapping = mapping
228240

229241
def __call__(self, intent):
230242
# Avoid hashing, because a lot of intents aren't hashable.
231-
for k, v in self.mapping.items():
243+
for k, v in self.mapping:
232244
if k == intent:
233245
return sync_performer(lambda d, i: v(i))

0 commit comments

Comments
 (0)