{"id":22674779,"url":"https://github.com/bitnom/expectations","last_synced_at":"2025-10-12T01:02:50.446Z","repository":{"id":105070682,"uuid":"336521360","full_name":"bitnom/expectations","owner":"bitnom","description":"Asynchronous expectations (Jobs/tasks) for future events.","archived":false,"fork":false,"pushed_at":"2021-02-06T11:37:28.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-04T13:24:23.618Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bitnom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-06T11:26:21.000Z","updated_at":"2021-02-07T17:55:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"252a5253-b44c-41c0-ba78-1e13cfca5d30","html_url":"https://github.com/bitnom/expectations","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitnom%2Fexpectations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitnom%2Fexpectations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitnom%2Fexpectations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bitnom%2Fexpectations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bitnom","download_url":"https://codeload.github.com/bitnom/expectations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246181844,"owners_count":20736624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-09T17:18:06.633Z","updated_at":"2025-10-12T01:02:45.400Z","avatar_url":"https://github.com/bitnom.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Expectations for Python\n\n**Asynchronous expectations (Jobs/tasks) for future events.**\n\nIf client user sends a message to server, a new eid is generated and sent with the message. If server sends back a message\nwith an eid, the eid is checked against our expectations. If eid matches an expectation, that expectation's callback\nwill run. If server sends a message without an eid, no expectations will be checked.\n\n## Installation\n\nPypi package is present.\n\n`pip install expectations`\n\n## Implementation\n\n**Important steps are marked 1-4.**\n\n```python\nimport asyncio\nimport os\nimport json\nimport aiohttp\nfrom expectations import Expector, eidgen\n\nHOST = os.getenv('HOST', '127.0.0.1')\nPORT = int(os.getenv('PORT', 1339))\n\nURL = f'http://{HOST}:{PORT}/ws'\n\n\ndef my_expected_proc(data):  # Simple function we will use as our expectation callback.\n\tprint('Processed expectation:', data)\n\n\nasync def main():\n\tsession = aiohttp.ClientSession()\n\texpector = Expector()  # 1: Get setup.\n\tasync with session.ws_connect(URL) as ws:\n\t\tasync for msg in ws:\n\t\t\ttry:\n\t\t\t\tserver_message = msg.data.rstrip()\n\t\t\t\tj = json.loads(server_message)\n\n\t\t\t\tif 'eid' in j:\n\t\t\t\t\t# 4: See if incoming eid from server matches any of our expectations.\n\t\t\t\t\texpected = await expector.check_expectations(j['eid'], server_message)\n\t\t\t\t\tif expected:\n\t\t\t\t\t\t# Give expected callback a second to process (For demo purposes; so it runs before prompt)\n\t\t\t\t\t\tawait asyncio.sleep(1000)\n\t\t\t\t\telse:\n\t\t\t\t\t\traise Exception(f'Unexpected message from server: {msg}')\n\t\t\texcept Exception as ex:\n\t\t\t\tprint(ex)\n\t\t\tfinally:\n\t\t\t\tawait prompt_and_send(ws, expector)\n\n\t\t\tif msg.type in (aiohttp.WSMsgType.CLOSED,\n\t\t\t                aiohttp.WSMsgType.ERROR):\n\t\t\t\tbreak\n\n\nasync def prompt_and_send(ws, expector):\n\tuser_msg = input('Type a message to send to the server: ')\n\tif user_msg == 'exit':\n\t\tprint('Exiting!')\n\t\traise SystemExit(0)\n\n\teid = eidgen()  # 2: Create new expectation ID (eid).\n\tawait expector.expect(eid, my_expected_proc)  # 3: Create new expectation using eid and any callback function.\n\n\tawait ws.send_str(json.dumps({\n\t\t'method': user_msg,\n\t\t'eid': eid  # eid is sent with msg (For demo). If we get this eid back later, the expectation callback will run.\n\t}))\n\n\nif __name__ == '__main__':\n\tprint('Type \"exit\" to quit')\n\tloop = asyncio.get_event_loop()\n\tloop.run_until_complete(main())\n\n```\n\n\n**MIT License**","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitnom%2Fexpectations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbitnom%2Fexpectations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbitnom%2Fexpectations/lists"}