最小事件系统
observable的Python项目详细描述
#可观测的[构建状态](https://travis-ci.com/timofurrer/observate.svg?branch=master)(https://travis ci.com/timofurrer/observate)
[![代码风格:黑色](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
**pyobservate**是一个针对python的极简事件系统。它提供了一个易于使用的界面,在特定事件发生时触发任意函数。
``python
from observate import observate
@obs.on(“error”)
def error\u handler(message):
{消息}“
…
def do_time_travel():
=1291:
obs.trigger(“error”,“time travel to 1291 didn't work”)
````
**注意:**我们只是python 3!仅支持python版本>;=3.5。对于较旧的python版本,请使用[v0.3.2](https://pypi.org/project/observate/0.3.2/)。
=observate()
```
第一种方法是用这样的修饰程序注册事件:
``python
@obs.on(“error”)
def error\u func(message):
print(“error:%s%”消息)
````
print(“error:
``python
``def error\u func(message):
print(“error:%s”%消息)
obs.on(“error”,error_func)
````
` once`:使用“once”
“once”注册事件处理程序,其工作方式与“on”类似,但是,一旦触发事件处理程序,它将被删除,并且不能再次触发。
“这是我的错误消息”)
````
>如果找不到“error”事件的任何处理程序,则会引发“obobobservable.nohandlernfound”异常。
//35 35 35 35 35 ` off `:删除处理程序和事件
>从指定事件中删除处理程序:
错误“,[错误函数,第二个错误函数]
```
` get_handlers`和'is_registered':检查注册了哪些处理程序
假设您注册了以下处理程序:
`` python
@obs.on(“success”)
def success_func():
print(“success!”)
@obs.on(“error”)
def error-func(message):
print(“error:%s”%message)
``````
>然后您可以执行以下操作来检查已注册的处理程序:
```python
>;>;obs.get-u all-handlers()
{success':[<;function success-func at 0x7f32d0a1e0>;],“error”:[<;0x7f7f7f3212d0a268>;]}
>>;>gt>gt;obs.getu处理函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数br>;一个[timo furrer]项目(https://tuxtimo.me)。lt;br>;-:tada:-<;/p>;*
[![代码风格:黑色](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
**pyobservate**是一个针对python的极简事件系统。它提供了一个易于使用的界面,在特定事件发生时触发任意函数。
``python
from observate import observate
@obs.on(“error”)
def error\u handler(message):
{消息}“
…
def do_time_travel():
=1291:
obs.trigger(“error”,“time travel to 1291 didn't work”)
````
**注意:**我们只是python 3!仅支持python版本>;=3.5。对于较旧的python版本,请使用[v0.3.2](https://pypi.org/project/observate/0.3.2/)。
=observate()
```
第一种方法是用这样的修饰程序注册事件:
``python
@obs.on(“error”)
def error\u func(message):
print(“error:%s%”消息)
````
print(“error:
``python
``def error\u func(message):
print(“error:%s”%消息)
obs.on(“error”,error_func)
````
` once`:使用“once”
“once”注册事件处理程序,其工作方式与“on”类似,但是,一旦触发事件处理程序,它将被删除,并且不能再次触发。
“这是我的错误消息”)
````
>如果找不到“error”事件的任何处理程序,则会引发“obobobservable.nohandlernfound”异常。
//35 35 35 35 35 ` off `:删除处理程序和事件
>从指定事件中删除处理程序:
错误“,[错误函数,第二个错误函数]
```
` get_handlers`和'is_registered':检查注册了哪些处理程序
假设您注册了以下处理程序:
`` python
@obs.on(“success”)
def success_func():
print(“success!”)
@obs.on(“error”)
def error-func(message):
print(“error:%s”%message)
``````
>然后您可以执行以下操作来检查已注册的处理程序:
```python
>;>;obs.get-u all-handlers()
{success':[<;function success-func at 0x7f32d0a1e0>;],“error”:[<;0x7f7f7f3212d0a268>;]}
>>;>gt>gt;obs.getu处理函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数函数br>;一个[timo furrer]项目(https://tuxtimo.me)。lt;br>;-:tada:-<;/p>;*