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