Skip to content

Commit

Permalink
feat(#31): add thunk function
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Jan 6, 2017
1 parent b1955a7 commit f71129d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ API
- paco.series_
- paco.some_
- paco.throttle_
- paco.thunk_
- paco.timeout_
- paco.TimeoutLimit_
- paco.times_
Expand Down Expand Up @@ -105,6 +106,7 @@ API
.. _paco.series: http://paco.readthedocs.io/en/latest/api.html#paco.searies
.. _paco.some: http://paco.readthedocs.io/en/latest/api.html#paco.some
.. _paco.throttle: http://paco.readthedocs.io/en/latest/api.html#paco.throttle
.. _paco.thunk: http://paco.readthedocs.io/en/latest/api.html#paco.thunk
.. _paco.timeout: http://paco.readthedocs.io/en/latest/api.html#paco.timeout
.. _paco.TimeoutLimit: http://paco.readthedocs.io/en/latest/api.html#paco.TimeoutLimit
.. _paco.times: http://paco.readthedocs.io/en/latest/api.html#paco.times
Expand Down
1 change: 1 addition & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ API documentation
paco.series <http://paco.readthedocs.io/en/latest/api.html#paco.searies>
paco.some <http://paco.readthedocs.io/en/latest/api.html#paco.some>
paco.throttle <http://paco.readthedocs.io/en/latest/api.html#paco.throttle>
paco.thunk <http://paco.readthedocs.io/en/latest/api.html#paco.thunk>
paco.timeout <http://paco.readthedocs.io/en/latest/api.html#paco.timeout>
paco.TimeoutLimit <http://paco.readthedocs.io/en/latest/api.html#paco.TimeoutLimit>
paco.times <http://paco.readthedocs.io/en/latest/api.html#paco.times>
Expand Down
43 changes: 43 additions & 0 deletions paco/thunk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
import asyncio
from .assertions import assert_corofunction


def thunk(coro):
"""
A thunk is a subroutine that is created, often automatically, to assist
a call to another subroutine.
Creates a thunk coroutine which returns coroutine function that accepts no
arguments and when invoked it schedules the wrapper coroutine and
returns the final result.
See Wikipedia page for more information about Thunk subroutines:
https://en.wikipedia.org/wiki/Thunk
Arguments:
value (coroutinefunction): wrapped coroutine function to invoke.
Returns:
coroutinefunction
Usage::
async def task():
return 'foo'
coro = paco.thunk(task)
await coro()
# => 'foo'
await coro()
# => 'foo'
"""
assert_corofunction(coro=coro)

@asyncio.coroutine
def wrapper():
return (yield from coro())

return wrapper
28 changes: 28 additions & 0 deletions tests/thunk_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
import pytest
import asyncio
from paco import thunk
from .helpers import run_in_loop


@asyncio.coroutine
def task():
return 'foo'


def test_thunk():
coro = thunk(task)
assert run_in_loop(coro()) == 'foo'
assert run_in_loop(coro()) == 'foo'
assert run_in_loop(coro()) == 'foo'


def test_thunk_error():
with pytest.raises(TypeError):
run_in_loop(None)

with pytest.raises(TypeError):
run_in_loop(1)

with pytest.raises(TypeError):
run_in_loop('foo')

0 comments on commit f71129d

Please sign in to comment.