Conditions | 3 |
Total Lines | 54 |
Code Lines | 21 |
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 |
||
36 | def button( |
||
37 | label: str, |
||
38 | style: ButtonStyle, |
||
39 | emoji: Emoji = None, |
||
40 | url: str = None, |
||
41 | disabled: bool = None, |
||
42 | custom_id: str = None |
||
43 | ) -> Button: |
||
44 | """ |
||
45 | Turn a function into handler for a :class:`~pincer.commands.components.button.Button`. |
||
46 | See :class:`~pincer.commands.components.button.Button` for information on parameters. |
||
47 | |||
48 | The function will still be callable. |
||
49 | |||
50 | .. code-block:: python |
||
51 | |||
52 | from pincer.commands import ActionRow, Button |
||
53 | |||
54 | class Bot(Client): |
||
55 | |||
56 | @command |
||
57 | async def send_a_button(self): |
||
58 | return Message( |
||
59 | content="Click a button", |
||
60 | components=[ |
||
61 | ActionRow( |
||
62 | self.button_one |
||
63 | ) |
||
64 | ] |
||
65 | ) |
||
66 | |||
67 | @button(label="Click me!", style=ButtonStyle.PRIMARY) |
||
68 | async def button_one(): |
||
69 | return "Button one pressed" |
||
70 | """ # noqa: E501 |
||
71 | |||
72 | def wrap(custom_id, func) -> Button: |
||
73 | if not iscoroutinefunction(func): |
||
74 | raise CommandIsNotCoroutine(f"`{func.__name__}` must be a coroutine.") |
||
75 | |||
76 | if custom_id is None: |
||
77 | custom_id = func.__name__ |
||
78 | |||
79 | return _PartialButton( |
||
80 | func=func, |
||
81 | custom_id=custom_id, |
||
82 | style=style, |
||
83 | label=label, |
||
84 | disabled=disabled, |
||
85 | emoji=emoji, |
||
86 | url=url, |
||
87 | ) |
||
88 | |||
89 | return partial(wrap, custom_id) |
||
90 | |||
184 |