sideeffect


Namesideeffect JSON
Version 1.0.4 PyPI version JSON
download
home_pagehttps://github.com/sharunashwanth/SideEffect
SummaryA Python library for managing state with synchronous or asynchronous side effects effortlessly.
upload_time2024-06-13 18:07:42
maintainerSharun Ashwanth K V
docs_urlNone
authorSharun Ashwanth K V
requires_pythonNone
licenseNone
keywords sideeffect state-management reactivity
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SideEffect Library

The `SideEffect` library provides a convenient way to manage state changes with optional synchronous or asynchronous side effects. This can be particularly useful in scenarios where you want to perform an action whenever a state changes, such as updating a user interface, logging, notifications, or other side effects.

## Installation

You can install `SideEffect` using pip:

```bash
pip install sideeffect
```

## Usage

### `SideEffect` Class

The `SideEffect` class allows you to manage a state with an optional side effect that can be executed either synchronously or asynchronously when the state is changed. Additionally, you can specify if the side effect is dependent on the state or not.

#### Example

```python
from sideeffect import SideEffect

def drink_coffee():
    print("Coffee consumed ☕️")

coffee_level = SideEffect(default="Full", side_effect=drink_coffee)

# Oh no, the coffee level is dropping!
coffee_level.setState("Half")
```

### `side_effect` Function

The `side_effect` function is a convenience function that creates a `SideEffect` instance and returns accessor functions for getting and setting the state.

#### Example

```python
from sideeffect import side_effect

def drink_coffee():
    print("Coffee consumed ☕️")

coffee_level, set_coffee_level = side_effect(default="Full", side_effect=drink_coffee)

# Oh no, the coffee level is dropping!
set_coffee_level("Half")
```

## API

1. ### `SideEffect` Class

    `__init__(self, default=0, side_effect=lambda: None, *, asynchronous=True, dependent=False) -> None`

    Initializes the `SideEffect` instance.

    - `default` (Any): The initial state (default is 0).
    - `side_effect` (Callable[[], None]): A callable to be executed as a side effect (default is a no-op lambda).
    - `asynchronous` (bool): If True, the side effect is executed asynchronously (default is True).
    - `dependent` (bool): If True, the side effect is dependent on the state (default is False).

    `state(self) -> Any`

    Returns the current state.

    `setState(self, value: Any, *, asynchronous: Union[bool, None]=None, cancel_side_effect: bool=False) -> None`

    Sets the state to the given value and executes the side effect.

    - `value` (Any): The new state value.
    - `asynchronous` (Union[bool, None]): If provided, overrides the instance's asynchronous setting for this call.
    - `cancel_side_effect` (bool): If True, cancels the execution of the side effect.
    - Raises `TypeError` if the `asynchronous` or `cancel_side_effect` parameter is not a boolean.

    `disable_side_effect(self) -> None`

    Disables the execution of the side effect.

    `enable_side_effect(self) -> None`

    Enables the execution of the side effect.

1. ### `side_effect` Function

    `side_effect(default=0, side_effect=lambda: None, *, asynchronous=True, dependent=False) -> Tuple[SideEffect.state, SideEffect.setState]`

    A convenience function to create a `SideEffect` instance and return accessor functions.

    - `default` (Any): The initial state (default is 0).
    - `side_effect` (Callable[[], None]): A callable to be executed as a side effect (default is a no-op lambda).
    - `asynchronous` (bool): If True, the side effect is executed asynchronously (default is True).
    - `dependent` (bool): If True, the side effect is dependent on the state (default is False).

    Returns a tuple containing:
    - A lambda to get the current state.
    - A lambda to set the state and execute the side effect.

## New Features in v1.0.4

- **Disable/Enable Side Effects**: Introduced `disable_side_effect` and `enable_side_effect` methods to temporarily pause and resume side effect execution.
- **Cancel Side Effects**: Added a `cancel_side_effect` parameter to `setState` to allow canceling the execution of the side effect when changing the state.

## Contributing

Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/sharunashwanth/SideEffect).

## License

