Skip to main content

一款简约的信号系统。

Project description

CheeseSignal

一款简单的信号系统。

安装

系统要求:Linux。

Python要求:目前仅保证支持3.11及以上版本的Python,新版本会优先支持Python的最新稳定版本。

pip install CheeseSignal

使用

使用方式较为简单,通过函数或装饰件连接至某个信号,随后等待信号发送。

import time

from CheeseSignal import signal

signal = Signal()

def receiver1(*args, **kwargs):
    print(arg, kwargs)
signal.connect(receiver1)

@signal.connect()
def receiver2(*args, **kwargs):
    return args, kwargs

while True:
    print(signal.send('Hello', 'World', {
        'Cheese': 'Signal'
    }))
    time.sleep(1)

接口文档

class Signal

from CheeseSignal import Signal

signal = Signal()

self.receivers: List[Receiver]

【只读】 接收器的列表,按注册顺序排序。

self.total_send_num: int

【只读】 总计信号发送次数。

def connect(self, fn: Callable, *, expected_receive_num: int = 0, auto_remove: bool = False)

通过函数注册响应函数。

  • 参数

    • expected_receive_num: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。

    • auto_remove: 是否自动删除响应次数超出期望次数的接收器。

  • 报错

    • ValueError: 已有重复的函数接收器。

def connect(self, *, expected_receive_num: int = 0, auto_remove: bool = False)

通过装饰器注册响应函数。

  • 参数

    • expected_receive_num: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。

    • auto_remove: 是否自动删除响应次数超出期望次数的接收器。

  • 报错

    • ValueError: 已有重复的函数接收器。

def send(self, *args, **kwargs) -> List[Any]

发送信号。

async def async_send(self, *args, **kwargs) -> List[Any]

在协程环境中发送信号,并请保证所有接收函数都是协程函数。

import asyncio

from CheeseSignal import Signal

async def run_asyncio():
    signal = Signal()
    await signal.async_send('data1', 'data2', **{
        'key1': 'value1',
        'key2': 'value2'
    })

asyncio.run(run_asyncio())

def disconnect(self, fn: Callable)

断开接收器。

from CheeseSignal import Signal

def receiver(*args, **kwargs):
    ...

signal = Signal()
signal.connect(receiver)
signal.disconnect(receiver)
  • 报错

    • ValueError: 未找到该函数的接收器。

def reset(self)

重置统计数据;所有的接收器的统计数据也会同步清空。

def disconnect_all(self)

断开所有接收器。

def get_receiver(self, fn: Callable) -> Receiver

获取接收器。

from CheeseSignal import Signal

def receiver(*args, **kwargs):
    ...

signal = Signal()
signal.connect(receiver)

print(signal.get_receiver(receiver))
  • 报错

    • ValueError: 未找到该函数的接收器。

def index(self, fn: Callable) -> index

获取接收器的顺序位置。

from CheeseSignal import Signal

def receiver(*args, **kwargs):
    ...

signal = Signal()
signal.connect(receiver)

print(signal.index(receiver))
  • 报错

    • ValueError: 未找到该函数的接收器。

class Receiver

正常使用并不需要手动创建该类。

from CheeseSignal import Receiver

def __init__(self, signal: Signal, fn: Callable, *, expected_receive_num: int, auto_remove: bool)

  • 参数

    • expected_receive_num: 期望接受信号的次数,超过该次数则不再响应信号;0为无限次。

    • auto_remove: 是否自动删除响应次数超出期望次数的接收器。

self.expected_receive_num: int

期望接受信号的次数,超过该次数则不再响应信号;0为无限次。

设置值小于total_receive_numauto_remove is True,则会立刻删除。

self.auto_remove: bool

是否自动删除响应次数超出期望次数的接收器。

设置为Trueis_expired is True,则会立刻删除。

self.active: bool

是否激活;未激活将忽略信号。

self.total_receive_num: int

【只读】 总计信号接受次数。

self.remaining_receive_num: int

【只读】 剩余的期望信号接受次数;返回为-1代表无期望信号接受次数。

self.is_expired: bool

【只读】 是否过期。

self.is_unexpired: bool

【只读】 是否未过期。

def reset(self)

重统计置数据。

在有期望信号接受次数的情况下,auto_remove is False的接收器会重新开始计数并接收信号。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cheesesignal-1.1.0.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cheesesignal-1.1.0-py2.py3-none-any.whl (5.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file cheesesignal-1.1.0.tar.gz.

File metadata

  • Download URL: cheesesignal-1.1.0.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for cheesesignal-1.1.0.tar.gz
Algorithm Hash digest
SHA256 37d756c7fc28d5696d8108c0059340b1e1d57d7565329712525428a220bb1401
MD5 6851047dd4aed35c5f41c6463b75542f
BLAKE2b-256 1c6d2f088d85b8690715fe5cc8e586d32f5fa90546f0dd77ed2d45758c60d2fb

See more details on using hashes here.

File details

Details for the file cheesesignal-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: cheesesignal-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for cheesesignal-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c2896d5faf25c8d28bcf4f9ec238088ba701fd526f615578fd22947a4b462532
MD5 a99174c420e8d68319ea40de295ffec2
BLAKE2b-256 182b3a95c49461d8a4308273958bf30001a26f0779f316b4e535bc8cd0528793

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page