Define state_list in __init_subclass__

This commit is contained in:
Cub11k 2023-04-14 22:11:08 +03:00
parent d466da3542
commit e4bddd91cb
2 changed files with 8 additions and 5 deletions

View File

@ -74,17 +74,18 @@ class StatesGroup:
my_state = State() # returns my_state:State string. my_state = State() # returns my_state:State string.
""" """
def __init_subclass__(cls) -> None: def __init_subclass__(cls) -> None:
state_list = []
for name, value in cls.__dict__.items(): for name, value in cls.__dict__.items():
if not name.startswith('__') and not callable(value) and isinstance(value, State): if not name.startswith('__') and not callable(value) and isinstance(value, State):
# change value of that variable # change value of that variable
value.name = ':'.join((cls.__name__, name)) value.name = ':'.join((cls.__name__, name))
value.group = cls value.group = cls
state_list.append(value)
cls._state_list = state_list
@property @property
def state_list(self): def state_list(self):
return [value for name, value in self.__dict__.items() return self._state_list
if not name.startswith('__') and not callable(value) and isinstance(value, State)]
class SkipHandler: class SkipHandler:

View File

@ -185,16 +185,18 @@ class StatesGroup:
my_state = State() # returns my_state:State string. my_state = State() # returns my_state:State string.
""" """
def __init_subclass__(cls) -> None: def __init_subclass__(cls) -> None:
state_list = []
for name, value in cls.__dict__.items(): for name, value in cls.__dict__.items():
if not name.startswith('__') and not callable(value) and isinstance(value, State): if not name.startswith('__') and not callable(value) and isinstance(value, State):
# change value of that variable # change value of that variable
value.name = ':'.join((cls.__name__, name)) value.name = ':'.join((cls.__name__, name))
value.group = cls value.group = cls
state_list.append(value)
cls._state_list = state_list
@property @property
def state_list(self): def state_list(self):
return [value for name, value in self.__dict__.items() return self._state_list
if not name.startswith('__') and not callable(value) and isinstance(value, State)]
class BaseMiddleware: class BaseMiddleware: