https://github.com/salva/py-forstate
Dispatch methods according to internal state
https://github.com/salva/py-forstate
Last synced: 5 months ago
JSON representation
Dispatch methods according to internal state
- Host: GitHub
- URL: https://github.com/salva/py-forstate
- Owner: salva
- License: mit
- Created: 2025-04-19T21:37:59.000Z (8 months ago)
- Default Branch: master
- Last Pushed: 2025-04-19T21:55:34.000Z (8 months ago)
- Last Synced: 2025-07-24T19:37:20.611Z (5 months ago)
- Language: Python
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# forstate
The `forstate` module introduces the `for_state` method decorator,
designed to facilitate state-specific method dispatching.
This decorator enables the definition of multiple versions of the same
method, which are dynamically called based on the object's internal
state, as indicated by its `_state` attribute.
## Installation
You can easily install the `forstate` module via pip. Simply run the
following command in your terminal:
```
pip install forstate
```
## Usage
- Import the `for_state` decorator and, if needed, the `ANY_STATE`
marker.
- Decorate your class methods with `@for_state`, specifying the
relevant state names for each method. This enables you to define
distinct behaviors for the same method name based on the object's
current state.
For instance:
```python
@for_state("state1")
def foo(self, arg):
# Implementation for state1
pass
@for_state("state2", "state3")
def foo(self, arg):
# Implementation for state2 and state3
pass
```
- Utilize the `ANY_STATE` marker to designate fallback methods that
will be invoked when no specific version of a method is defined for
the current state.
### Example
```python
from forstate import for_state, ANY_STATE
class MyStatefulObject:
def __init__(self):
self._state = "state0" # Initial state
@for_state("state1")
def action(self):
print("Action for state 1")
@for_state("state2", "state3")
def action(self):
print("Action for state 2")
@for_state(ANY_STATE)
def action(self):
print("Action for any state")
obj = MyStatefulObject()
obj._state = "state1"
obj.action() # Output: Action for state 1
obj._state = "state2"
obj.action() # Output: Action for state 2
obj._state = "unknown"
obj.action() # Output: Action for any state
```
## Copyright and License
Copyright (c) 2025 Salvador Fandiño (sfandino@yahoo.com)
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.