sc4py.choice module

sc4py.choice.to_choice(*args) list[tuple[Any, Any]][source]

Convert a list of values to a list of choices.

Parameters:

*args – A list of values to convert to choices. Each value can be an Enum, an EnumMeta, or any other value. If the value is an Enum, it will be converted to a tuple of (value, description). If the value is an EnumMeta, it will be converted to a list of tuples of (value, description) for each member of the Enum. If the value is any other value, it will be converted to a tuple of (value, value).

Returns:

A list of tuples of (value, description) for each value in the input list. The description is the value of the “description” attribute of the Enum member, if it exists, or the value itself if it does not exist.

Return type:

list of tuples

Examples:

from sc4py.choice import to_choice
from enum import Enum

class Status(Enum):
    ACTIVE   = (1, "Ativo",   "🟢")
    INACTIVE = (2, "Inativo", "🔴")
    PENDING  = (3, "Pendente", "🟡")

    def __init__(self, value, label, icon):
        self._value_ = value
        self.label = label
        self.icon = icon

    def __str__(self):
        return f"{self.icon} {self.label}"

to_choice(Status)
# [(1, '🟢 Ativo'), (2, '🔴 Inativo'), (3, '🟡 Pendente')]
to_choice(Status.ACTIVE)
# [(1, '🟢 Ativo')]
to_choice(1, 2, 3)
# [(1, 1), (2, 2), (3, 3)]