Hi.
The recent 2.3.2 and 2.4.0 update introduced aggregates (e.g. Throttle) to backends using the following code from cryptofeed/cryptofeed/backends/aggregate.py:
class AggregateCallback:
def __init__(self, handler):
self.handler = handler
if hasattr(self.handler, '__class__'):
setattr(self, 'start', self.handler.start)
setattr(self, 'stop', self.handler.stop)
self.__name__ = self.handler.__class__
class Throttle(AggregateCallback):
"""
Wraps a callback and throttles updates based on `window`. Will allow
1 update per `window` interval; all others are dropped
"""
def __init__(self, handler, window=60):
super().__init__(handler)
self.window = window
self.last_update = 0
But what if the handler is not a backend object, but a normal callback function?:
The callback book is a function, so book.__class__ would be function, so if hasattr(self.handler, '__class__') would be True, but book does not have an attribute or method called start, so self.handler.start would not exist, throwing an error, something as the following:
Do we need something like if hasattr(self.handler, '__class__') and (self.handler.__class__ != (lambda x: x).__class__): to let the non-backend callback functions pass? Are there more elegant ways to construct or use aggregates with functions without backends?
Thanks.
Hi. The recent 2.3.2 and 2.4.0 update introduced aggregates (e.g. Throttle) to backends using the following code from
cryptofeed/cryptofeed/backends/aggregate.py
:But what if the handler is not a backend object, but a normal callback function?:
The callback
book
is a function, sobook.__class__
would befunction
, soif hasattr(self.handler, '__class__')
would beTrue
, butbook
does not have an attribute or method calledstart
, soself.handler.start
would not exist, throwing an error, something as the following:Do we need something like
if hasattr(self.handler, '__class__') and (self.handler.__class__ != (lambda x: x).__class__):
to let the non-backend callback functions pass? Are there more elegant ways to construct or use aggregates with functions without backends? Thanks.