Conditions | 4 |
Total Lines | 72 |
Code Lines | 28 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Copyright Pincer 2021-Present |
||
103 | def select_menu( |
||
104 | func=None, |
||
105 | options: List[SelectOption] = None, |
||
106 | placeholder: str = None, |
||
107 | min_values: int = None, |
||
108 | max_values: int = None, |
||
109 | disabled: bool = None, |
||
110 | custom_id: str = None |
||
111 | ) -> SelectMenu: |
||
112 | """ |
||
113 | Turn a function into handler for a :class:`~pincer.commands.components.select_menu.SelectMenu`. |
||
114 | See :class:`~pincer.commands.components.select_menu.SelectMenu` for information on paramters. |
||
115 | |||
116 | The function will still be callable. |
||
117 | |||
118 | .. code-block:: python |
||
119 | |||
120 | from pincer.commands import button, ActionRow, ButtonStyle |
||
121 | |||
122 | class Bot(Client): |
||
123 | |||
124 | @command |
||
125 | async def send_a_select_menu(self): |
||
126 | return Message( |
||
127 | content="Choose an option", |
||
128 | components=[ |
||
129 | ActionRow( |
||
130 | self.select_menu |
||
131 | ) |
||
132 | ] |
||
133 | ) |
||
134 | |||
135 | @select_menu(options=[ |
||
136 | SelectOption(label="Option 1"), |
||
137 | SelectOption(label="Option 2", value="value different than label") |
||
138 | ]) |
||
139 | async def select_menu(values: List[str]): |
||
140 | return f"{values[0]} selected" |
||
141 | |||
142 | """ # noqa: E501 |
||
143 | |||
144 | def wrap(custom_id, func) -> SelectMenu: |
||
145 | |||
146 | if not iscoroutinefunction(func): |
||
147 | raise CommandIsNotCoroutine(f"`{func.__name__}` must be a coroutine.") |
||
148 | |||
149 | if custom_id is None: |
||
150 | custom_id = func.__name__ |
||
151 | |||
152 | ComponentHandler().register_id(custom_id, func) |
||
153 | |||
154 | menu = SelectMenu( |
||
155 | # Hack to not override defaults in button class |
||
156 | **remove_none( |
||
157 | { |
||
158 | "custom_id": custom_id, |
||
159 | "options": options, |
||
160 | "placeholder": placeholder, |
||
161 | "min_values": min_values, |
||
162 | "max_values": max_values, |
||
163 | "disabled": disabled, |
||
164 | "_func": func |
||
165 | } |
||
166 | ) |
||
167 | ) |
||
168 | |||
169 | return menu |
||
170 | |||
171 | if func is None: |
||
172 | return partial(wrap, custom_id) |
||
173 | |||
174 | return wrap(custom_id, func) |
||
175 |