Dispatch methods according to a state value
Project description
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_statedecorator and, if needed, theANY_STATEmarker. -
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:
@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_STATEmarker to designate fallback methods that will be invoked when no specific version of a method is defined for the current state.
Example
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 file for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file forstate-0.0.2.tar.gz.
File metadata
- Download URL: forstate-0.0.2.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d34fda1abbac50838234ae9b2486b2b232814fe2dfadd91b54419625ec4774ed
|
|
| MD5 |
3d1025a531817f4ddab5e647f9c584fd
|
|
| BLAKE2b-256 |
3ecfa4d0ded9e63c7210932f732e2dc68142ca01e8ef9027e8f43651699bbb6d
|
File details
Details for the file forstate-0.0.2-py2.py3-none-any.whl.
File metadata
- Download URL: forstate-0.0.2-py2.py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.32.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2ba7af63948d4f1684b89770ec18ccfc4d436dd8d02918f753069861b365460
|
|
| MD5 |
7b6abb6ca03323f33af5d8de98f6ed76
|
|
| BLAKE2b-256 |
f7d6d4e3c32c07a0062744e6c3792c30e36f6239fd1617ba6abf59f3daa3983b
|