{ "info": { "author": "Daniel 'Vector' Kerr", "author_email": "admin@vector.id.au", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3 :: Only", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "# awebus\n\n`awebus` (Asynchronous, Weak Event BUS) is an event system for Python which\nprovides (optional) asynchronous execution of handlers, and uses weak\nreferences to store event handlers.\n\n\n\n## Requirements\n\n* Python >= 3.4.0\n\n\n\n## Getting Started\n\n1. Install awebus with `pip`:\n ```sh\n pip install awebus\n ```\n\n1. Start using awebus!\n\n\n\n## Examples\n\n### Basic Usage\n\n```python\nimport awebus\n\n# Create a bus\nbus = awebus.Bus()\n\n# Create a handler\ndef handleMyEvent():\n print( \"Handling My Event\" )\n return \"Event Handled\"\n\n# Register an event handler\nbus.on( 'my-event', handleMyEvent )\n\n# Invoke `my-event`\nresults = bus.emit( 'my-event' )\n\nprint( results )\n# >> [ \"Event Handled\" ]\n\n# Remove the event handler\nbus.off( 'my-event', handleMyEvent )\n```\n\n\n### Event Arguments\n\nPass arguments to event handlers as an argument list or keyword arguments.\n\n```python\nimport awebus\n\n# Create a bus\nbus = awebus.Bus()\n\n# Create a handler\ndef handleArgsEvent( foo, bar, *args, **kwargs ):\n print( \"foo is\", str( foo ) )\n print( \"bar is\", str( bar ) )\n print( \"*args is\", str( args ) )\n print( \"**kwargs is\", str( kwargs ) )\n\n# Register an event handler\nbus.on( 'args-event', handleArgsEvent )\n\n# Invoke `args-event` with positional, listed, and keyword arguments\nbus.emit( 'args-event', 3, 7, \"hello\", \"world\", age = 30, name = \"Vector\" )\n# >> foo is 3\n# >> bar is 7\n# >> *args is ( \"hello\", \"world\" )\n# >> **kwargs is { \"age\": 30, \"name\": \"Vector\" }\n```\n\n### Synchronous Execution\n\nInvoke a synchronous event by using the `bus.emit()` method.\nIf `emit()` encounters an asynchronous handler, it will use an asyncio\nevent loop to run the handler to completion synchronously.\n\n```python\nimport awebus\nimport asyncio\n\n# Create a bus\nbus = awebus.Bus()\n\n# Create a regular handler\ndef synchronousHandler():\n print( \"Synchronous Handler\" )\n return \"sync\"\n\n# Create an asynchronous handler\nasync def asynchronousHandler():\n await asyncio.sleep( 0.2 )\n return \"async\"\n\n# Register handlers\nbus.on( 'async-event', synchronousHandler, asynchronousHandler )\n\n# Invoke the event synchronously.\nresults = bus.emit( 'async-event' )\n\nprint( results )\n# >> [ \"sync\", \"async\" ]\n```\n\n\n### Asynchronous Execution\n\nInvoke an asynchronous event by using the `bus.emitAsync()` method.\n\nIf `emitAsync()` encounters a synchronous handler, it will wrap the handler\nwith the asyncio coroutine decorator. `emitAsync()` returns a list of\nawaitables.\n\n\n```python\nimport awebus\nimport asyncio\n\n# Create a bus\nbus = awebus.Bus()\n\n# Create a regular handler\ndef synchronousHandler():\n print( \"Synchronous Handler\" )\n return \"sync\"\n\n# Create an asynchronous handler\nasync def asynchronousHandler():\n await asyncio.sleep( 0.2 )\n return \"async\"\n\n# Register handlers\nbus.on( 'async-event', synchronousHandler, asynchronousHandler )\n\n# (Inside an async method somewhere...) invoke the event asynchronously.\nresults = await bus.emitAsync( 'async-event' )\n\nprint( results )\n# >> [ \"sync\", \"async\" ]\n```\n\n\n### Weak References\n\nRegistered event handlers are stored internally using Python's weak reference\nmethod (`weakref.ref()`). This allows garbage collection of methods that would\notherwise be cleaned up if they did not have a handler registered in the bus.\n\nIf an event is invoked and a weak reference to an event handler no longer\nresolves, that handler is skipped and the reference to that handler is\nunregistered.\n\n\n```python\nimport awebus\nimport asyncio\n\n# Create a bus\nbus = awebus.Bus()\n\n# Create a regular handler\ndef handler():\n print( \"In Handler\" )\n return \"handled\"\n\n# Register the handler\nbus.on( 'weak-event', handler )\n\n# Invoke the event\n# The handler will run normally at this point.\nresults = bus.emit( 'weak-event' )\nprint( results )\n# >> [ \"handled\" ]\n\n# Delete the handler\ndel handler\n\n# Invoke the event again\n# The handler was never unregistered, but because weakrefs are\n# used, the deleted handler will be skipped and cleaned up. Yay!\nresults = bus.emit( 'weak-event' )\nprint( results )\n# >> []\n```\n\n#### Disabling Weak References\n\nThe use of weak references can be disabled by setting the `event_use_weakref`\nkeyword argument to `False` when creating an event bus.\n\n```python\n\nimport awebus\nimport asyncio\n\n# Create a bus\nbus = awebus.Bus( event_use_weakref = False )\n\n# Create a regular handler\ndef handler():\n print( \"In Handler\" )\n return \"result from non-weakref handler\"\n\n# Register the handler\nbus.on( 'no-weakref-event', handler )\n\n# Delete the reference to the handler\ndel handler\n\n# Invoke the event\nresults = bus.emit( 'no-weakref-event' )\n\n```\n\n### EventMixin\n\nThe module also exposes an `EventMixin` class which can be used by your own\nclasses to add the event bus functionality to them with little effort.\n\n```python\nimport awebus\n\n# Create a custom class\nclass CustomClass( awebus.EventMixin ):\n def __init__( self, *args, **kwargs ):\n awebus.EventMixin.__init__( self, *args, **kwargs )\n\n def onBar( self ):\n print( \"Bar Handler\" )\n\n# Create an instance\ninstance = CustomClass()\n\n# Treat the instance like an event bus\ninstance.on( 'bar', instance.onBar )\nresults = instance.emit( 'bar' )\nprint( results )\n# >> [ \"bar\" ]\n```\n\n\n\n## Authors\n\n* Daniel 'Vector' Kerr \n\n\n\n## License\n\nSee `LICENSE`", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://gitlab.com/vectoridau/awebus", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "awebus", "package_url": "https://pypi.org/project/awebus/", "platform": "", "project_url": "https://pypi.org/project/awebus/", "project_urls": { "Homepage": "https://gitlab.com/vectoridau/awebus" }, "release_url": "https://pypi.org/project/awebus/1.0a6/", "requires_dist": null, "requires_python": "", "summary": "Asynchronous Weak Event Bus", "version": "1.0a6" }, "last_serial": 4584169, "releases": { "0.1dev": [ { "comment_text": "", "digests": { "md5": "037791851d51a4444eb47e1768e6eb94", "sha256": "0240af3dc9a92227fbdf72e77c1a7124131ef0fb47e8131dd4a1351c7edd8e3d" }, "downloads": -1, "filename": "awebus-0.1dev.tar.gz", "has_sig": false, "md5_digest": "037791851d51a4444eb47e1768e6eb94", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6429, "upload_time": "2018-12-11T07:42:03", "url": "https://files.pythonhosted.org/packages/ea/bf/fa1c013cad4807602fa87ae2475168d994ea9b4f3741a244a20bbd67510c/awebus-0.1dev.tar.gz" } ], "1.0a6": [ { "comment_text": "", "digests": { "md5": "e26c3ee77eb4d01232403882b667fbf4", "sha256": "eda73b625ef2bee93e59762ccd24b4217a7703425392db50a901d62746c66590" }, "downloads": -1, "filename": "awebus-1.0a6.tar.gz", "has_sig": false, "md5_digest": "e26c3ee77eb4d01232403882b667fbf4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6455, "upload_time": "2018-12-11T07:48:27", "url": "https://files.pythonhosted.org/packages/d2/96/8827be2cb273ec586981687a92c6d7dd142244115ff2e579b1e003f6c17e/awebus-1.0a6.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e26c3ee77eb4d01232403882b667fbf4", "sha256": "eda73b625ef2bee93e59762ccd24b4217a7703425392db50a901d62746c66590" }, "downloads": -1, "filename": "awebus-1.0a6.tar.gz", "has_sig": false, "md5_digest": "e26c3ee77eb4d01232403882b667fbf4", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6455, "upload_time": "2018-12-11T07:48:27", "url": "https://files.pythonhosted.org/packages/d2/96/8827be2cb273ec586981687a92c6d7dd142244115ff2e579b1e003f6c17e/awebus-1.0a6.tar.gz" } ] }