mlp/src/syntax_tree.py
Overview:
The DynamicEventSystem
class provides a flexible way to manage and respond to events dynamically in Python applications. This system allows for the registration of event handlers that are executed when their corresponding events are emitted. Additionally, it supports the creation of dynamic handlers based on runtime logic.
DynamicEventSystem
(class):
Attributes:
_handlers
: a private dictionary that maps event names (strings) to lists of handler functions. These handlers are functions that are called when the corresponding event is emitted.
Methods:
register_event(event_name, handler)
: registers a handler function for a specified event. If the event does not already exist in _handlers, it is added as a new key with an empty list as its value. The handler is then appended to the list associated with event_name.emit(event_name, *args, **kwargs)
: emits an event by executing all handler functions associated with the given event_name. Handlers are called with any additional positional and keyword arguments provided. If no handlers are registered for the event, the method simply passes without any action.create_dynamic_handler(logic)
: creates a dynamic event handler based on a string representing Python logic (code). This method returns a lambda function that, when called, evaluates the string of logic using Python’s eval function, passing any arguments or keyword arguments into the evaluated code. This allows for the creation of event handlers that can be defined at runtime based on changing conditions or configurations.