# python-eventemitter
![Build status](https://github.com/jonghwanhyeon/python-eventemitter/actions/workflows/publish.yml/badge.svg)
A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution
## Help
See [documentation](https://python-eventemitter.readthedocs.io) for more details
## Install
To install **python-eventemitter**, simply use pip:
```console
$ pip install python-eventemitter
```
## Usage
### Synchronous API
```python
from eventemitter import EventEmitter
def main():
ee = EventEmitter()
sounds: list[str] = []
ee.add_listener("sound", lambda: sounds.append("woof"))
ee.prepend_listener("sound", lambda: sounds.append("meow"))
ee.on("sound", lambda: sounds.append("oink"))
@ee.on("sound")
def from_cow():
sounds.append("moo")
@ee.once("sound")
def from_bee():
sounds.append("buzz")
ee.emit("sound") # Run events in order
print(sounds) # ['meow', 'woof', 'oink', 'moo', 'buzz']
if __name__ == "__main__":
main()
```
### Asynchronous API
``` python
import asyncio
from eventemitter import AsyncIOEventEmitter
async def main():
aee = AsyncIOEventEmitter()
sounds: set[str] = set()
aee.add_listener("sound", lambda: sounds.add("woof"))
aee.prepend_listener("sound", lambda: sounds.add("meow"))
aee.on("sound", lambda: sounds.add("oink"))
@aee.on("sound")
def from_cow():
sounds.add("moo")
@aee.once("sound")
async def from_bee():
sounds.add("buzz")
await aee.emit("sound") # Run events concurrently
print(sounds) # {'woof', 'meow', 'buzz', 'moo', 'oink'}
if __name__ == "__main__":
asyncio.run(main())
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jonghwanhyeon/python-eventemitter",
"name": "python-eventemitter",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "eventemitter, event",
"author": "Jonghwan Hyeon",
"author_email": "jonghwanhyeon93@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/de/aa/5fbf8add73233ab8020b760b57e8313371512b3528d7d6ec63095d281458/python_eventemitter-1.0.13.tar.gz",
"platform": null,
"description": "# python-eventemitter\n![Build status](https://github.com/jonghwanhyeon/python-eventemitter/actions/workflows/publish.yml/badge.svg)\n\nA Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution\n\n## Help\nSee [documentation](https://python-eventemitter.readthedocs.io) for more details\n\n## Install\nTo install **python-eventemitter**, simply use pip:\n\n```console\n$ pip install python-eventemitter\n```\n\n## Usage\n### Synchronous API\n```python\nfrom eventemitter import EventEmitter\n\n\ndef main():\n ee = EventEmitter()\n\n sounds: list[str] = []\n\n ee.add_listener(\"sound\", lambda: sounds.append(\"woof\"))\n ee.prepend_listener(\"sound\", lambda: sounds.append(\"meow\"))\n ee.on(\"sound\", lambda: sounds.append(\"oink\"))\n\n @ee.on(\"sound\")\n def from_cow():\n sounds.append(\"moo\")\n\n @ee.once(\"sound\")\n def from_bee():\n sounds.append(\"buzz\")\n\n ee.emit(\"sound\") # Run events in order\n print(sounds) # ['meow', 'woof', 'oink', 'moo', 'buzz']\n\n\nif __name__ == \"__main__\":\n main()\n```\n\n### Asynchronous API\n``` python\nimport asyncio\n\nfrom eventemitter import AsyncIOEventEmitter\n\n\nasync def main():\n aee = AsyncIOEventEmitter()\n\n sounds: set[str] = set()\n\n aee.add_listener(\"sound\", lambda: sounds.add(\"woof\"))\n aee.prepend_listener(\"sound\", lambda: sounds.add(\"meow\"))\n aee.on(\"sound\", lambda: sounds.add(\"oink\"))\n\n @aee.on(\"sound\")\n def from_cow():\n sounds.add(\"moo\")\n\n @aee.once(\"sound\")\n async def from_bee():\n sounds.add(\"buzz\")\n\n await aee.emit(\"sound\") # Run events concurrently\n print(sounds) # {'woof', 'meow', 'buzz', 'moo', 'oink'}\n\n\nif __name__ == \"__main__\":\n asyncio.run(main())\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Python port of Node.js EventEmitter that supports both synchronous and asynchronous event execution",
"version": "1.0.13",
"project_urls": {
"Homepage": "https://github.com/jonghwanhyeon/python-eventemitter"
},
"split_keywords": [
"eventemitter",
" event"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f3136090fbbc0b84b58b5538f33a04dc03adfd53af098e391c8ea3c857401405",
"md5": "fb0382cd1bc2ff216936d6320489687e",
"sha256": "96a01cd3b64e3d11e1b4e8521447b24d8081b359d3641de306d7ed8ce66f71ac"
},
"downloads": -1,
"filename": "python_eventemitter-1.0.13-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fb0382cd1bc2ff216936d6320489687e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 26769,
"upload_time": "2024-08-06T11:27:33",
"upload_time_iso_8601": "2024-08-06T11:27:33.583781Z",
"url": "https://files.pythonhosted.org/packages/f3/13/6090fbbc0b84b58b5538f33a04dc03adfd53af098e391c8ea3c857401405/python_eventemitter-1.0.13-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "deaa5fbf8add73233ab8020b760b57e8313371512b3528d7d6ec63095d281458",
"md5": "f017ced532d4b04e1267ebc8ee26d904",
"sha256": "35372f6a0613eb786bc158c6b2ba70167c0b82d37940b5517a0f43ea706b977f"
},
"downloads": -1,
"filename": "python_eventemitter-1.0.13.tar.gz",
"has_sig": false,
"md5_digest": "f017ced532d4b04e1267ebc8ee26d904",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 15895,
"upload_time": "2024-08-06T11:27:35",
"upload_time_iso_8601": "2024-08-06T11:27:35.006721Z",
"url": "https://files.pythonhosted.org/packages/de/aa/5fbf8add73233ab8020b760b57e8313371512b3528d7d6ec63095d281458/python_eventemitter-1.0.13.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-06 11:27:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "jonghwanhyeon",
"github_project": "python-eventemitter",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "typing-extensions",
"specs": []
}
],
"lcname": "python-eventemitter"
}