-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathglobals.py
29 lines (23 loc) · 1023 Bytes
/
globals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from enum import Enum, auto
class GlobalKeys(Enum):
IS_IDLE = auto()
class GlobalState:
def __init__(self):
self._state = {}
def set_value(self, key: GlobalKeys, value):
"""Set a value for a given key."""
if not isinstance(key, GlobalKeys):
raise ValueError("key must be an instance of GlobalKeys enum")
self._state[key] = value
def get_value(self, key: GlobalKeys, default=None):
"""Get a value by key, return default if key does not exist."""
if not isinstance(key, GlobalKeys):
raise ValueError("key must be an instance of GlobalKeys enum")
return self._state.get(key, default)
def has_key(self, key: GlobalKeys):
"""Check if the given key exists in the state."""
if not isinstance(key, GlobalKeys):
raise ValueError("key must be an instance of GlobalKeys enum")
return key in self._state
# Global instance of the GlobalState
global_state = GlobalState()