def __init__(self, name, k=10): self.k = k self.fname = _timestamped_filename("%s-precision-at-%d" % (name, self.k)) smokesignal.on('evaluation_finished', self.on_evaluation_finished) super(PrecisionAtKListener, self).__init__()
def test_emit_with_callback_args(self): # Register first smokesignal.on('foo', self.mock_callback) assert smokesignal.responds_to(self.mock_callback, 'foo') smokesignal.emit('foo', 1, 2, 3, foo='bar') assert self.mock_callback.called_with(1, 2, 3, foo='bar')
def test_on_creates_responds_to_fn(self): # Registering a callback should create partials to smokesignal # methods for later user smokesignal.on('foo', self.fn) assert hasattr(self.fn, 'responds_to') assert self.fn.responds_to('foo')
def __init__(self): if not hasattr(self, 'plugins'): self.plugins = {} self.plugin_names = set( ep.name for ep in pkg_resources.iter_entry_points('helga_plugins')) # Plugins whitelist/blacklist self.whitelist_plugins = self._create_plugin_list('ENABLED_PLUGINS', default=True) self.blacklist_plugins = self._create_plugin_list('DISABLED_PLUGINS', default=set()) # Figure out default channel plugins using the whitelist and blacklist default = self._create_plugin_list('DEFAULT_CHANNEL_PLUGINS', default=set()) # Make sure to exclude extras self.default_channel_plugins = ( default & self.whitelist_plugins) - self.blacklist_plugins if not hasattr(self, 'enabled_plugins'): # Enabled plugins is a dict: channel -> set() self.enabled_plugins = defaultdict( lambda: self.default_channel_plugins) smokesignal.on('started', self.load)
def test_on_creates_responds_to_fn(self): # Registering a callback should create partials to smokesignal # methods for later user smokesignal.on('foo', self.callback) assert hasattr(self.callback, 'responds_to') assert self.callback.responds_to('foo')
def __init__(self, name, k=10): self.k = k self.fname = _timestamped_filename( "%s-precision-at-%d" % (name, self.k)) smokesignal.on('evaluation_finished', self.on_evaluation_finished) super(PrecisionAtKListener, self).__init__()
def test_clear_many(self): smokesignal.on(('foo', 'bar', 'baz'), self.fn) smokesignal.clear('foo', 'bar') assert smokesignal.receivers == { 'foo': set(), 'bar': set(), 'baz': set([self.fn]), }
def test_on_registers_many(self): assert len(smokesignal._receivers['foo']) == 0 assert len(smokesignal._receivers['bar']) == 0 smokesignal.on(('foo', 'bar'), self.callback) assert len(smokesignal._receivers['foo']) == 1 assert len(smokesignal._receivers['bar']) == 1
def test_clear_all(self): smokesignal.on(('foo', 'bar'), self.callback) assert len(smokesignal._receivers['foo']) == 1 assert len(smokesignal._receivers['bar']) == 1 smokesignal.clear_all() assert len(smokesignal._receivers['foo']) == 0 assert len(smokesignal._receivers['bar']) == 0
def test_on_creates_signals_fn(self): # Registering a callback should create partials to smokesignal # methods for later user smokesignal.on(('foo', 'bar'), self.fn) assert hasattr(self.fn, 'signals') assert 'foo' in self.fn.signals() assert 'bar' in self.fn.signals()
def test_on_creates_signals_fn(self): # Registering a callback should create partials to smokesignal # methods for later user smokesignal.on(('foo', 'bar'), self.callback) assert hasattr(self.callback, 'signals') assert 'foo' in self.callback.signals() assert 'bar' in self.callback.signals()
def test_on_decorator_max_calls_as_arg(self): # Register first - like a decorator smokesignal.on('foo', 3)(self.fn) # Call a bunch of times for x in range(10): smokesignal.emit('foo') assert self.fn.call_count == 3
def __init__(self): if not hasattr(self, 'plugins'): self.plugins = {} if not hasattr(self, 'enabled_plugins'): # Enabled plugins is a dict: channel -> set() self.enabled_plugins = defaultdict(lambda: set(getattr(settings, 'ENABLED_PLUGINS', []))) smokesignal.on('started', self.load)
def test_disconnect(self): # Register first smokesignal.on(('foo', 'bar'), self.fn) assert smokesignal.responds_to(self.fn, 'foo') assert smokesignal.responds_to(self.fn, 'bar') smokesignal.disconnect(self.fn) assert not smokesignal.responds_to(self.fn, 'foo') assert not smokesignal.responds_to(self.fn, 'bar')
def test_on_registers_many(self): assert smokesignal.receivers == {} smokesignal.on(('foo', 'bar'), self.fn) assert smokesignal.receivers == { 'foo': set([self.fn]), 'bar': set([self.fn]), }
def test_disconnect(self): # Register first smokesignal.on(('foo', 'bar'), self.callback) assert smokesignal.responds_to(self.callback, 'foo') assert smokesignal.responds_to(self.callback, 'bar') smokesignal.disconnect(self.callback) assert not smokesignal.responds_to(self.callback, 'foo') assert not smokesignal.responds_to(self.callback, 'bar')
def test_disconnect_from_removes_only_one(self): # Register first smokesignal.on(('foo', 'bar'), self.callback) assert smokesignal.responds_to(self.callback, 'foo') assert smokesignal.responds_to(self.callback, 'bar') # Remove it smokesignal.disconnect_from(self.callback, 'foo') assert not smokesignal.responds_to(self.callback, 'foo') assert smokesignal.responds_to(self.callback, 'bar')
def test_disconnect_from_removes_all(self): # Register first smokesignal.on(('foo', 'bar'), self.callback) assert smokesignal.responds_to(self.callback, 'foo') assert smokesignal.responds_to(self.callback, 'bar') # Remove it smokesignal.disconnect_from(self.callback, ('foo', 'bar')) assert not smokesignal.responds_to(self.callback, 'foo') assert not smokesignal.responds_to(self.callback, 'bar')
def test_disconnect_from_removes_only_one(self): # Register first smokesignal.on(('foo', 'bar'), self.fn) assert smokesignal.responds_to(self.fn, 'foo') assert smokesignal.responds_to(self.fn, 'bar') # Remove it smokesignal.disconnect_from(self.fn, 'foo') assert not smokesignal.responds_to(self.fn, 'foo') assert smokesignal.responds_to(self.fn, 'bar')
def test_disconnect_from_removes_all(self): # Register first smokesignal.on(('foo', 'bar'), self.fn) assert smokesignal.responds_to(self.fn, 'foo') assert smokesignal.responds_to(self.fn, 'bar') # Remove it smokesignal.disconnect_from(self.fn, ('foo', 'bar')) assert not smokesignal.responds_to(self.fn, 'foo') assert not smokesignal.responds_to(self.fn, 'bar')
def test_on_max_calls(self): # Register first smokesignal.on('foo', self.fn, max_calls=3) # Call a bunch of times for x in range(10): smokesignal.emit('foo') assert self.fn.call_count == 3 assert smokesignal.receivers['foo'] == set()
def __init__(self): # Preferred way smokesignal.on('foo', self.foo) # Old way @smokesignal.on('foo') def _bar(): self.bar() self.foo_count = 0 self.bar_count = 0
def test_clear_all(self): smokesignal.on(('foo', 'bar'), self.fn) assert smokesignal.receivers == { 'foo': set([self.fn]), 'bar': set([self.fn]), } smokesignal.clear_all() assert smokesignal.receivers == { 'foo': set(), 'bar': set(), }
def onJoin(self, details): # TEMP: ping the server, let it know we just came up # yield self.call('pd', 'routerConnected', self._session_id) yield self.register(self.ping, 'ping') yield self.register(self.update, 'update') # yield self.register(self.logsFromTime, 'logsFromTime') yield self.register(self.getConfig, 'getConfig') yield self.register(self.setConfig, 'setConfig') # route output to the logs call smokesignal.on('logs', self.logs) yield cxbr.BaseSession.onJoin(self, details)
def test_on_decorator_max_calls(self): # Make a method that has a call count def cb(): cb.call_count += 1 cb.call_count = 0 # Register first - like a cecorator smokesignal.on('foo', max_calls=3)(cb) assert len(smokesignal._receivers['foo']) == 1 # Call a bunch of times for x in range(10): smokesignal.emit('foo') assert cb.call_count == 3
def __init__(self, name, xlabel="", ylabel="", filetype="pdf", chart_looks=None): import matplotlib.pyplot as plt self.name = name self.filetype = filetype self.chart_looks = chart_looks self._charttype = "" self._legend_props = {'prop': {'size': 'x-small'}} self.fig = plt.figure() ax = self.fig.add_axes([0.1, 0.1, 0.8, 0.8], xlabel=xlabel, ylabel=ylabel) ax.set_ylim((0, 1)) self._x = [] self._y = [] smokesignal.on('evaluation_finished', self.on_evaluation_finished) super(Plotter, self).__init__()
def __init__(self): if not hasattr(self, 'plugins'): self.plugins = {} self.plugin_names = set(ep.name for ep in pkg_resources.iter_entry_points('helga_plugins')) # Plugins whitelist/blacklist self.whitelist_plugins = self._create_plugin_list('ENABLED_PLUGINS', default=True) self.blacklist_plugins = self._create_plugin_list('DISABLED_PLUGINS', default=set()) # Figure out default channel plugins using the whitelist and blacklist default = self._create_plugin_list('DEFAULT_CHANNEL_PLUGINS', default=set()) # Make sure to exclude extras self.default_channel_plugins = (default & self.whitelist_plugins) - self.blacklist_plugins if not hasattr(self, 'enabled_plugins'): # Enabled plugins is a dict: channel -> set() self.enabled_plugins = defaultdict(lambda: self.default_channel_plugins) smokesignal.on('started', self.load)
def init(self, core: BBotCore): """ :param bot: :return: """ self.core = core # Initialize the connection @TODO improve this if 'mongodb_uri' not in self.config: raise RuntimeError("FATAL ERR: Missing config var uri") uri = self.config['mongodb_uri'] client = MongoClient(uri) parts = uri.split("/") last_part = parts.pop() parts = last_part.split("?") database_name = parts[0] self.mongo = client[database_name] smokesignal.on(BBotCore.SIGNAL_CALL_BBOT_FUNCTION_AFTER, self.register_function_call) smokesignal.on(BBotCore.SIGNAL_GET_RESPONSE_AFTER, self.register_volley)
def test_max_calls(self): """ Do I decrement _max_calls immediately when the function is called, even when I'm passing through a reactor loop? Discussion: In a naive implementation, max_calls was handled when the function was called, but we use maybeDeferred, which passes the call through a reactor loop. max_calls needs to be decremented before it passes through the reactor, lest we call it multiple times per loop because the decrement hasn't happened yet. """ def cb(): return 19 smokesignal.on('19', cb, max_calls=19) d1 = smokesignal.emit('19') assert list(smokesignal.receivers['19'])[0]._max_calls == 18 d2 = smokesignal.emit('19') assert list(smokesignal.receivers['19'])[0]._max_calls == 17 return defer.DeferredList([d1, d2])
def init(self, core: BBotCore): """ :param bot: :return: """ self.core = core self.logger = BBotLoggerAdapter(logging.getLogger('core_fnc.weather'), self, self.core.bot, '$weather') self.method_name = 'weather' self.accuweather_text = 'Weather forecast provided by Accuweather' self.accuweather_image_url = 'https://static.seedtoken.io/AW_RGB.png' core.register_function( 'weather', { 'object': self, 'method': self.method_name, 'cost': 0.1, 'register_enabled': True }) # we register this to add accuweather text even when result is cached from extensions_cache decorator smokesignal.on(BBotCore.SIGNAL_CALL_BBOT_FUNCTION_AFTER, self.add_accuweather_text)
def init(self, core: BBotCore): """ :param bot: :return: """ self.core = core self.logger = BBotLoggerAdapter(logging.getLogger('ext.token_mgnt'), self, self.core.bot, '$token') smokesignal.on(BBotCore.SIGNAL_CALL_BBOT_FUNCTION_BEFORE, self.function_payment) smokesignal.on(BBotCore.SIGNAL_GET_RESPONSE_AFTER, self.volley_payment) smokesignal.on(BBotCore.SIGNAL_GET_RESPONSE_BEFORE, self.payment_check)
def __init__(self): smokesignal.on('foo', self.foo) self.foo_count = 0
def test_on_creates_disconnect_from_fn(self): smokesignal.on(('foo', 'bar'), self.fn) assert hasattr(self.fn, 'disconnect_from') self.fn.disconnect_from('foo') assert self.fn.signals() == ('bar', )
def test_on_registers(self): assert len(smokesignal._receivers['foo']) == 0 smokesignal.on('foo', self.callback) assert len(smokesignal._receivers['foo']) == 1
def addParadropLogObserver(self, observer): if (self.observers.count(observer) == 0): self.observers.append(observer) if len(self.observers) == 1: smokesignal.on('logs', self.onParadropLog)
def test_on_creates_disconnect_fn(self): smokesignal.on(('foo', 'bar'), self.fn) assert hasattr(self.fn, 'disconnect') self.fn.disconnect() assert self.fn.signals() == tuple()
def __init__(self): smokesignal.on('evaluation_finished', self.on_evaluation_finished) super(CacheEvaluationListener, self).__init__()
def __init__(self, name, beta=1): self.beta = beta self.fname = _timestamped_filename("%s-Fmax" % name) smokesignal.on('evaluation_finished', self.on_evaluation_finished) super(FMaxListener, self).__init__()
def test_clear(self): smokesignal.on('foo', self.fn) assert smokesignal.receivers['foo'] == set([self.fn]) smokesignal.clear('foo') assert smokesignal.receivers['foo'] == set()
def test_emit_with_callback_args(self): # Register first smokesignal.on('foo', self.fn) smokesignal.emit('foo', 1, 2, 3, foo='bar') self.fn.assert_called_with(1, 2, 3, foo='bar')
def test_signals(self): # Register first smokesignal.on(('foo', 'bar'), self.callback) assert 'foo' in smokesignal.signals(self.callback) assert 'bar' in smokesignal.signals(self.callback)
def test_clear(self): smokesignal.on('foo', self.callback) assert len(smokesignal._receivers['foo']) == 1 smokesignal.clear('foo') assert len(smokesignal._receivers['foo']) == 0
def test_emit_with_callbacks(self): # Register first smokesignal.on('foo', self.fn) smokesignal.emit('foo') assert self.fn.called
def test_responds_to_true(self): # Register first smokesignal.on('foo', self.callback) assert smokesignal.responds_to(self.callback, 'foo') is True
def test_on_must_have_callables(self): with pytest.raises(AssertionError): smokesignal.on('foo', 'bar')
def test_responds_to_false(self): # Register first smokesignal.on('foo', self.callback) assert smokesignal.responds_to(self.callback, 'bar') is False