• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created almost 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python port of the extended Node.js EventEmitter 2 approach providing namespaces, wildcards and TTL.

pymitter

Package status License Lint and test Code coverage PyPI downloads

Python port of the extended Node.js EventEmitter 2 approach of https://github.com/asyncly/EventEmitter2 providing namespaces, wildcards and TTL.

Features

  • Namespaces with wildcards
  • Times to listen (TTL)
  • Usage via decorators or callbacks
  • Coroutine support
  • Lightweight implementation, good performance

Installation

pymitter is a registered PyPI module, so the installation with pip is quite easy:

pip install pymitter

The last version with Python 2 support was v0.3.2 (PyPI).

Examples

Basic usage

from pymitter import EventEmitter


ee = EventEmitter()


# decorator usage
@ee.on("my_event")
def handler1(arg):
    print("handler1 called with", arg)


# callback usage
def handler2(arg):
    print("handler2 called with", arg)


ee.on("my_other_event", handler2)


# support for coroutine functions
@ee.on("my_third_event")
async def handler3(arg):
    print("handler3 called with", arg)


# emit
ee.emit("my_event", "foo")
# -> "handler1 called with foo"

ee.emit("my_other_event", "bar")
# -> "handler2 called with bar"

ee.emit("my_third_event", "baz")
# -> "handler3 called with baz"

Coroutines

Wrapping async functions outside an event loop will start an internal event loop and calls to emit return synchronously.

from pymitter import EventEmitter


ee = EventEmitter()


# register an async function
@ee.on("my_event")
async def handler1(arg):
    print("handler1 called with", arg)


# emit
ee.emit("my_event", "foo")
# -> "handler1 called with foo"

Wrapping async functions inside an event loop will use the same loop and emit_async is awaitable.

from pymitter import EventEmitter


ee = EventEmitter()


async def main():
    # emit_async
    awaitable = ee.emit_async("my_event", "foo")
    # -> nothing printed yet

    await awaitable
    # -> "handler1 called with foo"

TTL (times to listen)

from pymitter import EventEmitter


ee = EventEmitter()


@ee.once("my_event")
def handler1():
    print("handler1 called")


@ee.on("my_event", ttl=2)
def handler2():
    print("handler2 called")


ee.emit("my_event")
# -> "handler1 called"
# -> "handler2 called"

ee.emit("my_event")
# -> "handler2 called"

ee.emit("my_event")
# nothing called anymore

Wildcards

from pymitter import EventEmitter


ee = EventEmitter(wildcard=True)


@ee.on("my_event.foo")
def handler1():
    print("handler1 called")


@ee.on("my_event.bar")
def handler2():
    print("handler2 called")


@ee.on("my_event.*")
def hander3():
    print("handler3 called")


ee.emit("my_event.foo")
# -> "handler1 called"
# -> "handler3 called"

ee.emit("my_event.bar")
# -> "handler2 called"
# -> "handler3 called"

ee.emit("my_event.*")
# -> "handler1 called"
# -> "handler2 called"
# -> "handler3 called"

API

EventEmitter(*, wildcard=False, delimiter=".", new_listener=False, max_listeners=-1)

