prompy package¶
Subpackages¶
prompy.promise module¶
Promise for python
-
class
prompy.promise.
Promise
(starter, then=None, catch=None, complete=None, raise_again=False, start_now=False, results_buffer_size=100)[source]¶ Bases:
typing.Generic
Promise interface Based on js Promises.
Basic usage:
p = Promise(lambda resolve, reject: resolve(‘Hello’)).then(print)
-
__init__
(starter, then=None, catch=None, complete=None, raise_again=False, start_now=False, results_buffer_size=100)[source]¶ Promise takes at least a starter method with params to this promise resolve and reject. Does not call exec by default but with start_now the execution will be synchronous.
Parameters: - starter (
Callable
[[Callable
,Callable
],None
]) – otherwise known as executor. - then (
Optional
[Callable
[[~PromiseReturnType],None
]]) – initial resolve callback - catch (
Optional
[Callable
[[Exception
],None
]]) – initial catch callback - complete (
Optional
[Callable
[[Union
[List
[~PromiseReturnType], ~PromiseReturnType],Exception
],None
]]) – initial complete callback - raise_again (
bool
) – raise the rejection error again. - start_now (
bool
) – - results_buffer_size (
int
) – number of results to keep in the buffer.
- starter (
-
callback_handler
(obj)[source]¶ Override to handle the return value of callbacks.
Parameters: obj ( Any
) – The return value of a callbackReturns:
-
catch
(func)[source]¶ Add a callback to rejection
Parameters: func ( Callable
[[Exception
],None
]) –Returns:
-
complete
(func)[source]¶ Add a callback to finally block
Parameters: func ( Callable
[[Union
[List
[~PromiseReturnType], ~PromiseReturnType],Exception
],None
]) –Returns:
-
resolve
(result)[source]¶ Resolve the promise, called by executor.
Parameters: result (~PromiseReturnType) – Returns:
-
state
¶ Return type: PromiseState
-
prompy.awaitable module¶
Promise you can await.
Example: |
---|
import asyncio
from prompy.awaitable import AwaitablePromise
from prompy.networkio.async_call import call
async def call_starter(resolve, _):
google = await call('http://www.google.com')
resolve(google)
p = AwaitablePromise(call_starter)
@p.then
def then(result):
print(result)
asyncio.get_event_loop().stop()
@p.catch
def catch(err):
asyncio.get_event_loop().stop()
raise err
asyncio.get_event_loop().run_forever()
-
class
prompy.awaitable.
AsyncPromiseRunner
[source]¶ Bases:
prompy.container.BasePromiseRunner
Run the loop forever
-
add_promise
(promise)[source]¶ Add a promise to the container.
Parameters: promise ( Promise
[~PromiseReturnType]) –Returns:
-
-
class
prompy.awaitable.
AwaitablePromise
(starter, then=None, catch=None, complete=None, loop=None)[source]¶ Bases:
prompy.promise.Promise
asyncio compatible promise
Await it to get the result. Need a running loop to actually start the executor.
-
__init__
(starter, then=None, catch=None, complete=None, loop=None)[source]¶ Promise takes at least a starter method with params to this promise resolve and reject. Does not call exec by default but with start_now the execution will be synchronous.
Parameters: - starter (
Callable
[[Callable
,Callable
],None
]) – otherwise known as executor. - then (
Optional
[Callable
[[~PromiseReturnType],None
]]) – initial resolve callback - catch (
Optional
[Callable
[[Exception
],None
]]) – initial catch callback - complete (
Optional
[Callable
[[Union
[List
[~PromiseReturnType], ~PromiseReturnType],Exception
],None
]]) – initial complete callback - raise_again – raise the rejection error again.
- start_now –
- results_buffer_size – number of results to keep in the buffer.
- starter (
-
callback_handler
(obj)[source]¶ Override to handle the return value of callbacks.
Parameters: obj ( Any
) – The return value of a callbackReturns:
-
complete
(func)¶ Add a callback to finally block
Parameters: func ( Callable
[[Union
[List
[~PromiseReturnType], ~PromiseReturnType],Exception
],None
]) –Returns:
-
error
¶ Raise: invalid state if the promise was not completed. Returns: the exception or the handled error
-
exec
()¶ Execute the starter method.
Returns:
-
resolve
(result)[source]¶ Resolve the promise, called by executor.
Parameters: result ( Any
) –Returns:
-
state
¶ Return type: PromiseState
-
prompy.container module¶
-
class
prompy.container.
BasePromiseContainer
[source]¶ Bases:
object
Interface for a promise container.
-
class
prompy.container.
BasePromiseRunner
[source]¶ Bases:
prompy.container.BasePromiseContainer
A container that need to start and stop.
-
class
prompy.container.
PromiseContainer
[source]¶ Bases:
prompy.container.BasePromiseContainer
,collections.abc.Container
Basic promise container.
Keeps the promises in a dict with the promise id as key.
prompy.errors module¶
-
exception
prompy.errors.
PromiseRejectionError
[source]¶ Bases:
prompy.errors.PromiseError
Raised when a promise is called with raise_again option
-
exception
prompy.errors.
UnhandledPromiseError
[source]¶ Bases:
prompy.errors.PromiseError
Unhandled promise rejection error
-
exception
prompy.errors.
UrlCallError
[source]¶ Bases:
prompy.errors.PromiseError
Web call error
prompy.promtools module¶
Methods for working with promises.
-
prompy.promtools.
later
(func, delay, wait_func=<built-in function sleep>, prom_type=prompy.promise.Promise, **kwargs)[source]¶ Bad, do not use.
Return type: Callable
[…,Promise
[~PromiseReturnType]]
-
prompy.promtools.
pall
(*promises, prom_type=prompy.promise.Promise, **kwargs)[source]¶ Wrap all the promises in a single one that resolve when all promises are done.
Return type: Promise
[~PromiseReturnType]