-
Hey guys, I want to be able to create something like a metaclass for states Purpose: Perfectly -> run a for loop for a predefined range (f.e. 4) and get the number of unique states that I need (in this case I'd have 4 unique states created) Ready to buy a coffee for the one who solves this ;) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
If I understood you correctly, you can create your states as usual and loop them inside handlers like this: class States(StatesGroup):
a = State()
b = State()
c = State()
@dp.message_handler(commands=['start'])
@dp.message_handler(state=States.a)
async def first(msg):
await msg.reply('Gone to second')
await States.b.set()
@dp.message_handler(state=States.b)
async def first(msg):
await msg.reply('Gone to third')
await States.c.set()
@dp.message_handler(state=States.c)
async def first(msg):
await msg.reply('Gone to start')
await States.a.set() |
Beta Was this translation helpful? Give feedback.
-
Ok, so I came up with the answer myself. If anyone will search for an answer: def generate_state_groups(
group_prefix: str, groups_amount: int, states_amount: int
) -> list:
"""
Generates any amount of unique groups with a strict amount of states for each.
"""
return [
type(
f"{group_prefix}_{n}",
(StatesGroup,),
{f"{n}state{nn}": State() for nn in range(states_amount)},
)
for n in range(groups_amount)
] |
Beta Was this translation helpful? Give feedback.
Ok, so I came up with the answer myself. If anyone will search for an answer: