CheeseSignal 1.0.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of CheeseSignal might be problematic. Click here for more details.

@@ -0,0 +1,331 @@
1
+ from typing import List, Callable, overload, Any
2
+
3
+ class Receiver:
4
+ def __init__(self, fn: Callable, *, expected_receive_num: int, auto_remove: bool):
5
+ '''
6
+ - Args
7
+
8
+ - expected_receive_num: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
9
+
10
+ - auto_remove: 是否自动删除响应次数超出期望次数的接收器。
11
+ '''
12
+ self.fn: Callable = fn
13
+ self._expected_receive_num: int = expected_receive_num
14
+ self._auto_remove: bool = auto_remove
15
+ self._total_receive_num: int = 0
16
+
17
+ def reset(self):
18
+ '''
19
+ 重统计置数据。
20
+
21
+ 在有期望信号接受次数的情况下,`auto_remove is False`的接收器会重新开始计数并接收信号。
22
+ '''
23
+
24
+ self._total_receive_num = 0
25
+
26
+ @property
27
+ def expected_receive_num(self) -> int:
28
+ '''
29
+ 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
30
+ '''
31
+
32
+ return self._expected_receive_num
33
+
34
+ @expected_receive_num.setter
35
+ def expected_receive_num(self, value: int):
36
+ self._expected_receive_num = value
37
+
38
+ @property
39
+ def auto_remove(self) -> bool:
40
+ '''
41
+ 是否自动删除响应次数超出期望次数的接收器。
42
+ '''
43
+
44
+ return self._auto_remove
45
+
46
+ @auto_remove.setter
47
+ def auto_remove(self, value: bool):
48
+ self._auto_remove = value
49
+
50
+ @property
51
+ def total_receive_num(self) -> int:
52
+ '''
53
+ 【只读】 总计信号接受次数。
54
+ '''
55
+
56
+ return self._total_receive_num
57
+
58
+ @property
59
+ def remaining_receive_num(self) -> int:
60
+ '''
61
+ 【只读】 剩余的期望信号接受次数;返回为-1代表无期望信号接受次数。
62
+ '''
63
+
64
+ if not self.expected_receive_num:
65
+ return -1
66
+ return self.expected_receive_num - self.total_receive_num
67
+
68
+ @property
69
+ def receivable(self) -> bool:
70
+ '''
71
+ 【只读】 当前Receiver是否处于可接收信号状态。
72
+ '''
73
+
74
+ if self.remaining_receive_num == -1:
75
+ return True
76
+ return True if self.remaining_receive_num else False
77
+
78
+ @property
79
+ def unreceivable(self) -> bool:
80
+ '''
81
+ 【只读】 当前Receiver是否处于不可接收信号状态。
82
+ '''
83
+
84
+ return not self.receivable
85
+
86
+ class Signal:
87
+ def __init__(self):
88
+ self._receivers: List[Receiver] = []
89
+ self._total_send_num: int = 0
90
+
91
+ @overload
92
+ def connect(self, fn: Callable, *, expected_receive_num: int = 0, auto_remove: bool = False):
93
+ '''
94
+ 通过函数注册响应函数。
95
+
96
+ ```python
97
+ from CheeseSignal import Signal
98
+
99
+ def receiver(*args, **kwargs):
100
+ ...
101
+
102
+ signal = Signal()
103
+ signal.connect(receiver)
104
+ ```
105
+
106
+ - Args
107
+
108
+ - expected_receive_num: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
109
+
110
+ - auto_remove: 是否自动删除响应次数超出期望次数的接收器。
111
+
112
+ - Raise
113
+
114
+ - ValueError: 已有重复的函数接收器。
115
+ '''
116
+
117
+ @overload
118
+ def connect(self, *, expected_receive_num: int = 0, auto_remove: bool = False):
119
+ '''
120
+ 通过装饰器注册响应函数。
121
+
122
+ ```python
123
+ from CheeseSignal import Signal
124
+
125
+ signal = Signal()
126
+
127
+ @signal.connect()
128
+ def receiver(*args, **kwargs):
129
+ ...
130
+ ```
131
+
132
+ - Args
133
+
134
+ - expected_receive_num: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
135
+
136
+ - auto_remove: 是否自动删除响应次数超出期望次数的接收器。
137
+
138
+ - Raise
139
+
140
+ - ValueError: 已有重复的函数接收器。
141
+ '''
142
+
143
+ def connect(self, arg1: Callable | None = None, *, expected_receive_num: int = 0, auto_remove: bool = False):
144
+ if not arg1:
145
+ def wrapper(fn):
146
+ self._connect(fn, expected_receive_num = expected_receive_num, auto_remove = auto_remove)
147
+ return fn
148
+ return wrapper
149
+
150
+ self._connect(arg1, expected_receive_num = expected_receive_num, auto_remove = auto_remove)
151
+
152
+ def _connect(self, fn: Callable, *, expected_receive_num: int, auto_remove: bool):
153
+ for receiver in self.receivers:
154
+ if receiver.fn == fn:
155
+ raise ValueError('已有重复的函数接收器')
156
+
157
+ self.receivers.append(Receiver(fn, expected_receive_num = expected_receive_num, auto_remove = auto_remove))
158
+
159
+ def send(self, *args, **kwargs) -> List[Any]:
160
+ '''
161
+ 发送信号。
162
+
163
+ ```python
164
+ from CheeseSignal import Signal
165
+
166
+ signal = Signal()
167
+ signal.send('data1', 'data2', **{
168
+ 'key1': 'value1',
169
+ 'key2': 'value2'
170
+ })
171
+ ```
172
+ '''
173
+
174
+ self._total_send_num += 1
175
+
176
+ results = []
177
+ for receiver in self.receivers[:]:
178
+ if receiver.remaining_receive_num > 0 or receiver.remaining_receive_num == -1:
179
+ receiver._total_receive_num += 1
180
+ results.append(receiver.fn(*args, **kwargs))
181
+
182
+ if receiver.remaining_receive_num == 0 and receiver.auto_remove:
183
+ self.receivers.remove(receiver)
184
+ return results
185
+
186
+ async def async_send(self, *args, **kwargs) -> List[Any]:
187
+ '''
188
+ 在协程环境中发送信号,并请保证所有接收函数都是协程函数。
189
+
190
+ ```python
191
+ import asyncio
192
+
193
+ from CheeseSignal import Signal
194
+
195
+ async def run_asyncio():
196
+ signal = Signal()
197
+ await signal.async_send('data1', 'data2', **{
198
+ 'key1': 'value1',
199
+ 'key2': 'value2'
200
+ })
201
+
202
+ asyncio.run(run_asyncio())
203
+ ```
204
+ '''
205
+
206
+ self._total_send_num += 1
207
+
208
+ results = []
209
+ for receiver in self.receivers[:]:
210
+ if receiver.remaining_receive_num > 0 or receiver.remaining_receive_num == -1:
211
+ receiver._total_receive_num += 1
212
+ results.append(await receiver.fn(*args, **kwargs))
213
+
214
+ if receiver.remaining_receive_num == 0 and receiver.auto_remove:
215
+ self.receivers.remove(receiver)
216
+ return results
217
+
218
+ def disconnect(self, fn: Callable):
219
+ '''
220
+ 断开接收器。
221
+
222
+ ```python
223
+ from CheeseSignal import Signal
224
+
225
+ def receiver(*args, **kwargs):
226
+ ...
227
+
228
+ signal = Signal()
229
+ signal.connect(receiver)
230
+ signal.disconnect(receiver)
231
+ ```
232
+
233
+ - Raise
234
+
235
+ - ValueError: 未找到该函数的接收器。
236
+ '''
237
+
238
+ for receiver in self.receivers:
239
+ if receiver.fn == fn:
240
+ self.receivers.remove(receiver)
241
+ return
242
+
243
+ raise ValueError('未找到该函数的接收器')
244
+
245
+ def reset(self):
246
+ '''
247
+ 重置统计数据;所有的接收器的统计数据也会同步清空。
248
+ '''
249
+
250
+ self._total_send_num = 0
251
+ for receiver in self.receivers:
252
+ receiver.reset()
253
+
254
+ def disconnect_all(self):
255
+ '''
256
+ 断开所有接收器。
257
+ '''
258
+
259
+ self._receivers = []
260
+
261
+ def get_receiver(self, fn: Callable) -> Receiver:
262
+ '''
263
+ 获取接收器。
264
+
265
+ ```python
266
+ from CheeseSignal import Signal
267
+
268
+ def receiver(*args, **kwargs):
269
+ ...
270
+
271
+ signal = Signal()
272
+ signal.connect(receiver)
273
+
274
+ print(signal.get_receiver(receiver))
275
+ ```
276
+
277
+ - Raise
278
+
279
+ - ValueError: 未找到该函数的接收器。
280
+ '''
281
+
282
+ for receiver in self.receivers:
283
+ if receiver.fn == fn:
284
+ return receiver
285
+
286
+ raise ValueError('未找到该函数的接收器')
287
+
288
+ def index(self, fn: Callable) -> int:
289
+ '''
290
+ 获取接收器的顺序位置。
291
+
292
+ ```python
293
+ from CheeseSignal import Signal
294
+
295
+ def receiver(*args, **kwargs):
296
+ ...
297
+
298
+ signal = Signal()
299
+ signal.connect(receiver)
300
+
301
+ print(signal.index(receiver))
302
+ ```
303
+
304
+ - Raise
305
+
306
+ - ValueError: 未找到该函数的接收器。
307
+ '''
308
+
309
+ i = 0
310
+ for receiver in self.receivers:
311
+ if receiver.fn == fn:
312
+ return i
313
+ i += 1
314
+
315
+ raise ValueError('未找到该函数的接收器')
316
+
317
+ @property
318
+ def receivers(self) -> List[Receiver]:
319
+ '''
320
+ 【只读】 接收器的列表,按注册顺序排序。
321
+ '''
322
+
323
+ return self._receivers
324
+
325
+ @property
326
+ def total_send_num(self) -> int:
327
+ '''
328
+ 【只读】 总计信号发送次数。
329
+ '''
330
+
331
+ return self._total_send_num
@@ -0,0 +1,231 @@
1
+ Metadata-Version: 2.3
2
+ Name: CheeseSignal
3
+ Version: 1.0.0
4
+ Summary: 一款简约的信号系统。
5
+ Project-URL: Source, https://github.com/CheeseUnknown/CheeseSignal
6
+ Author-email: Cheese Unknown <cheese@cheese.ren>
7
+ License-File: LICENSE
8
+ Keywords: API,BackEnd
9
+ Description-Content-Type: text/markdown
10
+
11
+ # **CheeseSignal**
12
+
13
+ 一款简单的信号系统。
14
+
15
+ ## **安装**
16
+
17
+ 系统要求:Linux。
18
+
19
+ Python要求:目前仅保证支持3.11及以上版本的Python,新版本会优先支持Python的最新稳定版本。
20
+
21
+ ```
22
+ pip install CheeseSignal
23
+ ```
24
+
25
+ ## **使用**
26
+
27
+ 使用方式较为简单,通过函数或装饰件连接至某个信号,随后等待信号发送。
28
+
29
+ ```python
30
+ import time
31
+
32
+ from CheeseSignal import signal
33
+
34
+ signal = Signal()
35
+
36
+ def receiver1(*args, **kwargs):
37
+ print(arg, kwargs)
38
+ signal.connect(receiver1)
39
+
40
+ @signal.connect()
41
+ def receiver2(*args, **kwargs):
42
+ return args, kwargs
43
+
44
+ while True:
45
+ print(signal.send('Hello', 'World', {
46
+ 'Cheese': 'Signal'
47
+ }))
48
+ time.sleep(1)
49
+ ```
50
+
51
+ ## **接口文档**
52
+
53
+ ### **`class Signal`**
54
+
55
+ ```python
56
+ from CheeseSignal import Signal
57
+
58
+ signal = Signal()
59
+ ```
60
+
61
+ #### **`self.receivers: List[Receiver]`**
62
+
63
+ 【只读】 接收器的列表,按注册顺序排序。
64
+
65
+ #### **`self.total_send_num: int`**
66
+
67
+ 【只读】 总计信号发送次数。
68
+
69
+ #### **`def connect(self, fn: Callable, *, expected_receive_num: int = 0, auto_remove: bool = False)`**
70
+
71
+ 通过函数注册响应函数。
72
+
73
+ - **参数**
74
+
75
+ - `expected_receive_num`: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
76
+
77
+ - `auto_remove`: 是否自动删除响应次数超出期望次数的接收器。
78
+
79
+ - **报错**
80
+
81
+ - `ValueError`: 已有重复的函数接收器。
82
+
83
+ #### **`def connect(self, *, expected_receive_num: int = 0, auto_remove: bool = False)`**
84
+
85
+ 通过装饰器注册响应函数。
86
+
87
+ - **参数**
88
+
89
+ - `expected_receive_num`: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
90
+
91
+ - `auto_remove`: 是否自动删除响应次数超出期望次数的接收器。
92
+
93
+ - **报错**
94
+
95
+ - `ValueError`: 已有重复的函数接收器。
96
+
97
+ #### **`def send(self, *args, **kwargs) -> List[Any]`**
98
+
99
+ 发送信号。
100
+
101
+ #### **`async def async_send(self, *args, **kwargs) -> List[Any]`**
102
+
103
+ 在协程环境中发送信号,并请保证所有接收函数都是协程函数。
104
+
105
+ ```python
106
+ import asyncio
107
+
108
+ from CheeseSignal import Signal
109
+
110
+ async def run_asyncio():
111
+ signal = Signal()
112
+ await signal.async_send('data1', 'data2', **{
113
+ 'key1': 'value1',
114
+ 'key2': 'value2'
115
+ })
116
+
117
+ asyncio.run(run_asyncio())
118
+ ```
119
+
120
+ #### **`def disconnect(self, fn: Callable)`**
121
+
122
+ 断开接收器。
123
+
124
+ ```python
125
+ from CheeseSignal import Signal
126
+
127
+ def receiver(*args, **kwargs):
128
+ ...
129
+
130
+ signal = Signal()
131
+ signal.connect(receiver)
132
+ signal.disconnect(receiver)
133
+ ```
134
+
135
+ - **报错**
136
+
137
+ - `ValueError`: 未找到该函数的接收器。
138
+
139
+ #### **`def reset(self)`**
140
+
141
+ 重置统计数据;所有的接收器的统计数据也会同步清空。
142
+
143
+ #### **`def disconnect_all(self)`**
144
+
145
+ 断开所有接收器。
146
+
147
+ #### **`def get_receiver(self, fn: Callable) -> Receiver`**
148
+
149
+ 获取接收器。
150
+
151
+ ```python
152
+ from CheeseSignal import Signal
153
+
154
+ def receiver(*args, **kwargs):
155
+ ...
156
+
157
+ signal = Signal()
158
+ signal.connect(receiver)
159
+
160
+ print(signal.get_receiver(receiver))
161
+ ```
162
+
163
+ - **报错**
164
+
165
+ - `ValueError`: 未找到该函数的接收器。
166
+
167
+ #### **`def index(self, fn: Callable) -> index`**
168
+
169
+ 获取接收器的顺序位置。
170
+
171
+ ```python
172
+ from CheeseSignal import Signal
173
+
174
+ def receiver(*args, **kwargs):
175
+ ...
176
+
177
+ signal = Signal()
178
+ signal.connect(receiver)
179
+
180
+ print(signal.index(receiver))
181
+ ```
182
+
183
+ - **报错**
184
+
185
+ - `ValueError`: 未找到该函数的接收器。
186
+
187
+ ### **`class Receiver`**
188
+
189
+ 正常使用并不需要手动创建该类。
190
+
191
+ ```python
192
+ from CheeseSignal import Receiver
193
+ ```
194
+
195
+ #### **`def __init__(self, fn: Callable, *, expected_receive_num: int, auto_remove: bool)`**
196
+
197
+ - **参数**
198
+
199
+ - `expected_receive_num`: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
200
+
201
+ - `auto_remove`: 是否自动删除响应次数超出期望次数的接收器。
202
+
203
+ #### **`self.expected_receive_num: int`**
204
+
205
+ 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
206
+
207
+ #### **`self.auto_remove: bool`**
208
+
209
+ 是否自动删除响应次数超出期望次数的接收器。
210
+
211
+ #### **`self.total_receive_num: int`**
212
+
213
+ 【只读】 总计信号接受次数。
214
+
215
+ #### **`self.remaining_receive_num: int`**
216
+
217
+ 【只读】 剩余的期望信号接受次数;返回为-1代表无期望信号接受次数。
218
+
219
+ #### **`self.receivable: bool`**
220
+
221
+ 【只读】 当前Receiver是否处于可接收信号状态。
222
+
223
+ #### **`self.unreceivable: bool`**
224
+
225
+ 【只读】 当前Receiver是否处于不可接收信号状态。
226
+
227
+ #### **`def reset(self)`**
228
+
229
+ 重统计置数据。
230
+
231
+ 在有期望信号接受次数的情况下,`auto_remove is False`的接收器会重新开始计数并接收信号。
@@ -0,0 +1,5 @@
1
+ CheeseSignal/__init__.py,sha256=thvWjsoJ8ed1TxU6Rjq4fQDxRDOe1Q1aefhpKLJZuro,8855
2
+ cheesesignal-1.0.0.dist-info/METADATA,sha256=6IzokYvq4ACf7zuJijYVIneesveYHv7grTE3B0p2s-8,4978
3
+ cheesesignal-1.0.0.dist-info/WHEEL,sha256=lA-f9Lj70wKOLrgpRcgvGFxEoHnVMUTjYbvYas0c5ow,105
4
+ cheesesignal-1.0.0.dist-info/licenses/LICENSE,sha256=5vFb3i4UDlskszJ3jGPh8bXrM_axJfDRRuvLu1M3bIs,1070
5
+ cheesesignal-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.24.1
3
+ Root-Is-Purelib: true
4
+ Tag: py2-none-any
5
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Cheese Unknown
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.