Conditions | 3 |
Total Lines | 85 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # Copyright Pincer 2021-Present |
||
38 | def loop( |
||
39 | self, |
||
40 | days=0, |
||
41 | weeks=0, |
||
42 | hours=0, |
||
43 | minutes=0, |
||
44 | seconds=0, |
||
45 | milliseconds=0, |
||
46 | microseconds=0, |
||
47 | ) -> Callable[[Coro], Task]: |
||
48 | """A decorator to create a task that repeat the given amount of t |
||
49 | :Example usage: |
||
50 | |||
51 | .. code-block:: python |
||
52 | |||
53 | from pincer import Client |
||
54 | from pincer.utils import TaskScheduler |
||
55 | |||
56 | client = Client("token") |
||
57 | task = TaskScheduler(client) |
||
58 | |||
59 | @task.loop(minutes=3) |
||
60 | async def my_task(self): |
||
61 | ... |
||
62 | |||
63 | my_task.start() |
||
64 | client.run() |
||
65 | |||
66 | Parameters |
||
67 | ---------- |
||
68 | days : :class:`int` |
||
69 | Days to wait between iterations. |
||
70 | |default| ``0`` |
||
71 | weeks : :class:`int` |
||
72 | Days to wait between iterations. |
||
73 | |default| ``0`` |
||
74 | hours : :class:`int` |
||
75 | Days to wait between iterations. |
||
76 | |default| ``0`` |
||
77 | minutes : :class:`int` |
||
78 | Days to wait between iterations. |
||
79 | |default| ``0`` |
||
80 | seconds : :class:`int` |
||
81 | Days to wait between iterations. |
||
82 | |default| ``0`` |
||
83 | milliseconds : :class:`int` |
||
84 | Days to wait between iterations. |
||
85 | |default| ``0`` |
||
86 | microseconds : :class:`int` |
||
87 | Days to wait between iterations. |
||
88 | |default| ``0`` |
||
89 | Raises |
||
90 | ------ |
||
91 | TaskIsNotCoroutine: |
||
92 | The task is not a coroutine. |
||
93 | TaskInvalidDelay: |
||
94 | The delay is 0 or negative. |
||
95 | """ |
||
96 | |||
97 | def decorator(func: Coro) -> Task: |
||
98 | if not iscoroutinefunction(func): |
||
99 | raise TaskIsNotCoroutine( |
||
100 | f"Task `{func.__name__}` is not a coroutine, " |
||
101 | "which is required for tasks." |
||
102 | ) |
||
103 | |||
104 | delay = timedelta( |
||
105 | days=days, |
||
106 | weeks=weeks, |
||
107 | hours=hours, |
||
108 | minutes=minutes, |
||
109 | seconds=seconds, |
||
110 | microseconds=microseconds, |
||
111 | milliseconds=milliseconds, |
||
112 | ).total_seconds() |
||
113 | |||
114 | if delay <= 0: |
||
115 | raise TaskInvalidDelay( |
||
116 | f"Task `{func.__name__}` has a delay of {delay} seconds, " |
||
117 | "which is invalid. Delay must be greater than zero." |
||
118 | ) |
||
119 | |||
120 | return Task(self, func, delay) |
||
121 | |||
122 | return decorator |
||
123 | |||
222 |