Conditions | 4 |
Total Lines | 64 |
Code Lines | 24 |
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 |
||
106 | def select_menu( |
||
107 | func=None, |
||
108 | options: List[SelectOption] = None, |
||
109 | placeholder: str = None, |
||
110 | min_values: int = None, |
||
111 | max_values: int = None, |
||
112 | disabled: bool = None, |
||
113 | custom_id: str = None |
||
114 | |||
115 | |||
116 | ) -> SelectMenu: |
||
117 | """ |
||
118 | Turn a function into handler for a :class:`~pincer.commands.components.select_menu.SelectMenu`. |
||
119 | See :class:`~pincer.commands.components.select_menu.SelectMenu` for information on parameters. |
||
120 | |||
121 | The function will still be callable. |
||
122 | |||
123 | .. code-block:: python |
||
124 | |||
125 | from pincer.commands import button, ActionRow, ButtonStyle |
||
126 | |||
127 | class Bot(Client): |
||
128 | |||
129 | @command |
||
130 | async def send_a_select_menu(self): |
||
131 | return Message( |
||
132 | content="Choose an option", |
||
133 | components=[ |
||
134 | ActionRow( |
||
135 | self.select_menu |
||
136 | ) |
||
137 | ] |
||
138 | ) |
||
139 | |||
140 | @select_menu(options=[ |
||
141 | SelectOption(label="Option 1"), |
||
142 | SelectOption(label="Option 2", value="value different than label") |
||
143 | ]) |
||
144 | async def select_menu(values: List[str]): |
||
145 | return f"{values[0]} selected" |
||
146 | |||
147 | """ # noqa: E501 |
||
148 | |||
149 | def wrap(custom_id, func) -> SelectMenu: |
||
150 | if not iscoroutinefunction(func): |
||
151 | raise CommandIsNotCoroutine(f"`{func.__name__}` must be a coroutine.") |
||
152 | |||
153 | if custom_id is None: |
||
154 | custom_id = func.__name__ |
||
155 | |||
156 | return _PartialSelectMenu( |
||
157 | func=func, |
||
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 | ) |
||
165 | |||
166 | if func is None: |
||
167 | return partial(wrap, custom_id) |
||
168 | |||
169 | return wrap(custom_id, func) |
||
170 | |||
184 |