This project is licensed under the MIT License. See the `LICENSE` file for details.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/sharunashwanth/SideEffect",
    "name": "sideeffect",
    "maintainer": "Sharun Ashwanth K V",
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": "sharunashwanth03@gmail.com",
    "keywords": "sideeffect, state-management, reactivity",
    "author": "Sharun Ashwanth K V",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/ef/71/f3491c5a3236a6350b81ec6f861144279943c1b536c1c611fdbeeee68025/sideeffect-1.0.4.tar.gz",
    "platform": null,
    "description": "# SideEffect Library\r\n\r\nThe `SideEffect` library provides a convenient way to manage state changes with optional synchronous or asynchronous side effects. This can be particularly useful in scenarios where you want to perform an action whenever a state changes, such as updating a user interface, logging, notifications, or other side effects.\r\n\r\n## Installation\r\n\r\nYou can install `SideEffect` using pip:\r\n\r\n```bash\r\npip install sideeffect\r\n```\r\n\r\n## Usage\r\n\r\n### `SideEffect` Class\r\n\r\nThe `SideEffect` class allows you to manage a state with an optional side effect that can be executed either synchronously or asynchronously when the state is changed. Additionally, you can specify if the side effect is dependent on the state or not.\r\n\r\n#### Example\r\n\r\n```python\r\nfrom sideeffect import SideEffect\r\n\r\ndef drink_coffee():\r\n    print(\"Coffee consumed \u2615\ufe0f\")\r\n\r\ncoffee_level = SideEffect(default=\"Full\", side_effect=drink_coffee)\r\n\r\n# Oh no, the coffee level is dropping!\r\ncoffee_level.setState(\"Half\")\r\n```\r\n\r\n### `side_effect` Function\r\n\r\nThe `side_effect` function is a convenience function that creates a `SideEffect` instance and returns accessor functions for getting and setting the state.\r\n\r\n#### Example\r\n\r\n```python\r\nfrom sideeffect import side_effect\r\n\r\ndef drink_coffee():\r\n    print(\"Coffee consumed \u2615\ufe0f\")\r\n\r\ncoffee_level, set_coffee_level = side_effect(default=\"Full\", side_effect=drink_coffee)\r\n\r\n# Oh no, the coffee level is dropping!\r\nset_coffee_level(\"Half\")\r\n```\r\n\r\n## API\r\n\r\n1. ### `SideEffect` Class\r\n\r\n    `__init__(self, default=0, side_effect=lambda: None, *, asynchronous=True, dependent=False) -> None`\r\n\r\n    Initializes the `SideEffect` instance.\r\n\r\n    - `default` (Any): The initial state (default is 0).\r\n    - `side_effect` (Callable[[], None]): A callable to be executed as a side effect (default is a no-op lambda).\r\n    - `asynchronous` (bool): If True, the side effect is executed asynchronously (default is True).\r\n    - `dependent` (bool): If True, the side effect is dependent on the state (default is False).\r\n\r\n    `state(self) -> Any`\r\n\r\n    Returns the current state.\r\n\r\n    `setState(self, value: Any, *, asynchronous: Union[bool, None]=None, cancel_side_effect: bool=False) -> None`\r\n\r\n    Sets the state to the given value and executes the side effect.\r\n\r\n    - `value` (Any): The new state value.\r\n    - `asynchronous` (Union[bool, None]): If provided, overrides the instance's asynchronous setting for this call.\r\n    - `cancel_side_effect` (bool): If True, cancels the execution of the side effect.\r\n    - Raises `TypeError` if the `asynchronous` or `cancel_side_effect` parameter is not a boolean.\r\n\r\n    `disable_side_effect(self) -> None`\r\n\r\n    Disables the execution of the side effect.\r\n\r\n    `enable_side_effect(self) -> None`\r\n\r\n    Enables the execution of the side effect.\r\n\r\n1. ### `side_effect` Function\r\n\r\n    `side_effect(default=0, side_effect=lambda: None, *, asynchronous=True, dependent=False) -> Tuple[SideEffect.state, SideEffect.setState]`\r\n\r\n    A convenience function to create a `SideEffect` instance and return accessor functions.\r\n\r\n    - `default` (Any): The initial state (default is 0).\r\n    - `side_effect` (Callable[[], None]): A callable to be executed as a side effect (default is a no-op lambda).\r\n    - `asynchronous` (bool): If True, the side effect is executed asynchronously (default is True).\r\n    - `dependent` (bool): If True, the side effect is dependent on the state (default is False).\r\n\r\n    Returns a tuple containing:\r\n    - A lambda to get the current state.\r\n    - A lambda to set the state and execute the side effect.\r\n\r\n## New Features in v1.0.4\r\n\r\n- **Disable/Enable Side Effects**: Introduced `disable_side_effect` and `enable_side_effect` methods to temporarily pause and resume side effect execution.\r\n- **Cancel Side Effects**: Added a `cancel_side_effect` parameter to `setState` to allow canceling the execution of the side effect when changing the state.\r\n\r\n## Contributing\r\n\r\nContributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/sharunashwanth/SideEffect).\r\n\r\n## License\r\n\r\nThis project is licensed under the MIT License. See the `LICENSE` file for details.\r\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Python library for managing state with synchronous or asynchronous side effects effortlessly.",
    "version": "1.0.4",
    "project_urls": {
        "Homepage": "https://github.com/sharunashwanth/SideEffect"
    },
    "split_keywords": [
        "sideeffect",
        " state-management",
        " reactivity"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "3fba4a489279267b4a2201ca603b4e103abfd9dfeec59a77a95657919cd81ffe",
                "md5": "8d5c9398c7d8aa6a8316d894a7f8f4bb",
                "sha256": "7ed02d84fb023732a5f576e5a490eec34c9b653f71ae689fb73d60f4b015e73b"
            },
            "downloads": -1,
            "filename": "sideeffect-1.0.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "8d5c9398c7d8aa6a8316d894a7f8f4bb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5246,
            "upload_time": "2024-06-13T18:06:53",
            "upload_time_iso_8601": "2024-06-13T18:06:53.541017Z",
            "url": "https://files.pythonhosted.org/packages/3f/ba/4a489279267b4a2201ca603b4e103abfd9dfeec59a77a95657919cd81ffe/sideeffect-1.0.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ef71f3491c5a3236a6350b81ec6f861144279943c1b536c1c611fdbeeee68025",
                "md5": "2ee82bd11d6ed7b2c7479c7abdb4838c",
                "sha256": "8394b0add0624ca91340aae3b05443a6e67db303ceaf359fcb2c9bf50b19ec7e"
            },
            "downloads": -1,
            "filename": "sideeffect-1.0.4.tar.gz",
            "has_sig": false,
            "md5_digest": "2ee82bd11d6ed7b2c7479c7abdb4838c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4651,
            "upload_time": "2024-06-13T18:07:42",
            "upload_time_iso_8601": "2024-06-13T18:07:42.875741Z",
            "url": "https://files.pythonhosted.org/packages/ef/71/f3491c5a3236a6350b81ec6f861144279943c1b536c1c611fdbeeee68025/sideeffect-1.0.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-13 18:07:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sharunashwanth",
    "github_project": "SideEffect",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "sideeffect"
}
        
Elapsed time: 0.24916s