simple-observable


Namesimple-observable JSON
Version 0.1.0 PyPI version JSON
download
home_page
SummaryA simple observer pattern implementation
upload_time2024-02-03 18:22:17
maintainer
docs_urlNone
author
requires_python<4,>=3.7
license
keywords observer observable
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # simple-observable

A simple observer pattern implementation.

### Usage example

```python
from simple_observable import Observable


observable = Observable()


def my_callback(event):
    print(f"Called with {event}")


observable.add_listener("mytopic", my_callback)

observable.notify("mytopic", {"event_details": "hello"})

```

### Topic lifecycle events

The special topic `Observable.LIFECYCLE_EVENTS` can be used to perform an actions when new topics are added and existing ones are removed. 

```python
from simple_observable import Observable


observable = Observable()


def on_lifecycle_event(event):
    if event["event_type"] == observable.TOPIC_ADDED_EVENT:
        topic = event["topic"]
        print(f"topic lifecycle: added '{topic}'")
    if event["event_type"] == observable.TOPIC_REMOVED_EVENT:
        topic = event["topic"]
        print(f"topic lifecycle: removed '{topic}'")


observable.add_listener(observable.LIFECYCLE_EVENTS, on_lifecycle_event)


def my_callback(event):
    print(f"Called with {event}")


# the next line triggers a lifecycle event {"event_type": observable.TOPIC_ADDED_EVENT, "topic": "mytopic"}
listener_id = observable.add_listener("mytopic", my_callback)

observable.notify("mytopic", {"key": "value"})

# and the last line triggers a lifecycle event {"event_type": observable.TOPIC_REMOVED_EVENT, "topic": "mytopic"}
observable.remove_listener(listener_id)


```

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "simple-observable",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "<4,>=3.7",
    "maintainer_email": "",
    "keywords": "observer,observable",
    "author": "",
    "author_email": "darkhaniop <darkhaniop@google.com>",
    "download_url": "https://files.pythonhosted.org/packages/0b/ab/9fbbfafa1e3a89004b5bf5458a655270516ea54dc8905e975bd05cec93f3/simple-observable-0.1.0.tar.gz",
    "platform": null,
    "description": "# simple-observable\n\nA simple observer pattern implementation.\n\n### Usage example\n\n```python\nfrom simple_observable import Observable\n\n\nobservable = Observable()\n\n\ndef my_callback(event):\n    print(f\"Called with {event}\")\n\n\nobservable.add_listener(\"mytopic\", my_callback)\n\nobservable.notify(\"mytopic\", {\"event_details\": \"hello\"})\n\n```\n\n### Topic lifecycle events\n\nThe special topic `Observable.LIFECYCLE_EVENTS` can be used to perform an actions when new topics are added and existing ones are removed. \n\n```python\nfrom simple_observable import Observable\n\n\nobservable = Observable()\n\n\ndef on_lifecycle_event(event):\n    if event[\"event_type\"] == observable.TOPIC_ADDED_EVENT:\n        topic = event[\"topic\"]\n        print(f\"topic lifecycle: added '{topic}'\")\n    if event[\"event_type\"] == observable.TOPIC_REMOVED_EVENT:\n        topic = event[\"topic\"]\n        print(f\"topic lifecycle: removed '{topic}'\")\n\n\nobservable.add_listener(observable.LIFECYCLE_EVENTS, on_lifecycle_event)\n\n\ndef my_callback(event):\n    print(f\"Called with {event}\")\n\n\n# the next line triggers a lifecycle event {\"event_type\": observable.TOPIC_ADDED_EVENT, \"topic\": \"mytopic\"}\nlistener_id = observable.add_listener(\"mytopic\", my_callback)\n\nobservable.notify(\"mytopic\", {\"key\": \"value\"})\n\n# and the last line triggers a lifecycle event {\"event_type\": observable.TOPIC_REMOVED_EVENT, \"topic\": \"mytopic\"}\nobservable.remove_listener(listener_id)\n\n\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A simple observer pattern implementation",
    "version": "0.1.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/gw-tools/simple-observable/issues",
        "Homepage": "https://github.com/gw-tools/simple-observable"
    },
    "split_keywords": [
        "observer",
        "observable"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d12fcb9c314e5a8abeb99adba1cc23c33b4478269477e132cea90582ee75470c",
                "md5": "1eeda7fc87b71c40b17b0142dc392264",
                "sha256": "3b7f9adcac95b73198f07dbef68918909c1c1f7613d90430546e097bd91e262a"
            },
            "downloads": -1,
            "filename": "simple_observable-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1eeda7fc87b71c40b17b0142dc392264",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.7",
            "size": 4806,
            "upload_time": "2024-02-03T18:22:16",
            "upload_time_iso_8601": "2024-02-03T18:22:16.512027Z",
            "url": "https://files.pythonhosted.org/packages/d1/2f/cb9c314e5a8abeb99adba1cc23c33b4478269477e132cea90582ee75470c/simple_observable-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "0bab9fbbfafa1e3a89004b5bf5458a655270516ea54dc8905e975bd05cec93f3",
                "md5": "eedae2aadbd70281c377608949016f32",
                "sha256": "d29cd411ffc775452c7e9fa02a602c9ca5c768c49b312f69887090eb8c4014de"
            },
            "downloads": -1,
            "filename": "simple-observable-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "eedae2aadbd70281c377608949016f32",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.7",
            "size": 3512,
            "upload_time": "2024-02-03T18:22:17",
            "upload_time_iso_8601": "2024-02-03T18:22:17.967361Z",
            "url": "https://files.pythonhosted.org/packages/0b/ab/9fbbfafa1e3a89004b5bf5458a655270516ea54dc8905e975bd05cec93f3/simple-observable-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-02-03 18:22:17",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "gw-tools",
    "github_project": "simple-observable",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "simple-observable"
}
        
Elapsed time: 3.91110s