A minor rewrite of the NodeJS EventEmitter in Python
Project description
node_events.py
A minor rewrite of the NodeJS EventEmitter in Python
Installing
Via PyPI:
pip install node_events
Usage
from node_events import EventEmitter
myEmitter = EventEmitter()
def fn():
print("An event occurred")
myEmitter.on('event', fn)
myEmitter.emit('event')
# Prints
# An event occurred
API
Class: EventEmitter
The EventEmitter class is defined and exposed publicly by the module:
from node_events import EventEmitter
EventEmitter.addListener(eventName, listener)
eventName: <string>listener: <function>- Returns: <EventEmitter>
Alias for self.on(eventName, listener)
Emits the 'addlistener:{eventName}' event when called.
EventEmitter.on(eventName, listener)
eventName: <string> The name of the event.listener: <function> The callback function.- Returns: <EventEmitter>
Appends the listener to the listeners array for the event named eventName. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
Emits the 'addlistener:{eventName}' event when called.
By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.
Returns a reference to the EventEmitter, so that calls can be chained.
emitter = EventEmitter()
def appendListener():
print('a')
def prependListener():
print('b')
emitter.on('test', appendListener)
emitter.prependListener('test', appendListener)
emitter.emit('test')
# Prints
# b
# a
EventEmitter.once(eventName, listener)
eventName: <string> The name of the event.listener: <function> The callback function.- Returns: <EventEmitter>
Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.
Emits the 'addlistener:{eventName}' event when called.
By default, event listeners are invoked in the order they are added. The emitter.prependOnceListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.
Returns a reference to the EventEmitter, so that calls can be chained.
emitter = EventEmitter()
def appendListener():
print('a')
def prependListener():
print('b')
emitter.once('test', appendListener)
emitter.prependOnceListener('test', appendListener)
emitter.emit('test')
# Prints
# b
# a
EventEmitter.prependListener(eventName, listener)
eventName: <string> The name of the event.listener: <function> The callback function.- Returns: <EventEmitter>
Adds the listener function to the beginning of the listeners array for the event named eventName. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.
Emits the 'addlistener:{eventName}' event when called.
emitter = EventEmitter()
def newConnection():
print('someone connected!')
emitter.prependListener('connection', newConnection)
emitter.emit('connection')
Returns a reference to the EventEmitter, so that calls can be chained.
EventEmitter.prependOnceListener(eventName, listener)
eventName: <string> The name of the event.listener: <function> The callback function.- Returns: <EventEmitter>
Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.
Emits the 'addlistener:{eventName}' event when called.
emitter = EventEmitter()
def newConnection():
print('someone connected!')
emitter.prependOnceListener('connection', newConnection)
emitter.emit('connection')
Returns a reference to the EventEmitter, so that calls can be chained.
EventEmitter.removeAllListeners([eventName])
eventName: <string> The name of the event.- Returns: <EventEmitter>
Removes all listeners, or those of the specified eventName.
Emits the 'rmlistener:{eventName}' event when called.
Returns a reference to the EventEmitter, so that calls can be chained.
EventEmitter.removeListener(eventName, listener)
eventName: <string>listener: <function>- Returns: <EventEmitter>
Removes the specified listener from the listener array for the event named eventName.
Emits the 'rmlistener:{eventName}' event when called.
EventEmitter.hasEvent(eventName, raiseException)
eventName: <string>raiseException: <boolean> (Default:False)- Returns: <EventEmitter>
Check if the event emitter has within itself an event named eventName, return a boolean for the operation.
if raiseException is True, raise an exception if the result of the check evaluates to False.
EventEmitter.hasListeners(eventName)
eventName: <string>raiseException: <boolean>- Returns: <EventEmitter>
Safely check that the core EventListenerStack has at least one listener.
Implements the EventListenerStack::hasListeners() inherently.
Class: EventListener(listener)
listener: <function>
This class wraps the listener function with useful, sandboxed manipulative features
The EventListener class is defined and exposed publicly by the module:
from node_events import EventListener
def fn():
print("test_fn")
EventListener(fn).respond()
# Prints
# test_fn
EventListener.listenerCount(getter)
Number of times the function has been called
EventListener.respond(*data)
*data: <any>
Send the data arguments to the encapsulated function in evaluation
EventListener.verify(fn)
fn: <function>- Returns: <boolean>
Check if fn matches with the encapsulated function
Useful in finding the instance amongst others by matching its core
Class: EventListenerStack(eventName)
eventName: <string>
Stacking layer of listeners for an event defined named eventName
Serves as an interfacing remote for series of grouped listeners
The EventListenerStack class is defined and exposed publicly by the module:
from node_events import EventListenerStack
def test_fn():
print("hi from test_fn")
stack = EventListenerStack("event_name")
stack.attachListener(test_fn, 0)
stack.respond()
# Prints
# hi from test_fn
EventListenerStack.listeners(getter)
Return a copy of the private listeners array.
EventListenerStack.listenerCount(getter)
Return the number of listeners exist and are actively waiting for event firings
EventListenerStack.respond(*data)
*data: <any>- Returns: <boolean>
Send the data arguments to all the listeners within the stack in the order of which they appear
Returns True if the stack has any active listeners who read the data else False
EventListenerStack.verifyHasListener(fn)
fn: <function>
Check if the stack has the listener fn
EventListenerStack.attachListener(fn, index)
fn: <function>index: <number>
Attach the fn listener to the event stack.
The index parameter determines the index at which to place the function in the stack array
Note, function calls are based on orderly calls from the stack array
EventListenerStack.detachListener(fn)
fn: <function>- Returns: <boolean>
Detach the fn listener from the stack if it exists returning True otherwise return False
EventListenerStack.detachAllListeners()
Detach all the listeners within the stack
EventListenerStack.hasListeners()
Check if the stack has any listeners within
EventListenerStack.extractInstanceOf(fn)
fn: <function>
Extract the EventListener instance encapsulating the fn listener if it exists otherwise return None
Development
Building
Feel free to clone, use in adherance to the license and perhaps send pull requests
git clone https://github.com/miraclx/node_events.py.git
cd node_events.py
# hack on code
pip3 install . --user
License
Apache 2.0 © Miraculous Owonubi (@miraclx) <omiraculous@gmail.com>
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file node_events-0.6.9.tar.gz.
File metadata
- Download URL: node_events-0.6.9.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
933d7da7ddebe450cf53297c168bb5cdc1ce706980957978e48aa4ade1d3203b
|
|
| MD5 |
38001ce23087abe1b689dcc0b2e2f1a2
|
|
| BLAKE2b-256 |
a8119b2735dab65d42dfed509de6ab25643174456b89c89adf14ba57480f8632
|
File details
Details for the file node_events-0.6.9-py3-none-any.whl.
File metadata
- Download URL: node_events-0.6.9-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
160c6f4f2f3535eaa851c4a072aa853b65d12fd703474c182579c9c2b3e03665
|
|
| MD5 |
47b0a5928c4d91285f40fe067aafa9d4
|
|
| BLAKE2b-256 |
42dd9fff410c34aff104f569ac3d0f1d5fcf1f203bdfd3b908b928cdf2f8bfbe
|