prompy package

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:
callback_handler(obj)[source]

Override to handle the return value of callbacks.

Parameters:obj (Any) – The return value of a callback
Returns:
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:
error
Return type:Exception
exec()[source]

Execute the starter method.

Returns:
id
Return type:UUID
reject(error)[source]

Reject the promise.

Parameters:error (Exception) –
Returns:
resolve(result)[source]

Resolve the promise, called by executor.

Parameters:result (~PromiseReturnType) –
Returns:
result
Return type:Union[Tuple[~PromiseReturnType], ~PromiseReturnType]
state
Return type:PromiseState
then(func)[source]

Add a callback to resolve

Parameters:func (Callable[[~PromiseReturnType], None]) – callback to resolve
Returns:
class prompy.promise.PromiseState[source]

Bases: enum.Enum

An enumeration.

fulfilled = 2
pending = 1
rejected = 3

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

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

add_promise(promise)[source]

Add a promise to the container.

Parameters:promise (Promise[~PromiseReturnType]) –
Returns:
add_promises(*promises)

Add all the promises.

Parameters:promises (Promise[~PromiseReturnType]) – promises to add
Returns:
start()[source]
stop()[source]
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.
callback_handler(obj)[source]

Override to handle the return value of callbacks.

Parameters:obj (Any) – The return value of a callback
Returns:
catch(func)

Add a callback to rejection

Parameters:func (Callable[[Exception], None]) –
Returns:
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:
id
Return type:UUID
reject(error)[source]

Reject the promise.

Parameters:error (Exception) –
Returns:
resolve(result)[source]

Resolve the promise, called by executor.

Parameters:result (Any) –
Returns:
result
Return type:Union[Tuple[~PromiseReturnType], ~PromiseReturnType]
state
Return type:PromiseState
then(func)

Add a callback to resolve

Parameters:func (Callable[[~PromiseReturnType], None]) – callback to resolve
Returns:
static wrap(func)[source]

prompy.container module

class prompy.container.BasePromiseContainer[source]

Bases: object

Interface for a promise container.

add_promise(promise)[source]

Add a promise to the container.

Parameters:promise (Promise[~PromiseReturnType]) –
Returns:
add_promises(*promises)[source]

Add all the promises.

Parameters:promises (Promise[~PromiseReturnType]) – promises to add
Returns:
class prompy.container.BasePromiseRunner[source]

Bases: prompy.container.BasePromiseContainer

A container that need to start and stop.

add_promise(promise)[source]

Add a promise to the container.

Parameters:promise (Promise[~PromiseReturnType]) –
Returns:
start()[source]
stop()[source]
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.

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

add_promise(promise)[source]

Add a promise to the container.

Parameters:promise (Promise[~PromiseReturnType]) –
Returns:
prompy.container.container_wrap(func)[source]
Return type:Callable[…, Promise[~PromiseReturnType]]

prompy.errors module

exception prompy.errors.PromiseError[source]

Bases: Exception

Base promise error

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]
prompy.promtools.piter(func, iterable, prom_type=prompy.promise.Promise, **kwargs)[source]

Applies func to iterable in a promise, like a map and foreach (starter->then).

Return type:Promise[~PromiseReturnType]
prompy.promtools.promise_wrap(func, prom_type=prompy.promise.Promise, **kw)[source]

Wraps a function return in a promise resolve.

Return type:Callable[…, Promise[~PromiseReturnType]]