thiiiiings

This commit is contained in:
Jonas Zeunert
2020-02-13 19:05:55 +01:00
parent 82f1ad6ed8
commit 7414734dad
719 changed files with 41551 additions and 227 deletions

View File

@@ -0,0 +1,124 @@
# observable
[![Build Status](https://travis-ci.com/timofurrer/observable.svg?branch=master)](https://travis-ci.com/timofurrer/observable)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
**pyobservable** is a minimalist event system for python. It provides you an easy-to-use interface to trigger arbitrary functions when specific events occur.
```python
from observable import Observable
obs = Observable()
@obs.on("error")
def error_handler(message):
# do some fancy error handling
logging.error(f"An error occured: {message}")
...
def do_time_travel():
# do some time traveling
...
if year != 1291:
obs.trigger("error", "Time travel to 1291 didn't work")
```
**Note:** We are Python 3 only! Only Python Versions >= 3.5 are supported. Use [v0.3.2](https://pypi.org/project/observable/0.3.2/) for older Python Versions.
## How to use
Use a `pip` to install it from PyPI:
pip install observable
After completion you can start using `observable`:
```python
from observable import Observable
obs = Observable()
```
## Usage
### `on`: Register event handler with `on`
There are two ways to register a function to an event.<br />
The first way is to register the event with a decorator like this:
```python
@obs.on("error")
def error_func(message):
print("Error: %s" % message)
```
The second way is to register it with a method call:
```python
def error_func(message):
print("Error: %s" % message)
obs.on("error", error_func)
```
### `once`: Register event handler with `once`
`once` works like `on`, but once the event handler is triggered it will be removed and cannot be triggered again.
### `trigger`: trigger event
You can trigger a registered event with the `trigger` method:
```python
obs.trigger("error", "This is my error message")
```
If no handler for the event `error` could be found an `Observable.NoHandlerFound`-Exception will be raised.
### `off`: remove handler and events
Remove a handler from a specified event:
```python
obs.off("error", error_func)
```
```python
obs.off("error", [error_func, second_error_func])
```
Remove all handlers from a specified event:
```python
obs.off("error")
```
Clear all events:
```python
obs.off()
```
### `get_all_handlers`, `get_handlers` and `is_registered`: Check which handlers are registered
Imagine you registered the following handlers:
```python
@obs.on("success")
def success_func():
print("Success!")
@obs.on("error")
def error_func(message):
print("Error: %s" % message)
```
Then you can do the following to inspect the registered handlers:
```python
>>> obs.get_all_handlers()
{'success': [<function success_func at 0x7f7f32d0a1e0>], 'error': [<function error_func at 0x7f7f32d0a268>]}
>>> obs.get_handlers("success")
[<function success_func at 0x7f7f32d0a1e0>]
>>> obs.get_handlers("other_event")
[]
```
***
*<p align="center">This project is published under [MIT](LICENSE).<br>A [Timo Furrer](https://tuxtimo.me) project.<br>- :tada: -</p>*

View File

@@ -0,0 +1,147 @@
Metadata-Version: 2.0
Name: observable
Version: 1.0.3
Summary: minimalist event system
Home-page: https://github.com/timofurrer/observable
Author: Timo Furrer
Author-email: tuxtimo@gmail.com
License: MIT
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Description-Content-Type: text/markdown
# observable
[![Build Status](https://travis-ci.com/timofurrer/observable.svg?branch=master)](https://travis-ci.com/timofurrer/observable)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)
**pyobservable** is a minimalist event system for python. It provides you an easy-to-use interface to trigger arbitrary functions when specific events occur.
```python
from observable import Observable
obs = Observable()
@obs.on("error")
def error_handler(message):
# do some fancy error handling
logging.error(f"An error occured: {message}")
...
def do_time_travel():
# do some time traveling
...
if year != 1291:
obs.trigger("error", "Time travel to 1291 didn't work")
```
**Note:** We are Python 3 only! Only Python Versions >= 3.5 are supported. Use [v0.3.2](https://pypi.org/project/observable/0.3.2/) for older Python Versions.
## How to use
Use a `pip` to install it from PyPI:
pip install observable
After completion you can start using `observable`:
```python
from observable import Observable
obs = Observable()
```
## Usage
### `on`: Register event handler with `on`
There are two ways to register a function to an event.<br />
The first way is to register the event with a decorator like this:
```python
@obs.on("error")
def error_func(message):
print("Error: %s" % message)
```
The second way is to register it with a method call:
```python
def error_func(message):
print("Error: %s" % message)
obs.on("error", error_func)
```
### `once`: Register event handler with `once`
`once` works like `on`, but once the event handler is triggered it will be removed and cannot be triggered again.
### `trigger`: trigger event
You can trigger a registered event with the `trigger` method:
```python
obs.trigger("error", "This is my error message")
```
If no handler for the event `error` could be found an `Observable.NoHandlerFound`-Exception will be raised.
### `off`: remove handler and events
Remove a handler from a specified event:
```python
obs.off("error", error_func)
```
```python
obs.off("error", [error_func, second_error_func])
```
Remove all handlers from a specified event:
```python
obs.off("error")
```
Clear all events:
```python
obs.off()
```
### `get_all_handlers`, `get_handlers` and `is_registered`: Check which handlers are registered
Imagine you registered the following handlers:
```python
@obs.on("success")
def success_func():
print("Success!")
@obs.on("error")
def error_func(message):
print("Error: %s" % message)
```
Then you can do the following to inspect the registered handlers:
```python
>>> obs.get_all_handlers()
{'success': [<function success_func at 0x7f7f32d0a1e0>], 'error': [<function error_func at 0x7f7f32d0a268>]}
>>> obs.get_handlers("success")
[<function success_func at 0x7f7f32d0a1e0>]
>>> obs.get_handlers("other_event")
[]
```
***
*<p align="center">This project is published under [MIT](LICENSE).<br>A [Timo Furrer](https://tuxtimo.me) project.<br>- :tada: -</p>*

View File

@@ -0,0 +1,17 @@
observable-1.0.3.dist-info/DESCRIPTION.rst,sha256=ovXHS1efvthrNOKpH0JSFkxwgpAbFo1-BiHaos3Z90g,3057
observable-1.0.3.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
observable-1.0.3.dist-info/METADATA,sha256=fNl6JAlRoKVUcY7chrtftOqHH__hr7BiVBAHavcY5dE,3962
observable-1.0.3.dist-info/RECORD,,
observable-1.0.3.dist-info/WHEEL,sha256=kdsN-5OJAZIiHN-iO4Rhl82KyS0bDWf4uBwMbkNafr8,110
observable-1.0.3.dist-info/metadata.json,sha256=q8-F57T7Zhy3IGSEj795E7ni1dtlYDdRjA1uoQbO46Y,1005
observable-1.0.3.dist-info/top_level.txt,sha256=47luK7ZGvZgqf2u4sBjIO-HhTwR2ND-BR_LXVn0FjZ4,17
observable/__init__.py,sha256=3cKuWeS1ZLcjlx3nx43-3QKCBCyXn5kbaJ1KYwwwiqs,185
observable/__pycache__/__init__.cpython-38.pyc,,
observable/__pycache__/__version__.cpython-38.pyc,,
observable/__pycache__/core.cpython-38.pyc,,
observable/__version__.py,sha256=xa7LsbURY2yRXjuH5GR6U7DQ4hQJdaKPezzeLznkBCM,87
observable/core.py,sha256=A7pxabHTVM-_1T_V7mBal42xbuM0CMVblkDN1eknsIg,4286
tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
tests/__pycache__/__init__.cpython-38.pyc,,
tests/__pycache__/test_core.cpython-38.pyc,,
tests/test_core.py,sha256=DwYXB4ALlXGzGhL9IwFN_hS32xAsgnD7t9HJNomdmmE,7218

View File

@@ -0,0 +1,6 @@
Wheel-Version: 1.0
Generator: bdist_wheel (0.30.0)
Root-Is-Purelib: true
Tag: py2-none-any
Tag: py3-none-any

View File

@@ -0,0 +1 @@
{"classifiers": ["Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: Implementation", "Programming Language :: Python :: Implementation :: CPython", "Topic :: Software Development :: Libraries :: Python Modules"], "description_content_type": "text/markdown", "extensions": {"python.details": {"contacts": [{"email": "tuxtimo@gmail.com", "name": "Timo Furrer", "role": "author"}], "document_names": {"description": "DESCRIPTION.rst"}, "project_urls": {"Home": "https://github.com/timofurrer/observable"}}}, "generator": "bdist_wheel (0.30.0)", "license": "MIT", "metadata_version": "2.0", "name": "observable", "summary": "minimalist event system", "version": "1.0.3"}

View File

@@ -0,0 +1,2 @@
observable
tests