CheeseSignal 1.0.0__py2.py3-none-any.whl → 1.1.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.
- CheeseSignal/__init__.py +44 -15
- {cheesesignal-1.0.0.dist-info → cheesesignal-1.1.0.dist-info}/METADATA +14 -6
- cheesesignal-1.1.0.dist-info/RECORD +5 -0
- {cheesesignal-1.0.0.dist-info → cheesesignal-1.1.0.dist-info}/WHEEL +1 -1
- cheesesignal-1.0.0.dist-info/RECORD +0 -5
- {cheesesignal-1.0.0.dist-info → cheesesignal-1.1.0.dist-info}/licenses/LICENSE +0 -0
CheeseSignal/__init__.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
from typing import List, Callable, overload, Any
|
|
1
|
+
from typing import List, Callable, overload, Any, TYPE_CHECKING
|
|
2
|
+
|
|
3
|
+
if TYPE_CHECKING:
|
|
4
|
+
class Signal:
|
|
5
|
+
...
|
|
2
6
|
|
|
3
7
|
class Receiver:
|
|
4
|
-
def __init__(self, fn: Callable, *, expected_receive_num: int, auto_remove: bool):
|
|
8
|
+
def __init__(self, signal: 'Signal', fn: Callable, *, expected_receive_num: int, auto_remove: bool):
|
|
5
9
|
'''
|
|
6
10
|
- Args
|
|
7
11
|
|
|
@@ -9,10 +13,13 @@ class Receiver:
|
|
|
9
13
|
|
|
10
14
|
- auto_remove: 是否自动删除响应次数超出期望次数的接收器。
|
|
11
15
|
'''
|
|
16
|
+
|
|
17
|
+
self._signal: 'Signal' = signal
|
|
12
18
|
self.fn: Callable = fn
|
|
13
19
|
self._expected_receive_num: int = expected_receive_num
|
|
14
20
|
self._auto_remove: bool = auto_remove
|
|
15
21
|
self._total_receive_num: int = 0
|
|
22
|
+
self._active: bool = True
|
|
16
23
|
|
|
17
24
|
def reset(self):
|
|
18
25
|
'''
|
|
@@ -27,6 +34,8 @@ class Receiver:
|
|
|
27
34
|
def expected_receive_num(self) -> int:
|
|
28
35
|
'''
|
|
29
36
|
期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
|
|
37
|
+
|
|
38
|
+
设置值小于`total_receive_num`且`auto_remove is True`,则会立刻删除。
|
|
30
39
|
'''
|
|
31
40
|
|
|
32
41
|
return self._expected_receive_num
|
|
@@ -35,10 +44,15 @@ class Receiver:
|
|
|
35
44
|
def expected_receive_num(self, value: int):
|
|
36
45
|
self._expected_receive_num = value
|
|
37
46
|
|
|
47
|
+
if self._auto_remove and self.is_expired:
|
|
48
|
+
self._signal.receivers.remove(self)
|
|
49
|
+
|
|
38
50
|
@property
|
|
39
51
|
def auto_remove(self) -> bool:
|
|
40
52
|
'''
|
|
41
53
|
是否自动删除响应次数超出期望次数的接收器。
|
|
54
|
+
|
|
55
|
+
设置为`True`时若该receiver过期,则会立刻删除。
|
|
42
56
|
'''
|
|
43
57
|
|
|
44
58
|
return self._auto_remove
|
|
@@ -47,6 +61,21 @@ class Receiver:
|
|
|
47
61
|
def auto_remove(self, value: bool):
|
|
48
62
|
self._auto_remove = value
|
|
49
63
|
|
|
64
|
+
if self._auto_remove and self.is_expired:
|
|
65
|
+
self._signal.receivers.remove(self)
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def active(self) -> bool:
|
|
69
|
+
'''
|
|
70
|
+
是否激活;未激活将忽略信号。
|
|
71
|
+
'''
|
|
72
|
+
|
|
73
|
+
return self._active
|
|
74
|
+
|
|
75
|
+
@active.setter
|
|
76
|
+
def active(self, value: bool):
|
|
77
|
+
self._active = value
|
|
78
|
+
|
|
50
79
|
@property
|
|
51
80
|
def total_receive_num(self) -> int:
|
|
52
81
|
'''
|
|
@@ -66,22 +95,22 @@ class Receiver:
|
|
|
66
95
|
return self.expected_receive_num - self.total_receive_num
|
|
67
96
|
|
|
68
97
|
@property
|
|
69
|
-
def
|
|
98
|
+
def is_expired(self) -> bool:
|
|
70
99
|
'''
|
|
71
|
-
【只读】
|
|
100
|
+
【只读】 是否过期。
|
|
72
101
|
'''
|
|
73
102
|
|
|
74
|
-
|
|
75
|
-
return True
|
|
76
|
-
return True if self.remaining_receive_num else False
|
|
103
|
+
return not self.is_unexpired
|
|
77
104
|
|
|
78
105
|
@property
|
|
79
|
-
def
|
|
106
|
+
def is_unexpired(self) -> bool:
|
|
80
107
|
'''
|
|
81
|
-
【只读】
|
|
108
|
+
【只读】 是否未过期。
|
|
82
109
|
'''
|
|
83
110
|
|
|
84
|
-
|
|
111
|
+
if self.remaining_receive_num == -1:
|
|
112
|
+
return True
|
|
113
|
+
return True if self.remaining_receive_num else False
|
|
85
114
|
|
|
86
115
|
class Signal:
|
|
87
116
|
def __init__(self):
|
|
@@ -154,7 +183,7 @@ class Signal:
|
|
|
154
183
|
if receiver.fn == fn:
|
|
155
184
|
raise ValueError('已有重复的函数接收器')
|
|
156
185
|
|
|
157
|
-
self.receivers.append(Receiver(fn, expected_receive_num = expected_receive_num, auto_remove = auto_remove))
|
|
186
|
+
self.receivers.append(Receiver(self, fn, expected_receive_num = expected_receive_num, auto_remove = auto_remove))
|
|
158
187
|
|
|
159
188
|
def send(self, *args, **kwargs) -> List[Any]:
|
|
160
189
|
'''
|
|
@@ -175,11 +204,11 @@ class Signal:
|
|
|
175
204
|
|
|
176
205
|
results = []
|
|
177
206
|
for receiver in self.receivers[:]:
|
|
178
|
-
if receiver.
|
|
207
|
+
if receiver.active and receiver.is_unexpired:
|
|
179
208
|
receiver._total_receive_num += 1
|
|
180
209
|
results.append(receiver.fn(*args, **kwargs))
|
|
181
210
|
|
|
182
|
-
if receiver.
|
|
211
|
+
if receiver.is_expired:
|
|
183
212
|
self.receivers.remove(receiver)
|
|
184
213
|
return results
|
|
185
214
|
|
|
@@ -207,11 +236,11 @@ class Signal:
|
|
|
207
236
|
|
|
208
237
|
results = []
|
|
209
238
|
for receiver in self.receivers[:]:
|
|
210
|
-
if receiver.
|
|
239
|
+
if receiver.active and receiver.is_unexpired:
|
|
211
240
|
receiver._total_receive_num += 1
|
|
212
241
|
results.append(await receiver.fn(*args, **kwargs))
|
|
213
242
|
|
|
214
|
-
if receiver.
|
|
243
|
+
if receiver.is_expired:
|
|
215
244
|
self.receivers.remove(receiver)
|
|
216
245
|
return results
|
|
217
246
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: CheeseSignal
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.1.0
|
|
4
4
|
Summary: 一款简约的信号系统。
|
|
5
5
|
Project-URL: Source, https://github.com/CheeseUnknown/CheeseSignal
|
|
6
6
|
Author-email: Cheese Unknown <cheese@cheese.ren>
|
|
@@ -192,7 +192,7 @@ print(signal.index(receiver))
|
|
|
192
192
|
from CheeseSignal import Receiver
|
|
193
193
|
```
|
|
194
194
|
|
|
195
|
-
#### **`def __init__(self, fn: Callable, *, expected_receive_num: int, auto_remove: bool)`**
|
|
195
|
+
#### **`def __init__(self, signal: Signal, fn: Callable, *, expected_receive_num: int, auto_remove: bool)`**
|
|
196
196
|
|
|
197
197
|
- **参数**
|
|
198
198
|
|
|
@@ -204,10 +204,18 @@ from CheeseSignal import Receiver
|
|
|
204
204
|
|
|
205
205
|
期望接受信号的次数,超过该次数则不再响应信号;0为无限次。
|
|
206
206
|
|
|
207
|
+
设置值小于`total_receive_num`且`auto_remove is True`,则会立刻删除。
|
|
208
|
+
|
|
207
209
|
#### **`self.auto_remove: bool`**
|
|
208
210
|
|
|
209
211
|
是否自动删除响应次数超出期望次数的接收器。
|
|
210
212
|
|
|
213
|
+
设置为`True`且`is_expired is True`,则会立刻删除。
|
|
214
|
+
|
|
215
|
+
#### **`self.active: bool`**
|
|
216
|
+
|
|
217
|
+
是否激活;未激活将忽略信号。
|
|
218
|
+
|
|
211
219
|
#### **`self.total_receive_num: int`**
|
|
212
220
|
|
|
213
221
|
【只读】 总计信号接受次数。
|
|
@@ -216,13 +224,13 @@ from CheeseSignal import Receiver
|
|
|
216
224
|
|
|
217
225
|
【只读】 剩余的期望信号接受次数;返回为-1代表无期望信号接受次数。
|
|
218
226
|
|
|
219
|
-
#### **`self.
|
|
227
|
+
#### **`self.is_expired: bool`**
|
|
220
228
|
|
|
221
|
-
【只读】
|
|
229
|
+
【只读】 是否过期。
|
|
222
230
|
|
|
223
|
-
#### **`self.
|
|
231
|
+
#### **`self.is_unexpired: bool`**
|
|
224
232
|
|
|
225
|
-
【只读】
|
|
233
|
+
【只读】 是否未过期。
|
|
226
234
|
|
|
227
235
|
#### **`def reset(self)`**
|
|
228
236
|
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
CheeseSignal/__init__.py,sha256=UNrN77xeVfj6YkPgPKjgZo9DjodL_uJ_mW6EjTKrLHM,9397
|
|
2
|
+
cheesesignal-1.1.0.dist-info/METADATA,sha256=XgScoe7qZ45H2-zhXWupDgI-YohJmqNLpDwx2kwf1ic,5146
|
|
3
|
+
cheesesignal-1.1.0.dist-info/WHEEL,sha256=cDcbFFSNXOE-241I5PFuLkIYfR_FM7WTlPEi33njInY,105
|
|
4
|
+
cheesesignal-1.1.0.dist-info/licenses/LICENSE,sha256=5vFb3i4UDlskszJ3jGPh8bXrM_axJfDRRuvLu1M3bIs,1070
|
|
5
|
+
cheesesignal-1.1.0.dist-info/RECORD,,
|
|
@@ -1,5 +0,0 @@
|
|
|
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,,
|
|
File without changes
|