Promises with opinions for asyncio and Python 3.6+
This is a revitalization of a promise submodule previously included in another project of mine: aRx. For older revisions of the code check there.
- docs:
Folder containing project's documentation
- src:
Folder containing project's source code
- tests:
Folder containing project's unit tests
- tools:
Folder containing tools for formatting and organizing the code
# pip install lxml asks prop
import asks
import lxml.html
from prop import Promise
from asyncio import get_event_loop
loop = get_event_loop()
p = (
Promise(asks.get("https://en.wikipedia.org/wiki/Main_Page"), loop=loop)
.then(
lambda response: lxml.html.fromstring(response.text).xpath(
'//*[contains(text(), "Did you know...")]/../following-sibling::*/ul//li'
)
)
.catch(
# In case the request fails or lxml can't parse the response, continues with empty list
lambda _: []
)
.then(lambda lis: "\n".join(("Did you know:", *(li.text_content() for li in lis))))
.then(print)
)
loop.run_until_complete(p)
$ pip install prop
All of the source code in this repository is available under the Mozilla Public License 2.0 (MPL). Those that require reproduction of the license text in the distribution are given here.
Copyright (c) 2018-2023 Vítor Vasconcellos. All rights reserved.