Skip to main content

A simple library that implements a basic event system

Project description

A simple library that implements a basic event system.

You can simply create classes, just use EventBase as second, or only base class, and use its methods.

Register event names in init method using chain self.initEvents(?delegateObject).registerNewEvents(*names: str)

from SimpleEvents import *
class EventDict(dict, EventBase):
    def __init__(self, *args, **kwargs):
        self.initEvents().registerNewEvents(
            "get", "set", "del"
        )
        super(EventDict, self).__init__(*args, **kwargs)
    
    def get(self, key, *args, **kwargs):
        self.notifySubscribersWithDataAuto("get", key=key)
        return super(EventDict, self).get(key, *args, **kwargs)
    
    def __getitem__(self, key, *args, **kwargs):
        self.notifySubscribersWithDataAuto("get", key=key)
        return super(EventDict, self).__getitem__(key, *args, **kwargs)
    
    def __setitem__(self, key, *args, **kwargs):
        self.notifySubscribersWithDataAuto("set", key=key)
        return super(EventDict, self).__setitem__(key, *args, **kwargs)
    
    def __delitem__(self, key, *args, **kwargs):
        self.notifySubscribersWithDataAuto("del", key=key)
        return super(EventDict, self).__delitem__(key, *args, **kwargs)

Subscribe:

ed = EventDict()

ed.subscribeToEvent("get", print)
ed.subscribeToEvent("set", print)
ed.subscribeToEvent("del", print)

Unsubscribe:

ed = EventDict()

ed.unsubscribeToEvent("get", print)
ed.unsubscribeToEvent("set", print)
ed.unsubscribeToEvent("del", print)

Structure

  • SimpleEvents
    • [-CLASS-] DelegateUnary
    • [-CLASS-] DelegateTuple
    • [-CLASS-] DelegateList (Recomended, default)
    • [-CLASS-] EventData
    • [-CLASS-] EventBase
    • [-FUNCTION-] Dispatcher (works as decorator)

Delegates:

In fact, they are containers of multiple functions and allow you to call them all, returning the last result. An unary delegate can only contain one function and will throw a TypeError when you try to merge it by .merge(otherDelegate).

You can easily create and apply your own delegate types (the default is DelegateList). You only need to implement a number of methods:

  • invoke(*args, **kwargs) - invokes delegate
  • add(func) - add function to delegate
  • clear() - clear delegate
  • remove() - remove first appearance of function indelegate
  • merge() - merge delegate with other on-place [optional]

How to use custom delegates - goto EventBase description

EventData

A simple dictionary-inherited container for storing event data. There are minor differences from the usual dictionary:

  • .args - can contain an array of ordered elements with no keys passed to the constructor
  • .name - is the name of the calling event
  • Constructor signature is: def __init__(self, name:str, *args , **kwargs)
#you just can pass key-value pairs
x = EventData(somekey="somevalue", somekey2=2)
  • You can get key values not only through the [] operator, but also through accessing the attributes '.'. All unknown attributes will return None, but this works purely as a getter
data.somekey == data["somekey"] == "somevalue"
data.unknown == None

data.unknown = "newvalue" # raises error, no setter

EventBase

The basic interface for implementing an event system for an object.Just use it instead of 'object' when inheriting a new class, or if your class is already inheriting from another class, then as a second ancestor. Above is a primitive example of an event wrapper class for a dictionary. Methods included in this class:

  • initEvents() - must be called when the instance is created
  • registerNewEvent(name:str, *, delegateObject = None) Registers a new event for an object. You can explicitly pass a custom delegate object for a given event registerNewEvent(...delegateObject=MyDelegate())
  • registerNewEvents(*names: str, delegateObjectType=None) Registers multiple new events. You can also specify a custom delegate, but already in the form of the delegate class itself. registerNewEvents(...delegateObjectType=MyDelegate)
  • subscribeToEvent(name: str, function: Callable Subscribes the passed function to a specific event on the object, by its name. .subscribeToEvent("myEvent", lambda data: print(data))
  • unsubscribeToEvent(name: str, function: Callable reverses the result of the previous method
  • clearSubscribersAndRegistrations() completely removes all registrations and subscribers
  • clearSubscribers() removes all subscribers but leaves registrations
  • notifySubscribers(name: str, *args, **kwargs) The simplest type of notification. All subscribers will be called with the given arguments.
  • notifySubscribersWithData(data: EventData) works with a ready-made data object. The "sender" key automatically sets to a link to the caller.
  • notifySubscribersWithDataAuto(name: str, *args, **kwargs) Completely repeats the previous method, but creates a data object on its own.

All of these methods, with the exception of three Notify methods, return the object they were called on. This allows you to build them as chains if it necessary to you.

Custom delegates are passed when registering events.

Dispatcher

A primitive event system attached to a specific function. Works like a decorator.

from SimpleEvents import Dispatcher

# don't forget about those parentheses.
# It is a parameterized decorator,
# with just one optional parameter.
# @Dispatcher(delegateObject = None)
@Dispatcher()
def doNothing():
    pass

@doNothing.subscribe
def noname():
    print("A")


@doNothing.subscribe
def noname():
    print("B")

doNothing.subscribe(noname) # after subscribing returns noname function, decorator-safe
doNothing.unsubscribe(noname) # after unsubscribing returns noname function, decorator-safe

doNothing()
# out:
# A
# B

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

Simple-Events-1.0.2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Simple_Events-1.0.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file Simple-Events-1.0.2.tar.gz.

File metadata

  • Download URL: Simple-Events-1.0.2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for Simple-Events-1.0.2.tar.gz
Algorithm Hash digest
SHA256 1703abe862613c9c5088ce9d2576bcc7e110d37f5b9f893a154edf3fe14e350a
MD5 d4dee377e18aa859a71f3e4f7cf3f4f6
BLAKE2b-256 a27d16e0fa815f608dd2b9f52839dfd1115546e7e3e4cdae8c114352a9b14422

See more details on using hashes here.

File details

Details for the file Simple_Events-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: Simple_Events-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for Simple_Events-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 81c481e4f2678dc46bf7c2950117a6df1d4e4affeb21f9abfda94e590a399756
MD5 b935f3bdbd64669cc7d848c4570336c0
BLAKE2b-256 06c23ff94a1fc992991aa275b0c11b7937033a6149a4161c8c06f1ef2d462bdd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page