onesecondtrader 0.14.2__py3-none-any.whl → 0.16.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,499 +0,0 @@
1
- """
2
- This module provides the event bus for managing event-driven communication between
3
- the trading infrastructure's components via a publish-subscribe messaging pattern.
4
- """
5
-
6
- import collections
7
- import inspect
8
- import logging
9
- import threading
10
- from collections.abc import Callable
11
- from onesecondtrader.messaging import events
12
- from onesecondtrader.monitoring import console
13
-
14
-
15
- __all__ = [
16
- "EventBus",
17
- "system_event_bus",
18
- ]
19
-
20
-
21
- class EventBus:
22
- # noinspection PyTypeChecker
23
- """
24
- Event bus for managing event-driven communication between the trading
25
- infrastructure's components via a publish-subscribe messaging pattern.
26
- Supports inheritance-based subscriptions where handlers subscribed to a parent event
27
- type will receive events of child types.
28
- Each subscription can include an optional filter function to receive only specific
29
- events of a given type (e.g. filtering `IncomingBar` events for a specific symbol).
30
-
31
- Examples:
32
- >>> # Import necessary modules
33
- >>> import pandas as pd
34
- >>> from onesecondtrader.messaging.eventbus import EventBus
35
- >>> from onesecondtrader.messaging import events
36
- >>> from onesecondtrader.core import models
37
-
38
- >>> # Instantiate event bus
39
- >>> event_bus = EventBus()
40
-
41
- >>> # Create a dummy handler that simply prints the symbol of the received event
42
- >>> def dummy_handler(incoming_bar_event: events.Market.IncomingBar):
43
- ... print(f"Received: {incoming_bar_event.symbol}")
44
-
45
- >>> # Subscribe to IncomingBar events whose symbol is AAPL
46
- >>> event_bus.subscribe(
47
- ... events.Market.IncomingBar,
48
- ... dummy_handler,
49
- ... lambda event: event.symbol == "AAPL" # Lambda filter function
50
- ... )
51
-
52
- >>> # Create events to publish
53
- >>> aapl_event = events.Market.IncomingBar(
54
- ... ts_event=pd.Timestamp("2023-01-01", tz="UTC"),
55
- ... symbol="AAPL",
56
- ... bar=models.Bar(
57
- ... open=100.0, high=101.0, low=99.0,
58
- ... close=100.5, volume=1000
59
- ... )
60
- ... )
61
- >>> googl_event = events.Market.IncomingBar(
62
- ... ts_event=pd.Timestamp("2023-01-01", tz="UTC"),
63
- ... symbol="GOOGL",
64
- ... bar=models.Bar(
65
- ... open=2800.0, high=2801.0, low=2799.0,
66
- ... close=2800.5, volume=500
67
- ... )
68
- ... )
69
-
70
- >>> # Publish events - only AAPL passes filter and will be printed
71
- >>> event_bus.publish(aapl_event)
72
- Received: AAPL
73
- >>> event_bus.publish(googl_event)
74
-
75
- >>> # Unsubscribe the dummy handler
76
- >>> event_bus.unsubscribe(events.Market.IncomingBar, dummy_handler)
77
-
78
- >>> # Publish again - no handler receives it (warning will be logged)
79
- >>> event_bus.publish(aapl_event) # doctest: +SKIP
80
- WARNING:root:Published IncomingBar but no subscribers exist - check event wiring
81
- """
82
-
83
- def __init__(self) -> None:
84
- """
85
- Initializes the event bus with optimized data structures for high-performance
86
- event publishing.
87
-
88
- Attributes:
89
- self._handlers (collections.defaultdict): Direct storage mapping event types
90
- to handler lists
91
- self._publish_cache (dict): Pre-computed cache for O(1) publish operations
92
- self._lock (threading.Lock): Single lock for all operations
93
- (subscribe/unsubscribe are rare)
94
- self._sequence_number (int): Sequence number counter for events
95
- """
96
- self._handlers: dict[
97
- type[events.Base.Event],
98
- list[
99
- tuple[
100
- Callable[[events.Base.Event], None],
101
- Callable[[events.Base.Event], bool],
102
- ]
103
- ],
104
- ] = collections.defaultdict(list)
105
-
106
- self._publish_cache: dict[
107
- type[events.Base.Event],
108
- list[
109
- tuple[
110
- Callable[[events.Base.Event], None],
111
- Callable[[events.Base.Event], bool],
112
- ]
113
- ],
114
- ] = {}
115
-
116
- self._lock: threading.Lock = threading.Lock()
117
- self._sequence_number: int = -1
118
-
119
- self._rebuild_cache()
120
-
121
- def subscribe(
122
- self,
123
- event_type: type[events.Base.Event],
124
- event_handler: Callable[[events.Base.Event], None],
125
- event_filter: Callable[[events.Base.Event], bool] | None = None,
126
- ) -> None:
127
- """
128
- The `subscribe` method registers an event handler for event messages of a
129
- specified type and all its subtypes (expressed as subclasses in the event
130
- dataclass hierarchy, so-called inheritance-based subscription).
131
- When an event of that type or any subtype is published, the handler will be
132
- invoked if the associated `event_filter` returns `True` for that event
133
- instance.
134
- A given handler can only be subscribed once per event type.
135
- If the handler is already subscribed to the given event type
136
- —regardless of the filter function—
137
- the subscription attempt is ignored and a warning is logged.
138
-
139
- Arguments:
140
- event_type (type[events.Base.Event]): Type of the event to subscribe to,
141
- must be a subclass of `events.Base.Event`.
142
- event_handler (Callable[events.Base.Event, None]): Function to call when an
143
- event of the given type is published.
144
- This callable must accept a single argument of type `events.Base.Event`
145
- (or its subclass).
146
- event_filter (Callable[[events.Base.Event], bool] | None): Function to
147
- determine whether to call the event handler for a given event.
148
- Should accept one event and return `True` to handle or `False` to
149
- ignore.
150
- Defaults to `None`, which creates a filter that always returns `True`
151
- (i.e. always call the event handler).
152
- """
153
-
154
- if not issubclass(event_type, events.Base.Event):
155
- console.logger.error(
156
- f"Invalid subscription attempt: event_type must be a subclass of "
157
- f"Event, got {type(event_type).__name__}"
158
- )
159
- return
160
-
161
- if not callable(event_handler):
162
- console.logger.error(
163
- f"Invalid subscription attempt: event_handler must be callable, "
164
- f"got {type(event_handler).__name__}"
165
- )
166
- return
167
-
168
- if event_filter is None:
169
-
170
- def event_filter(event: events.Base.Event) -> bool:
171
- return True
172
-
173
- if not callable(event_filter):
174
- console.logger.error(
175
- f"Invalid subscription attempt: event_filter must be callable, "
176
- f"got {type(event_filter).__name__}"
177
- )
178
- return
179
-
180
- is_valid, error_msg = self._validate_filter_signature(event_filter)
181
- if not is_valid:
182
- console.logger.error(f"Invalid subscription attempt: {error_msg}")
183
- return
184
-
185
- with self._lock:
186
- if any(
187
- event_handler == existing_handler
188
- for existing_handler, _ in self._handlers[event_type]
189
- ):
190
- console.logger.warning(
191
- f"Duplicate subscription attempt: event_handler was already "
192
- f"subscribed to {event_type.__name__}"
193
- )
194
- return
195
-
196
- self._handlers[event_type].append((event_handler, event_filter))
197
-
198
- self._rebuild_cache()
199
-
200
- handler_name = getattr(event_handler, "__name__", "<lambda>")
201
- console.logger.info(f"Subscribed {handler_name} to {event_type.__name__}.")
202
-
203
- def unsubscribe(
204
- self,
205
- event_type: type[events.Base.Event],
206
- event_handler: Callable[[events.Base.Event], None],
207
- ) -> None:
208
- """
209
- The `unsubscribe` method removes an event handler from the subscribers list for
210
- the specified event type.
211
- If the event handler is not subscribed to the given event type, the
212
- unsubscription attempt is ignored and a warning is logged.
213
- After removing the event handler, the event type may have an empty subscribers
214
- list but remains in the `subscribers` dictionary.
215
-
216
- Arguments:
217
- event_type (type[events.Base.Event]): Type of the event to unsubscribe from,
218
- must be a subclass of `events.Base.Event`.
219
- event_handler (Callable[events.Base.Event, None]): Event handler to remove
220
- from the subscribers list (this will also remove the associated filter
221
- function).
222
- """
223
- if not issubclass(event_type, events.Base.Event):
224
- console.logger.error(
225
- f"Invalid unsubscription attempt: event_type must be a subclass of "
226
- f"Event, got {type(event_type).__name__}"
227
- )
228
- return
229
-
230
- if not callable(event_handler):
231
- console.logger.error(
232
- f"Invalid unsubscription attempt: callback must be callable, "
233
- f"got {type(event_handler).__name__}"
234
- )
235
- return
236
-
237
- with self._lock:
238
- if event_type not in self._handlers:
239
- console.logger.warning(
240
- f"Attempted to unsubscribe from {event_type.__name__}, "
241
- f"but no subscribers exist"
242
- )
243
- return
244
-
245
- current_handlers = self._handlers[event_type]
246
- new_handlers = [
247
- (existing_handler, existing_filter)
248
- for existing_handler, existing_filter in current_handlers
249
- if existing_handler != event_handler
250
- ]
251
-
252
- removed_count = len(current_handlers) - len(new_handlers)
253
- if removed_count == 0:
254
- handler_name = getattr(event_handler, "__name__", "<lambda>")
255
- console.logger.warning(
256
- f"Attempted to unsubscribe {handler_name} from "
257
- f"{event_type.__name__}, but it was not subscribed"
258
- )
259
- return
260
-
261
- if new_handlers:
262
- self._handlers[event_type] = new_handlers
263
- else:
264
- # Clean up empty lists
265
- del self._handlers[event_type]
266
-
267
- self._rebuild_cache()
268
-
269
- handler_name = getattr(event_handler, "__name__", "<lambda>")
270
- console.logger.info(
271
- f"Unsubscribed {handler_name} from "
272
- f"{event_type.__name__} (removed {removed_count} subscription(s))"
273
- )
274
-
275
- def publish(self, event: events.Base.Event) -> None:
276
- """
277
- The `publish` method delivers the event to all handlers subscribed to the
278
- event's type or any of its parent types (inheritance-based subscription).
279
- Handlers are only called if their filter function returns True for this event.
280
- Handlers are called synchronously in the order they were subscribed.
281
-
282
- This method uses a pre-computed handler cache for O(1) lookup performance
283
- and runs without locks for maximum concurrency.
284
-
285
- Arguments:
286
- event (events.Base.Event): Event to publish. Must be an instance of
287
- `events.Base.Event` or one of its subclasses.
288
- """
289
- if not isinstance(event, events.Base.Event):
290
- console.logger.error(
291
- f"Invalid publish attempt: event must be an instance of Event, "
292
- f"got {type(event).__name__}"
293
- )
294
- return
295
-
296
- object.__setattr__(
297
- event, "event_bus_sequence_number", self._set_sequence_number()
298
- )
299
-
300
- event_type: type[events.Base.Event] = type(event)
301
-
302
- handlers = self._publish_cache.get(event_type, [])
303
-
304
- if not handlers:
305
- console.logger.warning(
306
- f"Published {event_type.__name__} but no subscribers exist - "
307
- f"check event wiring"
308
- )
309
- return
310
-
311
- delivered_count = 0
312
- for event_handler, event_filter in handlers:
313
- try:
314
- should_handle = event_filter(event)
315
-
316
- if not isinstance(should_handle, bool):
317
- handler_name = getattr(event_handler, "__name__", "<lambda>")
318
- console.logger.warning(
319
- f"Filter for handler {handler_name} returned "
320
- f"{type(should_handle).__name__}, expected bool. "
321
- f"Treating as False."
322
- )
323
- should_handle = False
324
-
325
- except TypeError as type_error:
326
- handler_name = getattr(event_handler, "__name__", "<lambda>")
327
- if "takes" in str(type_error) and "positional argument" in str(
328
- type_error
329
- ):
330
- console.logger.error(
331
- f"Filter for handler {handler_name} has wrong signature: "
332
- f"{type_error}"
333
- )
334
- else:
335
- console.logger.exception(
336
- f"Filter function for handler {handler_name} failed "
337
- f"processing {event_type.__name__}: {type_error}"
338
- )
339
- continue
340
- except Exception as filter_exception:
341
- handler_name = getattr(event_handler, "__name__", "<lambda>")
342
- console.logger.exception(
343
- f"Filter function for handler {handler_name} failed "
344
- f"processing {event_type.__name__}: {filter_exception}"
345
- )
346
- continue
347
-
348
- if should_handle:
349
- try:
350
- event_handler(event)
351
- delivered_count += 1
352
- except Exception as handler_exception:
353
- handler_name = getattr(event_handler, "__name__", "<lambda>")
354
- console.logger.exception(
355
- f"Handler {handler_name} failed processing "
356
- f"{event_type.__name__}: {handler_exception}"
357
- )
358
-
359
- if delivered_count == 0:
360
- console.logger.warning(
361
- f"Published {event_type.__name__} but no handlers received it - "
362
- f"all {len(handlers)} handler(s) filtered out the event"
363
- )
364
- else:
365
- # Conditional debug logging to avoid string formatting overhead
366
- if console.logger.isEnabledFor(logging.DEBUG):
367
- console.logger.debug(
368
- f"Published {event_type.__name__} to {delivered_count} handler(s)"
369
- )
370
-
371
- @staticmethod
372
- def _validate_filter_signature(
373
- event_filter: Callable[[events.Base.Event], bool],
374
- ) -> tuple[bool, str | None]:
375
- """
376
- Validate that filter function has the correct signature.
377
-
378
- A valid filter function must:
379
- - Accept exactly 1 parameter (the event)
380
- - Not use *args or **kwargs
381
- - Optionally return bool (if type annotated)
382
-
383
- Arguments:
384
- event_filter (Callable): The filter function to validate
385
-
386
- Returns:
387
- tuple[bool, str | None]: (is_valid, error_message)
388
- is_valid: True if signature is valid, False otherwise
389
- error_message: Description of the issue if invalid, None if valid
390
- """
391
- try:
392
- sig = inspect.signature(event_filter)
393
- params = list(sig.parameters.values())
394
-
395
- if len(params) != 1:
396
- return (
397
- False,
398
- f"Filter must accept exactly 1 parameter, got {len(params)}",
399
- )
400
-
401
- param = params[0]
402
- if param.kind == inspect.Parameter.VAR_POSITIONAL:
403
- return (
404
- False,
405
- "Filter cannot use *args - must accept exactly 1 event parameter",
406
- )
407
- if param.kind == inspect.Parameter.VAR_KEYWORD:
408
- return (
409
- False,
410
- "Filter cannot use **kwargs - must accept exactly 1 event "
411
- "parameter",
412
- )
413
-
414
- if sig.return_annotation is not inspect.Parameter.empty:
415
- if sig.return_annotation is not bool:
416
- return (
417
- False,
418
- f"Filter return type should be bool, got "
419
- f"{sig.return_annotation}",
420
- )
421
-
422
- return True, None
423
-
424
- except Exception as e:
425
- return False, f"Could not inspect filter signature: {e}"
426
-
427
- def _set_sequence_number(self) -> int:
428
- """
429
- Increment and return the event bus sequence number in a thread-safe manner.
430
- """
431
- with self._lock:
432
- self._sequence_number += 1
433
- return self._sequence_number
434
-
435
- @staticmethod
436
- def _get_all_concrete_event_types() -> list[type[events.Base.Event]]:
437
- """
438
- Dynamically discover all concrete event types from the events module.
439
- Automatically adapts to namespace changes without code modifications.
440
-
441
- Returns:
442
- list[type[events.Base.Event]]: List of concrete event classes that can be
443
- instantiated and published.
444
- """
445
- concrete_types = []
446
-
447
- for attr_name in dir(events):
448
- if attr_name.startswith("_"):
449
- continue
450
-
451
- attr = getattr(events, attr_name)
452
-
453
- if not inspect.isclass(attr) or attr_name == "Base":
454
- continue
455
-
456
- for member_name, member_obj in inspect.getmembers(attr, inspect.isclass):
457
- if (
458
- issubclass(member_obj, events.Base.Event)
459
- and member_obj != events.Base.Event
460
- and not inspect.isabstract(member_obj)
461
- ):
462
- concrete_types.append(member_obj)
463
-
464
- return concrete_types
465
-
466
- def _rebuild_cache(self) -> None:
467
- """
468
- Rebuild the pre-computed publish cache for all concrete event types.
469
- This method should be called whenever subscriptions change.
470
- """
471
- new_cache = {}
472
- concrete_event_types = self._get_all_concrete_event_types()
473
-
474
- for concrete_event_type in concrete_event_types:
475
- handlers = []
476
- seen_handler_ids = set()
477
-
478
- for handler_type, handler_list in self._handlers.items():
479
- if issubclass(concrete_event_type, handler_type):
480
- for handler, filter_func in handler_list:
481
- handler_id = id(handler)
482
- if handler_id not in seen_handler_ids:
483
- handlers.append((handler, filter_func))
484
- seen_handler_ids.add(handler_id)
485
-
486
- if handlers:
487
- new_cache[concrete_event_type] = handlers
488
-
489
- self._publish_cache = new_cache
490
-
491
- if console.logger.isEnabledFor(logging.DEBUG):
492
- console.logger.debug(
493
- f"Publish cache rebuilt: {len(new_cache)} event types cached, "
494
- f"total handlers: "
495
- f"{sum(len(handlers) for handlers in new_cache.values())}"
496
- )
497
-
498
-
499
- system_event_bus = EventBus()