state-handler 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- state_handler/State_handler.py +43 -0
- state_handler/__init__.py +1 -0
- state_handler-0.1.0.dist-info/METADATA +130 -0
- state_handler-0.1.0.dist-info/RECORD +7 -0
- state_handler-0.1.0.dist-info/WHEEL +5 -0
- state_handler-0.1.0.dist-info/licenses/LICENCE +21 -0
- state_handler-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# state.py
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
class State:
|
|
7
|
+
def __init__(self):
|
|
8
|
+
self._data = {}
|
|
9
|
+
self._callbacks = []
|
|
10
|
+
|
|
11
|
+
def get(self, key, default=None):
|
|
12
|
+
return self._data.get(key, default)
|
|
13
|
+
|
|
14
|
+
def set(self, key, value):
|
|
15
|
+
old_value = self._data.get(key)
|
|
16
|
+
self._data[key] = value
|
|
17
|
+
|
|
18
|
+
for callback in self._callbacks:
|
|
19
|
+
callback(key, old_value, value)
|
|
20
|
+
|
|
21
|
+
def delete(self, key):
|
|
22
|
+
self._data.pop(key, None)
|
|
23
|
+
|
|
24
|
+
def exists(self, key):
|
|
25
|
+
return key in self._data
|
|
26
|
+
|
|
27
|
+
def register_callback(self, callback):
|
|
28
|
+
self._callbacks.append(callback)
|
|
29
|
+
|
|
30
|
+
def save(self, filename="state.json"):
|
|
31
|
+
with open(filename, "w") as f:
|
|
32
|
+
json.dump(self._data, f, indent=4)
|
|
33
|
+
|
|
34
|
+
def load(self, filename="state.json"):
|
|
35
|
+
if Path(filename).exists():
|
|
36
|
+
with open(filename, "r") as f:
|
|
37
|
+
self._data = json.load(f)
|
|
38
|
+
|
|
39
|
+
def all(self):
|
|
40
|
+
return self._data.copy()
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
state = State()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .State_handler import State
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: state-handler
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple global state manager to help pass values through mautiple py files
|
|
5
|
+
Author: OneNorth Technologies
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
License-File: LICENCE
|
|
9
|
+
Dynamic: license-file
|
|
10
|
+
|
|
11
|
+
# State Handler
|
|
12
|
+
|
|
13
|
+
A lightweight global state manager for Python.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
* Simple global key-value storage
|
|
20
|
+
* Accessible across your entire Python project
|
|
21
|
+
* Lightweight and dependency-free
|
|
22
|
+
* Event callbacks on state changes
|
|
23
|
+
* Optional persistence support
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
pip install state-handler
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## Quick Start
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from state_handler import state
|
|
39
|
+
|
|
40
|
+
# Set a value
|
|
41
|
+
state.set("username", "bob")
|
|
42
|
+
|
|
43
|
+
# Get a value
|
|
44
|
+
print(state.get("username"))
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## API Reference
|
|
50
|
+
|
|
51
|
+
### set(key, value)
|
|
52
|
+
|
|
53
|
+
Store a value in global state.
|
|
54
|
+
|
|
55
|
+
### get(key, default=None)
|
|
56
|
+
|
|
57
|
+
Retrieve a value.
|
|
58
|
+
|
|
59
|
+
### delete(key)
|
|
60
|
+
|
|
61
|
+
Remove a value.
|
|
62
|
+
|
|
63
|
+
### exists(key)
|
|
64
|
+
|
|
65
|
+
Returns `True` if key exists.
|
|
66
|
+
|
|
67
|
+
### all()
|
|
68
|
+
|
|
69
|
+
Returns a copy of all current state values.
|
|
70
|
+
|
|
71
|
+
---
|
|
72
|
+
|
|
73
|
+
## Persistence
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
state.save("state.json")
|
|
77
|
+
state.load("state.json")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Saves and loads state from a file.
|
|
81
|
+
|
|
82
|
+
---
|
|
83
|
+
|
|
84
|
+
## Callbacks (Events)
|
|
85
|
+
|
|
86
|
+
Run code whenever state changes:
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
def on_change(key, old, new):
|
|
90
|
+
print(f"{key} changed from {old} to {new}")
|
|
91
|
+
|
|
92
|
+
state.register_callback(on_change)
|
|
93
|
+
|
|
94
|
+
state.set("mode", "debug")
|
|
95
|
+
state.set("mode", "release")
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Example
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import state_handler as sh
|
|
104
|
+
|
|
105
|
+
sh.state.set("mode", "debug")
|
|
106
|
+
|
|
107
|
+
if sh.state.get("mode") == "debug":
|
|
108
|
+
print("Debug mode is ON")
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## Why use this?
|
|
114
|
+
|
|
115
|
+
Sometimes you need shared state across multiple files without passing variables around. This package provides a simple global state system for Python projects.
|
|
116
|
+
|
|
117
|
+
---
|
|
118
|
+
|
|
119
|
+
## Roadmap
|
|
120
|
+
|
|
121
|
+
* Nested state support
|
|
122
|
+
* Async event listeners
|
|
123
|
+
* Thread-safe mode
|
|
124
|
+
* State syncing between processes
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT License
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
state_handler/State_handler.py,sha256=__eW-ddFXA-Y_xMHwDlcg_sOCNzRQMJ8YnKvJZRItdQ,1028
|
|
2
|
+
state_handler/__init__.py,sha256=G3P4krZfgRyNQiiLjD_ndLPxErXqKlz1ooD7C0Go2Sg,32
|
|
3
|
+
state_handler-0.1.0.dist-info/licenses/LICENCE,sha256=QYwKdHu4iYcJAq88Q8-ii4FpMbtaSlUnyB6EpCCCung,1097
|
|
4
|
+
state_handler-0.1.0.dist-info/METADATA,sha256=T1Y5RbJ9YYcZdTHA7nZByvgmOZn-dG_HGQ2ZuPzZzL8,2011
|
|
5
|
+
state_handler-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
6
|
+
state_handler-0.1.0.dist-info/top_level.txt,sha256=ptHBnejqngfE0vp_A49hLUXVOOAzA6izw47J4jp3i-k,14
|
|
7
|
+
state_handler-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 OneNorth Technologies
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
state_handler
|