Expectations for Python
Asynchronous expectations (Jobs/tasks) for future events.
If client user sends a message to server, a new eid is generated and sent with the message. If server sends back a message with an eid, the eid is checked against our expectations. If eid matches an expectation, that expectation's callback will run. If server sends a message without an eid, no expectations will be checked.
Installation
Pypi package is present.
pip install expectations
Implementation
Important steps are marked 1-4.
import asyncio
import os
import json
import aiohttp
from expectations import Expector, eidgen
HOST = os.getenv('HOST', '127.0.0.1')
PORT = int(os.getenv('PORT', 1339))
URL = f'http://{HOST}:{PORT}/ws'
def my_expected_proc(data): # Simple function we will use as our expectation callback.
print('Processed expectation:', data)
async def main():
session = aiohttp.ClientSession()
expector = Expector() # 1: Get setup.
async with session.ws_connect(URL) as ws:
async for msg in ws:
try:
server_message = msg.data.rstrip()
j = json.loads(server_message)
if 'eid' in j:
# 4: See if incoming eid from server matches any of our expectations.
expected = await expector.check_expectations(j['eid'], server_message)
if expected:
# Give expected callback a second to process (For demo purposes; so it runs before prompt)
await asyncio.sleep(1000)
else:
raise Exception(f'Unexpected message from server: {msg}')
except Exception as ex:
print(ex)
finally:
await prompt_and_send(ws, expector)
if msg.type in (aiohttp.WSMsgType.CLOSED,
aiohttp.WSMsgType.ERROR):
break
async def prompt_and_send(ws, expector):
user_msg = input('Type a message to send to the server: ')
if user_msg == 'exit':
print('Exiting!')
raise SystemExit(0)
eid = eidgen() # 2: Create new expectation ID (eid).
await expector.expect(eid, my_expected_proc) # 3: Create new expectation using eid and any callback function.
await ws.send_str(json.dumps({
'method': user_msg,
'eid': eid # eid is sent with msg (For demo). If we get this eid back later, the expectation callback will run.
}))
if __name__ == '__main__':
print('Type "exit" to quit')
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
MIT License
Metadata
Release files for expectations 1.0.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 |
|---|---|---|---|---|
| expectations-1.0.1.tar.gz | 4.1 kB | Without provenance | Details for expectations-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| expectations-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for expectations-1.0.1-py3-none-any.whl |
Total release size: 8.0 kB
Release files / expectations-1.0.1.tar.gz
| Download URL | expectations-1.0.1.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d97c6274c1c32df52666c93c296658bc550b9a9ab5af0e23b35a2ebac78dc02
|
|
BLAKE2b-256 checksum How to use checksums |
02f3eaeb2f9e3e1910fcf9ea3864b46058babd00f38bf6da5d63e782b7788914
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
|
Release files / expectations-1.0.1-py3-none-any.whl
| Download URL | expectations-1.0.1-py3-none-any.whl |
|---|---|
| Size | 3.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
40e149ff1404392b57a1ebc78354eb7254fed5774ee5bcbf968c30293e6dffa3
|
|
BLAKE2b-256 checksum How to use checksums |
e0bc7daa6d8a4e9169ee70206a3288f8fbd54719643a538efa24e0e776b2df89
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.9.0
|
Security for expectations 1.0.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.