{"id":15015678,"url":"https://github.com/lobocv/eventdispatcher","last_synced_at":"2025-04-12T09:32:02.435Z","repository":{"id":21584582,"uuid":"24904594","full_name":"lobocv/eventdispatcher","owner":"lobocv","description":"Allows for automatic dispatching of bound functions when properties are changed.","archived":false,"fork":false,"pushed_at":"2022-10-24T01:51:07.000Z","size":198,"stargazers_count":10,"open_issues_count":2,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T04:51:05.494Z","etag":null,"topics":["bound-functions","callbacks","default-handler","dispatch-events","event-dispatcher","events","handler","kivy","python"],"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/lobocv.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-07T18:16:41.000Z","updated_at":"2023-12-08T09:02:45.000Z","dependencies_parsed_at":"2022-08-21T05:10:14.017Z","dependency_job_id":null,"html_url":"https://github.com/lobocv/eventdispatcher","commit_stats":null,"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobocv%2Feventdispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobocv%2Feventdispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobocv%2Feventdispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lobocv%2Feventdispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lobocv","download_url":"https://codeload.github.com/lobocv/eventdispatcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248546144,"owners_count":21122262,"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":["bound-functions","callbacks","default-handler","dispatch-events","event-dispatcher","events","handler","kivy","python"],"created_at":"2024-09-24T19:47:46.569Z","updated_at":"2025-04-12T09:32:02.173Z","avatar_url":"https://github.com/lobocv.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/lobocv/eventdispatcher.svg?branch=master)](https://travis-ci.org/lobocv/eventdispatcher)\n\nEventDispatcher\n===============\n\nAn event dispatcher framework inspired by the [Kivy project](http://kivy.org/#home).\n \nProperty instances are monitored and dispatch events when their value changes. The event callback handler name defaults to `on_PROPERTY_NAME` and is\ncalled with two arguments: the dispatcher instance and the value of the property.\n\nInstallation\n==================\n\nYou can install eventdispatcher using pip package manager:\n\n    pip install eventdispatcher\n\n    \nLearn by Example:\n================\n\nCreating a Dispatcher\n---------------------\n\nIn our example, we have a settings file that needs to be constantly written to and updated whenever certain settings \nchange. We have two settings that the file tracks, `last_login` and `favourite_color`. \n\nWe can create a EventDispatcher class that can automatically update the file with the latest values whenever they are changed.\n\n```python\n    \n    import json\n    from eventdispatcher import EventDispatcher, Property\n    \n    class SettingsFile(EventDispatcher):\n        last_login = Property(None)\n        favourite_color = Property('red')\n    \n        def __init__(self, filepath):\n            super(SettingsFile, self).__init__()\n            self.filepath = filepath\n            self.number_of_file_updates = 0\n            # Bind the properties to the function that updates the file\n            self.bind(last_login=self.update_settings_file,\n                      favourite_color=self.update_settings_file)\n    \n        def on_last_login(self, inst, last_login):\n            # Default handler for the last_login property\n            print 'last login was %s' % last_login\n    \n        def on_color(self, inst, color):\n            # Default handler for the color property\n            print 'color has been set to %s' % color\n    \n        def update_settings_file(self, *args):\n            # Update the file with the latest settings.\n            print 'Updating settings file.'\n            self.number_of_file_updates += 1\n            with open(self.filepath, 'w') as _f:\n                settings = {'last_login': self.last_login, 'favourite_color': self.favourite_color}\n                json.dump(settings, _f)\n                \n```\n\nNow, in the application, we no longer need to worry about updating the settings file, changing any of the settings will \ndo that for us!\n\n```python\n    s = SettingsFile('./myfile.json')\n    s.last_login = 'May 18 2015'             # Updates settings file\n    # last login was May 18 2015\n    # Updating settings file.\n    s.favourite_color = 'blue'               # Updates settings file\n    # color has been set to blue\n    # Updating settings file.  \n      \n```\n\nThe bound functions are only called when the value of the properties change, so assigning the same date and color will do nothing.\n\n```python\n    s.last_login = 'May 18 2015'             # Does not update the settings file\n    s.favourite_color = 'blue'               # Does not update the settings file  \n```\n    \nBinding Events\n--------------\n\nYou can bind functions to the event queue and call multiple functions when the event dispatches. The handler for the \nbinding is called with the EventDispatcher instance and the property value as the arguments. \n\nIn our example, SettingsFile has default handlers for both color and last_login properties that just print a notification.\nIn addition to these default handlers we bound both of those properties to the function that updates the settings file:\n\n```python\n\n    self.bind(last_login=self.update_settings_file,\n              favourite_color=self.update_settings_file)\n              \n```\n    \nWhen an event is dispatched, the EventDispatcher calls each bound function in order, starting with the default handler.\nYou do not need to define a default handler, the EventDispatcher will not attempt to call it if it does not exist.\n    \n    \nManually Dispatching an Event\n-----------------------------\nYou can also dispatch an event manually:\n\n    d.dispatch('name', d, d.name)\n    \n    \nBinding Properties to Properties\n--------------------------------\n\nYou can bind an event to another so that when one changes, the other will reflect those changes.\n        \n    file1 = SettingsFile('./myfile1.json')\n    file2 = SettingsFile('./myfile2.json')\n       \n    # Binds the color property from file1 to the color property of file2\n    # Changing file1.color will automatically update the value of file2.color\n    file1.bind(color=file2.setter('color'))     \n    \n    file1.color = 'green'\n    # color has been set to green       \n    # Updating settings file.\n    # color has been set to green\n    # Updating settings file.\n\n    \n    \n    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flobocv%2Feventdispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flobocv%2Feventdispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flobocv%2Feventdispatcher/lists"}