EventEmitter constructor. Note: always use kwargs for configuration. When wildcard is True, wildcards are used as shown in this example. delimiter is used to seperate namespaces within events. If new_listener is True, the "new_listener" event is emitted every time a new listener is registered. Functions listening to this event are passed (func, event=None). max_listeners defines the maximum number of listeners per event. Negative values mean infinity.

  • on(event, func=None, ttl=-1)

    Registers a function to an event. When func is None, decorator usage is assumed. ttl defines the times to listen. Negative values mean infinity. Returns the function.

  • once(event, func=None)

    Registers a function to an event with ttl = 1. When func is None, decorator usage is assumed. Returns the function.

  • on_any(func=None)

    Registers a function that is called every time an event is emitted. When func is None, decorator usage is assumed. Returns the function.

  • off(event, func=None)

    Removes a function that is registered to an event. When func is None, decorator usage is assumed. Returns the function.

  • off_any(func=None)

    Removes a function that was registered via on_any(). When func is None, decorator usage is assumed. Returns the function.

  • off_all()

    Removes all functions of all events.

  • listeners(event)

    Returns all functions that are registered to an event. Wildcards are not applied.

  • listeners_any()

    Returns all functions that were registered using on_any().

  • listeners_all()

    Returns all registered functions.

  • emit(event, *args, **kwargs)

    Emits an event. All functions of events that match event are invoked with args and kwargs in the exact order of their registeration. Async functions are called in a new event loop. There is no return value.

  • (async) emit_async(event, *args, **kwargs)

    Emits an event. All functions of events that match event are invoked with args and kwargs in the exact order of their registeration. Async functions are called in the outer event loop. Returns an Awaitable.

Development

More Repositories

1

tfdeploy

Deploy tensorflow graphs for fast evaluation and export to tensorflow-less environments running numpy.
Python
352
star
2

law

Build large-scale task workflows: luigi + job submission + remote targets + environment sandboxing using Docker/Singularity
Python
74
star
3

jsonrpyc

Minimal python JSON-RPC 2.0 implementation in a single file.
Python
23
star
4

scinum

Scientific numbers with multiple uncertainties, correlation-aware gaussian error propagation and numpy support.
Python
22
star
5

jclass

Advanced but lightweight and fast Javascript inheritance model providing class members and prototype conversion.
JavaScript
15
star
6

rpyc-stream

A simple, one-file Python RPC System that is based on Streams allowing for cross-language/SSH usage.
Python
14
star
7

LBN

TensorFlow implementation of the Lorentz Boost Network (LBN).
Python
9
star
8

order

Pythonic class collection that helps you structure external data from LHC / HEP experiments.
Python
9
star
9

numphy

Physics objects backed by NumPy and/or TensorFlow.
Python
4
star
10

homectrl

Lightweight Plugin based Home Automation App for the Raspberry Pi using Nodejs, Socket.io and Bootstrap.
JavaScript
4
star
11

jquery.logger

A simple but powerful logging plugin for jQuery with namespaces.
JavaScript
3
star
12

CMSSW-DNN

DNN / Tensorflow Interface for CMSSW
C++
3
star
13

node-json-rpc

A simple and lightweight NodeJs JSON-RPC 2.0 implementation including object wrapping.
JavaScript
3
star
14

plotlib

Collection of plotting helpers for ROOT and matplotlib.
Python
3
star
15

mermaidmro

Create mermaid graphs from the method resolution order (mro) of Python objects.
Python
2
star
16

law_example_CMSSingleTopAnalysis

SingleTop Analysis with Public CMS Data using luigi analysis workflows
Python
2
star
17

cms-hlt-parser

Read, parse and provide CERN CMS High-Level Trigger and Luminosity information.
Python
1
star
18

aws-setup

A Node Module and Command Line Tool for initializing AWS setups via JSON, YAML or JS scripts.
JavaScript
1
star
19

jquery.shortcuts

jQuery Shortcuts lets you easily switch between sets of arbitrary and easy-to-define shortcuts, including namespaces (especially for use in desktop-like webapps).
JavaScript
1
star
20

jquery.history

jQuery History connects your history object with jQuery.Callbacks() to provide a low-level (Github-like) paging w/o reloads.
JavaScript
1
star
21

CMSSW-DNNShowCase

Proof-of-concept showing how DNNs can be used within CMSSW using tfdeploy
Python
1
star
22

docker-images

Docker images with different python software stacks.
1
star
23

node-picam

A simple Node interface to the Raspberry Pi Camera Modul
JavaScript
1
star
24

jquery.ctabs

A plugin that implements chrome-like tabs in your browser.
JavaScript
1
star