Skip to content

Commit 95e75e3

Browse files
committed
python-ecosys/pymitter: Added legacy branch from the original project.
Signed-off-by: Oliver Maye <maye@ihp-microelectronics.com>
1 parent e4cf095 commit 95e75e3

File tree

10 files changed

+749
-0
lines changed

10 files changed

+749
-0
lines changed

python-ecosys/pymitter/LICENSE

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Copyright (c) 2014-2021, Marcel Rieger
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above copyright notice,
11+
this list of conditions and the following disclaimer in the documentation
12+
and/or other materials provided with the distribution.
13+
14+
* Neither the name of the copyright holder nor the names of its contributors
15+
may be used to endorse or promote products derived from this software without
16+
specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

python-ecosys/pymitter/MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include pymitter.py setup.py requirements.txt README.md LICENSE .flake8
2+
global-exclude *.py[cod] __pycache__

python-ecosys/pymitter/README.md

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# pymitter
2+
3+
This is a fork of the [original pymitter project](https://pypi.org/project/pymitter/) by Marcel Rieger.
4+
Sources are from the legacy/py2 branch which is a frozen v0.3.2 of that project.
5+
At this state, the implementation is compatible to Python >= v2.7 including
6+
MicroPython with a language level v3.4.
7+
8+
Later versions of that project make use of type hints, which were introduced
9+
in Python 3.5. Type hints are currently not supported by MicroPython.
10+
11+
12+
## Features
13+
14+
- Namespaces with wildcards
15+
- Times to listen (TTL)
16+
- Usage via decorators or callbacks
17+
- Lightweight implementation, good performance
18+
19+
20+
## Installation
21+
22+
*pymitter* is a registered [MicroPython module](https://github.com/olimaye/micropython-lib),
23+
so the installation with *mip* is quite easy:
24+
25+
```console
26+
mpremote mip install pymitter
27+
```
28+
29+
30+
## Examples
31+
32+
### Basic usage
33+
34+
```python
35+
from pymitter import EventEmitter
36+
37+
38+
ee = EventEmitter()
39+
40+
41+
# decorator usage
42+
@ee.on("myevent")
43+
def handler1(arg):
44+
print("handler1 called with", arg)
45+
46+
47+
# callback usage
48+
def handler2(arg):
49+
print("handler2 called with", arg)
50+
51+
52+
ee.on("myotherevent", handler2)
53+
54+
55+
# emit
56+
ee.emit("myevent", "foo")
57+
# -> "handler1 called with foo"
58+
59+
ee.emit("myotherevent", "bar")
60+
# -> "handler2 called with bar"
61+
```
62+
63+
64+
### TTL (times to listen)
65+
66+
```python
67+
from pymitter import EventEmitter
68+
69+
70+
ee = EventEmitter()
71+
72+
73+
@ee.once("myevent")
74+
def handler1():
75+
print("handler1 called")
76+
77+
78+
@ee.on("myevent", ttl=10)
79+
def handler2():
80+
print("handler2 called")
81+
82+
83+
ee.emit("myevent")
84+
# -> "handler1 called"
85+
# -> "handler2 called"
86+
87+
ee.emit("myevent")
88+
# -> "handler2 called"
89+
```
90+
91+
92+
### Wildcards
93+
94+
```python
95+
from pymitter import EventEmitter
96+
97+
98+
ee = EventEmitter(wildcard=True)
99+
100+
101+
@ee.on("myevent.foo")
102+
def handler1():
103+
print("handler1 called")
104+
105+
106+
@ee.on("myevent.bar")
107+
def handler2():
108+
print("handler2 called")
109+
110+
111+
@ee.on("myevent.*")
112+
def hander3():
113+
print("handler3 called")
114+
115+
116+
ee.emit("myevent.foo")
117+
# -> "handler1 called"
118+
# -> "handler3 called"
119+
120+
ee.emit("myevent.bar")
121+
# -> "handler2 called"
122+
# -> "handler3 called"
123+
124+
ee.emit("myevent.*")
125+
# -> "handler1 called"
126+
# -> "handler2 called"
127+
# -> "handler3 called"
128+
```
129+
130+
## API
131+
132+
133+
### ``EventEmitter(wildcard=False, delimiter=".", new_listener=False, max_listeners=-1)``
134+
135+
EventEmitter constructor. **Note**: always use *kwargs* for configuration. When *wildcard* is
136+
*True*, wildcards are used as shown in [this example](#wildcards). *delimiter* is used to seperate
137+
namespaces within events. If *new_listener* is *True*, the *"new_listener"* event is emitted every
138+
time a new listener is registered. Functions listening to this event are passed
139+
``(func, event=None)``. *max_listeners* defines the maximum number of listeners per event. Negative
140+
values mean infinity.
141+
142+
- #### ``on(event, func=None, ttl=-1)``
143+
Registers a function to an event. When *func* is *None*, decorator usage is assumed. *ttl*
144+
defines the times to listen. Negative values mean infinity. Returns the function.
145+
146+
- #### ``once(event, func=None)``
147+
Registers a function to an event with ``ttl = 1``. When *func* is *None*, decorator usage is
148+
assumed. Returns the function.
149+
150+
- #### ``on_any(func=None)``
151+
Registers a function that is called every time an event is emitted. When *func* is *None*,
152+
decorator usage is assumed. Returns the function.
153+
154+
- #### ``off(event, func=None)``
155+
Removes a function that is registered to an event. When *func* is *None*, decorator usage is
156+
assumed. Returns the function.
157+
158+
- #### ``off_any(func=None)``
159+
Removes a function that was registered via ``on_any()``. When *func* is *None*, decorator usage
160+
is assumed. Returns the function.
161+
162+
- #### ``off_all()``
163+
Removes all functions of all events.
164+
165+
- #### ``listeners(event)``
166+
Returns all functions that are registered to an event. Wildcards are not applied.
167+
168+
- #### ``listeners_any()``
169+
Returns all functions that were registered using ``on_any()``.
170+
171+
- #### ``listeners_all()``
172+
Returns all registered functions.
173+
174+
- #### ``emit(event, *args, **kwargs)``
175+
Emits an event. All functions of events that match *event* are invoked with *args* and *kwargs*
176+
in the exact order of their registeration. Wildcards might be applied.
177+
178+
179+
## Development
180+
181+
- Source hosted at [GitHub](https://github.com/riga/pymitter)
182+
- Python module hostet at [PyPI](https://pypi.python.org/pypi/pymitter)
183+
- Report issues, questions, feature requests on [GitHub Issues](https://github.com/riga/pymitter/issues)

python-ecosys/pymitter/examples.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# coding: utf-8
2+
3+
# python imports
4+
import os
5+
import sys
6+
from pymitter import EventEmitter
7+
8+
9+
# create an EventEmitter instance
10+
ee = EventEmitter(wildcard=True, new_listener=True, max_listeners=-1)
11+
12+
13+
@ee.on("new_listener")
14+
def on_new(func, event=None):
15+
print("added listener", event, func)
16+
17+
18+
@ee.on("foo")
19+
def handler_foo1(arg):
20+
print("foo handler 1 called with", arg)
21+
22+
23+
@ee.on("foo")
24+
def handler_foo2(arg):
25+
print("foo handler 2 called with", arg)
26+
27+
28+
@ee.on("foo.*", ttl=1)
29+
def handler_fooall(arg):
30+
print("foo.* handler called with", arg)
31+
32+
33+
@ee.on("foo.bar")
34+
def handler_foobar(arg):
35+
print("foo.bar handler called with", arg)
36+
37+
38+
@ee.on_any()
39+
def handler_any(*args, **kwargs):
40+
print("called every time")
41+
42+
43+
print("emit foo")
44+
ee.emit("foo", "test")
45+
print(10 * "-")
46+
47+
print("emit foo.bar")
48+
ee.emit("foo.bar", "test")
49+
print(10 * "-")
50+
51+
print("emit foo.*")
52+
ee.emit("foo.*", "test")
53+
print(10 * "-")

python-ecosys/pymitter/manifest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
metadata(
2+
description="Event subscription and publishing tools.",
3+
version="0.3.2",
4+
pypi="pymitter",
5+
)
6+
7+
module("pymitter.py")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy