Skip to main content

A lightweight event handling library for Python and C/C++.

Project description

Simple Event Library (SEL)


The Simple Event Library (SEL) is a lightweight event handling library for Python >= 3.6 and C/C++. It provides a simple and efficient way to manage events and event handlers.

Current version: 1.0.4142 (see res/common/ver.txt)

Installation

To install SEL, you can:

git clone https://github.com/LuoWenhaoFromDangYang/Simple-Event-Library.git
cd Simple-Event-Library
pip install .

Or:

pip install SimpleELpy==1.0.4142

Usage

Python

Use SEL on Python like this:

import SEL

def foo(*args):
    a = args[0]
    b = args[1]
    print(f"Received argument: {a:.2f} {b:.2f}")
    return a + b

def main():
    print(f"SEL version: {SEL.SEL_PY}")
    SEL.Events.add_event("foo")
    SEL.Events.bind("foo", foo)
    retval = SEL.Events.trigger("foo", 0.1, 0.2)[0]
    print(f"Result: {retval}")
    return 0

if __name__ == "__main__":
    main()

C/C++

Or use SEL on C/C++ like this:

#include <stdio.h>
#include <stdarg.h>
#include "./SEL.h"

void foo(va_list args) {
    double a = va_arg(args, double);
    double b = va_arg(args, double);
    va_end(args);
    printf("Received argument: %.2lf %.2lf\n", a, b);
    return;
}

int main() {
    printf("SEL version: %s\n", SEL_C);
    int val;
    if(val = SEL_Init()) {
        return 1;
    }
    printf("Successfully initialized SEL.\n");
    if(val = SEL_AddEvent("foo")) {
        SEL_Free();
        return 1;
    }
    if(val = SEL_Bind("foo", foo)) {
        SEL_Free();
        return 1;
    }
    if(val = SEL_Trigger("foo", 0.1, 0.2)) {
        SEL_Free();
        return 1;
    }
    SEL_Free();
    printf("SEL freed.\n");
    return 0;
}

C++

And use SEL on C++ like this:

#include <iostream>
#include <cstdarg>
#include "./SEL.h"

void foo(va_list args) {
    double a = va_arg(args, double);
    double b = va_arg(args, double);
    va_end(args);
    std::cout << "Received argument: " << a << " " << b << std::endl;
    return;
}

int main() {
    std::cout << "SEL version: " << SEL_C << std::endl;
    int val;
    if(val = SEL::Events.Init()) {
        return 1;
    }
    std::cout << "Successfully initialized SEL.\n";
    if(val = SEL::Events.AddEvent("foo")) {
        SEL::Events.Free();
        return 1;
    }
    if(val = SEL::Events.Bind("foo", foo)) {
        SEL::Events.Free();
        return 1;
    }
    if(val = SEL::Events.Trigger("foo", 0.1, 0.2)) {
        SEL::Events.Free();
        return 1;
    }
    SEL::Events.Free();
    std::cout << "SEL freed.\n";
    return 0;
}

Initialization

To initialize SEL, you can use functions like these:

  • In Python, SEL initializes automatically when imported.
  • In it's C/C++, you need to call SEL_Init() manually.
  • In it's C++, you need to create a SEL::Events object and call events.Init() manually.

These function returns an integer value indicating the success or failure of the initialization. If the initialization is successful, the function returns 0. If the initialization fails, the function returns a non-zero value.

APIs

Python

  • SEL.Events.add_event(event_name: str) -> None: Adds an event with the given name.
  • SEL.Events.remove_event(event_name: str) -> None: Removes an event with the given name.
  • SEL.Events.get_events() -> dict[str, list[Callable]]: Returns a dict of all events and their corresponding handlers.
  • SEL.Events.bind(event_name: str, handler: Callable) -> None: Binds a handler to an event.
  • SEL.Events.unbind(event_name: str, handler: Callable) -> None: Unbinds a handler from an event.
  • SEL.Events.trigger(event_name: str, *args, **kwargs) -> list[Any]: Triggers an event with the given arguments.

C/C++

  • int SEL_Init(): Initializes SEL. Returns 0 on success, non-zero on failure.
  • int SEL_AddEvent(const char* event_name): Adds an event with the given name. Returns 0 on success, non-zero on failure.
  • int SEL_RemoveEvent(const char* event_name): Removes an event with the given name. Returns 0 on success, non-zero on failure.
  • int SEL_ClearEvents(): Clears all events. Returns 0 on success, non-zero on failure.
  • int SEL_Bind(const char* event_name, void (*handler)(va_list)): Binds a handler to an event. Returns 0 on success, non-zero on failure.
  • int SEL_Unbind(const char* event_name, void (*handler)(va_list)): Unbinds a handler from an event. Returns 0 on success, non-zero on failure.
  • int SEL_TriggerV(const char* event_name, va_list args): Triggers an event with the given arguments. Returns 0 on success, non-zero on failure.
  • int SEL_Trigger(const char* event_name, ...): Triggers an event with the given arguments. Returns 0 on success, non-zero on failure.
  • int SEL_Free(): Frees SEL. Always returns 0.

C++

  • int SEL::Events::Init(): Same as SEL_Init().
  • int SEL::Events::AddEvent(const char* event_name): Same as SEL_AddEvent().
  • int SEL::Events::RemoveEvent(const char* event_name): Same as SEL_RemoveEvent().
  • int SEL::Events::ClearEvents(): Same as SEL_ClearEvents().
  • int SEL::Events::Bind(const char* event_name, void (*handler)(va_list)): Same as SEL_Bind().
  • int SEL::Events::Unbind(const char* event_name, void (*handler)(va_list)): Same as SEL_Unbind().
  • int SEL::Events::TriggerV(const char* event_name, va_list args): Same as SEL_TriggerV().
  • int SEL::Events::Trigger(const char* event_name, ...): Same as SEL_Trigger().
  • int SEL::Events::Free(): Same as SEL_Free().

Compiling C/C++ programs

Compile with:

gcc -o your_out.o your_code.c ./res/c/main.c

Or:

g++ -o your_out.o your_code.cpp ./res/c/main.c

License

MIT License

Contributing

Feel free to open issues or pull requests on GitHub.

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

simpleelpy-1.0.4142.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

simpleelpy-1.0.4142-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file simpleelpy-1.0.4142.tar.gz.

File metadata

  • Download URL: simpleelpy-1.0.4142.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for simpleelpy-1.0.4142.tar.gz
Algorithm Hash digest
SHA256 1e6f2aff110adabad35cd0f12cd6ba94cfad710e2e9b7bcb32b6c7007e454f90
MD5 f6f925d8ccb6cbbe4216fa605089021b
BLAKE2b-256 bad0dc06493f4527114e6767ace456ef5b11f7bbe9b2038342b0f324ee0fe823

See more details on using hashes here.

File details

Details for the file simpleelpy-1.0.4142-py3-none-any.whl.

File metadata

  • Download URL: simpleelpy-1.0.4142-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for simpleelpy-1.0.4142-py3-none-any.whl
Algorithm Hash digest
SHA256 e81d414bce7a70f428551782ea89eeeeb2e9950dca5eca27a22e5996ac93b274
MD5 cd59d0a5d87eb67161781f907d0f95b1
BLAKE2b-256 de45b660dee0f85338d7cc189f59faaa319180e090a50bdbd30300ca740ceef9

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