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.
Metadata
Release files for forstate 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| forstate-0.0.2.tar.gz | 3.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| forstate-0.0.2-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 7.4 kB
Release files / forstate-0.0.2.tar.gz
| Download URL | forstate-0.0.2.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d34fda1abbac50838234ae9b2486b2b232814fe2dfadd91b54419625ec4774ed
|
|
BLAKE2b-256 checksum How to use checksums |
3ecfa4d0ded9e63c7210932f732e2dc68142ca01e8ef9027e8f43651699bbb6d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|
Release files / forstate-0.0.2-py2.py3-none-any.whl
| Download URL | forstate-0.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 3.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
a2ba7af63948d4f1684b89770ec18ccfc4d436dd8d02918f753069861b365460
|
|
BLAKE2b-256 checksum How to use checksums |
f7d6d4e3c32c07a0062744e6c3792c30e36f6239fd1617ba6abf59f3daa3983b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-requests/2.32.3
|