Hi devplayer, thx for the feedback and code. I will add support for annotated listeners shortly and plan on addressing the inspect function deprecation, I just have to find way to maintain compatibility with Python 2.7. And no worries, the older dists are always available (really old ones get hidden but never deleted).
One note: your wrapper uses data as arg, this suggests that you are using arg1 prototcol. The still-supported protocol is kwargs, which means your data would be send as separate objects using keyword arguments.
Cheers,
Oliver
I personally haven't for some time.--But since you're looking at pubsub arg/kwarg1 protocols/API, I'd like to mention a little recipe that I use to get around an exception I get from the inspect package which I think pubsub uses. I get this exception when I use listener functions with argument annotations.Also there's a depreciation warning for Python 3.5 for the inspect package dropping getargspect() or whatever it is called, which I think is used by pubsub. I came across the notice for depreciation for Python 3.5 after I added argument annotations to my listeners.The inspect package's depreciation warning for Python 3.5 says it might drop getargspec() and instead to use inspect.getfullargspect() or inspect.getkwargspect(). I am not sure of the function name.My current workaround is to use a subscribe() decorator which pulls off the annotations and then re-adds them after I pub.sendMessage() in the decorator. The added benefit is that the topic subscription can be before the listener functions def statement and it doesn't invalidate the pub.subscribe() being used elsewhere.With additional modification to the subscribe decorator I suspect the inspect module's use could be removed and the topic tree can be created that way too.# py -3.4+"""author: DevP...@gmail.comdate: 2015-11-26license: GNU Lesser General Public License v 3.0"""import functoolsfrom pubsub import pubdef subscribe(topic, publisher:"You do not have to use the default pubsub.pub instance"=None):""" A decorator for pubsub listeners with a function(data=None) signature. This templateallows function argument annotations to be used with pubsub v3 without throwing anexception. Example usage:@subscribe('server')def audit(data:'Capture only metadata of action.'=None):log_action(data)@subscribe('server.process')def process(data:'State change request.'=None):do_stuff(data)pub.sendMessage('server.process', data=a_request)"""def real_decorator(function):_annotations = function.__annotations__.copy()function.__annotations__.clear()@functools.wraps(function)def wrapper(data=None):return function(data=data)if publisher is None:from pubsub import pubelse:pub = publisherpub.subscribe(wrapper, topic)function.__annotations__.update(_annotations)return wrapperreturn real_decorator
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
@subscribe('server')
def audit(data:'Capture only metadata of action.'=None):
log_action(data)
@subscribe('server.process')
def process(data:'State change request.'=None):
do_stuff(data)
pub.sendMessage('server.process', data=a_request)
def audit(data=None):
log_action(data)
pub.subscribe(audit, "server")
def process(data=None):
do_stuff(data)
pub.subscribe(process, "server.process")
pub.sendMessage('server.process', data=a_request) # data= is an explicit keyword
def some_listener(firstname=None, lastname=None):
do_stuff(firstname, lastname)
pub.subscribe(some_listener, 'database.record.add')
def db_subscribe(topic, publisher:"You do not have to use the default pubsub.pub instance"=None):
def real_decorator(function):
_annotations = function.__annotations__.copy()
function.__annotations__.clear()
@functools.wraps(function)
def wrapper(firstname=None, lastname=None):
return function(firstname=firstname, lastname=lastname)
if publisher is None:
from pubsub import pub
else:
pub = publisher
pub.subscribe(wrapper, topic)
function.__annotations__.update(_annotations)
return wrapper
return real_decorator
@subscribe('log.user.deletes') # SUCCESS correct function signature
def user_deletes(data):
do_delete(data)
@subscribe('db.user.add')
def db_user_add(firstname, lastname): # FAIL wrong function signature
do_stuff()
@subscribe('log.user.deletes') # SUCCESS correct function signature
def user_deletes(data):
do_delete(data)
@db_subscribe('db.user.add') # different decorator
def db_user_add(firstname, lastname): # SUCCESS correct function signature
do_stuff()
@subscribe('server')
def audit(data:'Capture only metadata of action.'=None):
log_action(data)
@subscribe('server.process')
def process(data:'State change request.'=None):
do_stuff(data)
pub.sendMessage('server.process', data=a_request)
def audit(data=None):
log_action(data)
pub.subscribe(audit, "server")
def process(data=None):
do_stuff(data)
pub.subscribe(process, "server.process")
pub.sendMessage('server.process', data=a_request) # data= is an explicit keyword
def some_listener(firstname=None, lastname=None):
do_stuff(firstname, lastname)
pub.subscribe(some_listener, 'database.record.add')
def db_subscribe(topic, publisher:"You do not have to use the default pubsub.pub instance"=None):
def real_decorator(function):
_annotations = function.__annotations__.copy()
function.__annotations__.clear()
@functools.wraps(function)
def wrapper(firstname=None, lastname=None):
return function(firstname=firstname, lastname=lastname)
if publisher is None:
from pubsub import pub
else:
pub = publisher
pub.subscribe(wrapper, topic)
function.__annotations__.update(_annotations)
return wrapper
return real_decorator
@subscribe('log.user.deletes') # SUCCESS correct function signature
def user_deletes(data=None):
do_delete(data)
@subscribe('db.user.add')
def db_user_add(firstname=None, lastname=None): # FAIL wrong function signature
do_stuff()
@subscribe('log.user.deletes') # SUCCESS correct function signature
def user_deletes(data=None):
do_delete(data)
@db_subscribe('db.user.add') # different decorator
def db_user_add(firstname=None, lastname=None): # SUCCESS correct function signature
do_stuff()