objwatch.event_handls module

class objwatch.event_handls.EventHandls(config: ObjWatchConfig)[source]

Bases: object

Handles various events for ObjWatch, including function execution and variable updates. Optionally saves the events in a JSON format.

__init__(config: ObjWatchConfig) None[source]

Initialize the EventHandls with optional JSON output.

Parameters:

config (ObjWatchConfig) – The configuration object to include in the JSON output.

static _generate_prefix(lineno: int, call_depth: int) str[source]

Generate a formatted prefix for logging with caching.

Parameters:
  • lineno (int) – The line number where the event occurred.

  • call_depth (int) – Current depth of the call stack.

Returns:

The formatted prefix string.

Return type:

str

_log_event(lineno: int, event_type: EventType, message: str, call_depth: int, index_info: str) None[source]

Log an event with consistent formatting.

Parameters:
  • lineno (int) – The line number where the event occurred.

  • event_type (EventType) – The type of event.

  • message (str) – The message to log.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

_add_json_event(event_type: str, data: Dict[str, Any]) Dict[str, Any][source]

Create a JSON event object with the given data and add it to the current node.

Parameters:
  • event_type (str) – Type of the event to create.

  • data (dict) – Dictionary of data to include in the event.

Returns:

The created event dictionary.

Return type:

dict

_handle_collection_change(lineno: int, class_name: str, key: str, value_type: type, old_value_len: int | None, current_value_len: int | None, call_depth: int, index_info: str, event_type: EventType) None[source]

Handle collection change events (APD or POP) with a common implementation.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the data structure.

  • key (str) – Name of the data structure.

  • value_type (type) – Type of the elements.

  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

  • event_type (EventType) – The type of event (APD or POP).

handle_run(lineno: int, func_info: dict, abc_wrapper: Any | None, call_depth: int, index_info: str) None[source]

Handle the ‘run’ event indicating the start of a function or method execution.

handle_end(lineno: int, func_info: dict, abc_wrapper: Any | None, call_depth: int, index_info: str, result: Any) None[source]

Handle the ‘end’ event indicating the end of a function or method execution.

handle_upd(lineno: int, class_name: str, key: str, old_value: Any, current_value: Any, call_depth: int, index_info: str, abc_wrapper: Any | None = None) None[source]

Handle the ‘upd’ event representing the creation or updating of a variable.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the variable.

  • key (str) – Variable name.

  • old_value (Any) – Previous value of the variable.

  • current_value (Any) – New value of the variable.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

  • abc_wrapper (Optional[Any]) – Custom wrapper for additional processing.

handle_apd(lineno: int, class_name: str, key: str, value_type: type, old_value_len: int | None, current_value_len: int | None, call_depth: int, index_info: str) None[source]

Handle the ‘apd’ event denoting the addition of elements to data structures.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the data structure.

  • key (str) – Name of the data structure.

  • value_type (type) – Type of the elements being added.

  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

handle_pop(lineno: int, class_name: str, key: str, value_type: type, old_value_len: int | None, current_value_len: int | None, call_depth: int, index_info: str) None[source]

Handle the ‘pop’ event marking the removal of elements from data structures.

Parameters:
  • lineno (int) – The line number where the event is called.

  • class_name (str) – Name of the class containing the data structure.

  • key (str) – Name of the data structure.

  • value_type (type) – Type of the elements being removed.

  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

  • call_depth (int) – Current depth of the call stack.

  • index_info (str) – Information about the index to track in a multi-process environment.

determine_change_type(old_value_len: int, current_value_len: int) EventType | None[source]

Determine the type of change based on the difference in lengths.

Parameters:
  • old_value_len (int) – Previous length of the data structure.

  • current_value_len (int) – New length of the data structure.

Returns:

The determined event type (APD or POP).

Return type:

EventType

static format_sequence(seq: Any, max_elements: int = 3, func: LambdaType | None = None) str[source]

Format a sequence to display a limited number of elements.

Parameters:
  • seq (Any) – The sequence to format.

  • max_elements (int) – Maximum number of elements to display.

  • func (Optional[FunctionType]) – Optional function to process elements.

Returns:

The formatted sequence string.

Return type:

str

static _format_value(value: Any) str[source]

Format individual values for the ‘upd’ event when no wrapper is provided.

Parameters:

value (Any) – The value to format.

Returns:

The formatted value string.

Return type:

str

save_json() None[source]

Save the accumulated events to a JSON file upon program exit with optimized size.

signal_handler(signum, frame)[source]

Signal handler for abnormal program termination. Calls save_json when a termination signal is received.

Parameters:
  • signum (int) – The signal number.

  • frame (frame) – The current stack frame.