Skip to content

Instantly share code, notes, and snippets.

@EliAndrewC
Created November 22, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliAndrewC/cbfdb7a259916ac9493c18f36360e44e to your computer and use it in GitHub Desktop.
Save EliAndrewC/cbfdb7a259916ac9493c18f36360e44e to your computer and use it in GitHub Desktop.
Example of customized subscriptions
from collections import defaultdict
from sideboard.lib import notify, listify, threadlocal
__all__ = ['get', 'send']
ecards = defaultdict(list)
class user_subscribes(object):
def __init__(self, func):
self.func = func
@property
def subscribes(self):
params = listify(threadlocal.get('message', {}).get('params', []))
if isinstance(params, dict):
return ['ecards.' + params['username']]
else:
return ['ecards.' + params[0]]
def __call__(self, *args, **kwargs):
return self.func(*args, **kwargs)
@user_subscribes
def get(username):
return ecards[username]
def send(username, message):
ecards[username].append(message)
notify('ecards.' + username)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment