Pleasant python promises
pip install pleasant-promises
Python promises are a thing, really! But they get ugly quickly. This package lets you handle promises using generator syntax.
Here's what using 3 sequential promises looks like without this package:
def resolve_greatgrandparent(person,info):
parent_prom = person_loader.load(person.parent_id)
def handle_parent(parent):
grandparent_prom = person_loader.load(parent.parent_id)
def handle_grandparent(grandparent):
return person_loader.load(grandparent.parent_id)
return grandparent_prom.then(handle_grandparent)
return parent_prom.then(handle_parent)
And now with this package,
from pleasant_promises import genfunc_to_prom
@genfunc_to_prom
def resolve_greatgrandparent_name(person,info):
parent = yield person_loader.load(person.parent_id)
grandparent = yield person_loader.load(parent.parent_id)
great_grandparent = yield person_loader.load(grandparent.parent_id)
return great_grandparent
Usage with Graphql/Graphene
If you're using promises, you're probably also using graphene or at least graphql. There are a few other helpers in here to help with your graphql schema,
Dataloaders
This package has a useful subclass wrapper of the promise package's Dataloader. You can overload its batch_load method with a generator function. This is useful if you want call another dataloader within batch_load. This class will also convert non-promise values to promises, which is required by the API we're wrapping.
from pleasant_promises.dataloader import Dataloader
class GrandparentLoader(DataLoader):
def batch_load(self,keys):
parents = yield person_loader.load_many(keys)
grandparents = yield person_loader.load_many(parent.parent_id for parent in parents)
# ...
You'll still have to decorate other dataloader methods with @genfunc_to_prom, though:
from pleasant_promises import genfunc_to_prom
class GrandparentLoader(DataLoader):
@genfunc_to_prom
def get_grandparent_for_single_key(self,key):
parent = yield person_loader.load(key)
grandparent = yield person_loader.load(parent.parent_id)
return grandparent
def batch_load(self,keys):
return Promise.all([self.get_grandparent_for_single_key(key) for key in keys])
Graphene middleware
Without the middleware, you'll have to decorate all your resolvers that want to use the generator syntax.
from pleasant_promises import genfunc_to_prom
class MyPersonType(graphene.ObjectType):
# ...
@genfunc_to_prom
def resolve_grandparent(self,info):
parent = yield person_loader.load(person.parent_id)
grandparent = yield person_loader.load(parent.parent_id)
return grandparent
To avoid repeating this decorator on all your resolvers, use our pleasant middleware
from pleasant_promises.graphene import promised_generator_middleware
my_graphene_schema.execute('THE QUERY', middleware=[promised_generator_middleware])
Metadata
Release files for pleasant-promises 1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pleasant_promises-1.1.tar.gz | 3.5 kB | Without provenance | Details for pleasant_promises-1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pleasant_promises-1.1-py3-none-any.whl | Python 3 | none | any | Details for pleasant_promises-1.1-py3-none-any.whl |
Total release size: 7.7 kB
Release files / pleasant_promises-1.1.tar.gz
| Download URL | pleasant_promises-1.1.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2661f09a9a8dcf65a5cbf5254e56a1e77e00e9471aa238773908d7366c4b0f8d
|
|
BLAKE2b-256 checksum How to use checksums |
8f8aca436a3caf01b009eceaf67d3da56ac28700bb92370e186465a82f7dade5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / pleasant_promises-1.1-py3-none-any.whl
| Download URL | pleasant_promises-1.1-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bf474cf7674a9b0fbf6a1096a639116ff2e3d95118d94c22cef43392c5438731
|
|
BLAKE2b-256 checksum How to use checksums |
f110089414d5f7b4f63f7cbaef6ddbbb1dcb82deb53c861bf0f25aad8ede300b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Security for pleasant-promises 1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.