Skip to content

How to generate a specified amount of States and StatesGroups #657

Answered by Salz0
Salz0 asked this question in Q&A
Discussion options

You must be logged in to vote

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)
    ]

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
4 replies
@Salz0
Comment options

@evgfilim1
Comment options

@Salz0
Comment options

@Salz0
Comment options

Comment options

You must be logged in to vote
1 reply
@shakhrillodev
Comment options

Answer selected by Salz0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants