• Stars
    star
    352
  • Rank 120,622 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ultra-performant Promise implementation in Python

Promise

This is a implementation of Promises in Python. It is a super set of Promises/A+ designed to have readable, performant code and to provide just the extensions that are absolutely necessary for using promises in Python.

Its fully compatible with the Promises/A+ spec

travis pypi coveralls

Installation

$ pip install promise

Usage

The example below shows how you can load the promise library. It then demonstrates creating a promise from scratch. You simply call Promise(fn). There is a complete specification for what is returned by this method in Promises/A+.

from promise import Promise

promise = Promise(
    lambda resolve, reject: resolve('RESOLVED!')
)

API

Before all examples, you will need:

from promise import Promise

Promise(resolver)

This creates and returns a new promise. resolver must be a function. The resolver function is passed two arguments:

  1. resolve should be called with a single argument. If it is called with a non-promise value then the promise is fulfilled with that value. If it is called with a promise (A) then the returned promise takes on the state of that new promise (A).
  2. reject should be called with a single argument. The returned promise will be rejected with that argument.

Class Methods

These methods are invoked by calling Promise.methodName.

Promise.resolve(value)

Converts values and foreign promises into Promises/A+ promises. If you pass it a value then it returns a Promise for that value. If you pass it something that is close to a promise (such as a jQuery attempt at a promise) it returns a Promise that takes on the state of value (rejected or fulfilled).

Promise.reject(value)

Returns a rejected promise with the given value.

Promise.all(list)

Returns a promise for a list. If it is called with a single argument then this returns a promise for a copy of that list with any promises replaced by their fulfilled values. e.g.

p = Promise.all([Promise.resolve('a'), 'b', Promise.resolve('c')]) \
       .then(lambda res: res == ['a', 'b', 'c'])

assert p.get() is True

Promise.cast(obj)

This function wraps the obj ect as a Promise if possible. Python Futures are supported, with a callback to promise.done when resolved. Has the same effects as Promise.resolve(obj).

Promise.for_dict(d)

A special function that takes a dictionary of promises and turns them into a promise for a dictionary of values. In other words, this turns a dictionary of promises for values into a promise for a dictionary of values.

Promise.is_thenable(obj)

This function checks if the obj is a Promise, or could be casted.

Promise.promisify(func)

This function wraps the result of calling func in a Promise instance.

Instance Methods

These methods are invoked on a promise instance by calling myPromise.methodName

promise.then(did_fulfill, did_reject)

This method follows the Promises/A+ spec. It explains things very clearly so I recommend you read it.

Either did_fulfill or did_reject will be called and they will not be called more than once. They will be passed a single argument and will always be called asynchronously (in the next turn of the event loop).

If the promise is fulfilled then did_fulfill is called. If the promise is rejected then did_reject is called.

The call to .then also returns a promise. If the handler that is called returns a promise, the promise returned by .then takes on the state of that returned promise. If the handler that is called returns a value that is not a promise, the promise returned by .then will be fulfilled with that value. If the handler that is called throws an exception then the promise returned by .then is rejected with that exception.

promise.catch(did_reject)

Sugar for promise.then(None, did_reject), to mirror catch in synchronous code.

promise.done(did_fulfill, did_reject)

The same semantics as .then except that it does not return a promise and any exceptions are re-thrown so that they can be logged (crashing the application in non-browser environments)

Contributing

After cloning this repo, ensure dependencies are installed by running:

pip install -e ".[test]"

After developing, the full test suite can be evaluated by running:

py.test tests --cov=promise --benchmark-skip # Use -v -s for verbose mode

You can also run the benchmarks with:

py.test tests --benchmark-only

Static type checking

Python type annotations are very useful for making sure we use the libary the way is intended.

You can run mypy static type checker:

pip install mypy
mypy promise  --ignore-missing-imports

Or pyre:

pip install pyre-check
pyre --source-directory promise check

Notes

This package is heavily insipired in aplus.

License

MIT License

More Repositories

1

gdom

DOM Traversing and Scraping using GraphQL
Python
1,240
star
2

pyjade

Jade template system for Django, Jinja2 and Mako
Python
696
star
3

Flask-SuperAdmin

The best admin interface framework for Flask. With scaffolding for MongoEngine, Django and SQLAlchemy.
Python
635
star
4

validate_email

Validate_email verify if an email address is valid and really exists
Python
544
star
5

snapshottest

Snapshot Testing utils for Python πŸ“Έ
Python
529
star
6

react-native-refresher

A pull to refresh ListView for React Native completely written in js.
JavaScript
453
star
7

aiodataloader

Asyncio DataLoader for Python3
Python
270
star
8

interpy

Ruby like string interpolation #{} in Python
Python
254
star
9

fastdiff

A fast native implementation of diff algorithm with a pure Python fallback
Python
37
star
10

jsjinja

Jinja2 to Javascript template converter. Now you can render your Jinja2 templates on the client side! ;)
Python
33
star
11

Djinja

(Deprecated) A package that makes possible the integration of Jinja2 in Django, in a clean way.
Python
20
star
12

Webclient-Kohana

Kohana Utilities
PHP
8
star
13

syrusakbary.com

My personal webpage
CSS
6
star
14

django-processorfield

A powerful filefield for Django with multiple processor outputs
Python
5
star
15

PlagiarismDetector-PHP

A Plagiarism Detector system made in PHP
PHP
4
star
16

graphqlprimer

Graphene Twitter Streams Demo for the presentation at Primer.ai
Python
3
star
17

eXercita

Python
3
star
18

flask-beaker

Beaker session interface for Flask.
Python
3
star
19

wasmer-vs-docker

Wasmer vs Docker
Makefile
3
star
20

netlify-wasmer

Netlify wasmer example
2
star
21

Animotion-Java

An animation framework for java. Supports transitions and much more!
2
star
22

assetsy

A vitaminated way to minify, combine and optimize your web resources
Python
1
star
23

gulp-jade-mixins

Jade mixins js-generator plugin for Gulp
JavaScript
1
star
24

PlagiarismDetector_Block-Moodle

The PlagiarismDetector Block for Moodle
1
star
25

RobotExploration-Java

A Robot Interface
Java
1
star
26

wasi-tests

Shell
1
star
27

wasmer_rust_playground

Playground for the Wasmer Rust Integration
Rust
1
star
28

django-vagrant-base

Automatically deploy Django site locally with Vagrant
Ruby
1
star
29

PlagiarismDetector_Mod-Moodle

The PlagiarismDetector platform for Moodle
PHP
1
star
30

FuzzyUppaal-Java

An implementation of the Uppaal modeling system for the support of fuzzy logic.
Java
1
star
31

tuentify

Te avisa al instante de tus notificaciones pendientes en Tuenti y agiliza la carga de la pΓ‘gina.
JavaScript
1
star