hi,is it possible to work with hard references?
--
You received this message because you are subscribed to the Google Groups "pypubsub" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pypubsub+u...@googlegroups.com.
To post to this group, send email to pypu...@googlegroups.com.
Visit this group at http://groups.google.com/group/pypubsub.
For more options, visit https://groups.google.com/d/optout.
from pubsub import pub
def handler1(): print("handler1 is running")pub.subscribe(handler1, "someEvent")
def init(config): # doing some other works here def handler2(): print("handler2 is running") pub.subscribe(handler2, "someEvent")init({})pub.sendMessage("someEvent")
def listen(arg1, arg2, arg3):passobj.subscribe(listen, 'topic', arg1=1, arg3=3)pub.sendMessage('topic', arg2='a') # calls listen(1, 'a', 3)pub.sendMessage('topic', arg2='b') # calls listen(1, 'b', 3)pub.sendMessage('topic', arg2='c') # calls listen(1, 'c', 3)
Le 26 août 2015 à 14:58, oliver <oliver.s...@gmail.com> a écrit :You didn't miss anything. You would have to pass curried args by name, just like you have to pass named arguments when sending a message (if only to support currying non-contiguous positional parameters). Subscribe() currently takes a listener callable and topic name, so the API should be extendable with a **kwargs, allowing you to do this:def listen(arg1, arg2, arg3):passobj.subscribe(listen, 'topic', arg1=1, arg3=3)pub.sendMessage('topic', arg2='a') # calls listen(1, 'a', 3)pub.sendMessage('topic', arg2='b') # calls listen(1, 'b', 3)pub.sendMessage('topic', arg2='c') # calls listen(1, 'c', 3)Would that work? The objects in the dict would have to be strong referenced which is not great (I would prefer to stick with the policy that when an object is no longer used outside of pubsub, it can be destroyed). So you'd have to be careful what you bind, although at most the "artificial" lifetime due to strong referencing by pubsub would be that of the listener, perhaps not too bad.
def listener(a, b, c):print(a, b, c)pub.subscribe(listener, 'topic')pub.sendMessage('topic', b=2, c=3)pub.sendMessage('topic', a=1, b=2, c=3)
# "a" is "curried" arg, specific to this listener:
pub.subscribe(listener, 'topic', a=4)
pub.subscribe(listener, 'topic', a=4, _when_=UseCurry.always) # case 1pub.subscribe(listener, 'topic', a=4, _when_=UseCurry.sendDefault) # case 2pub.subscribe(listener, 'topic', a=4, _when_=UseCurry.notInTopic) # case 3
Le 23 déc. 2015 à 19:08, Oliver <Oliver.S...@gmail.com> a écrit :Hi Jerome (and anyone else interested in this feature -- please feel free to chip in), I'm working to implement curried args, and need some input on your use case. Say you have the following:def listener(a, b, c):print(a, b, c)pub.subscribe(listener, 'topic')pub.sendMessage('topic', b=2, c=3)pub.sendMessage('topic', a=1, b=2, c=3)and the topic defines "a" as optional with default value 0. Then you would expect the first send to print "0 2 3" and the second one to print "1 2 3". But what would you expect to see printed if you instead subscribed listener with a curried arg that overlaps the optional topic arg:# "a" is "curried" arg, specific to this listener:pub.subscribe(listener, 'topic', a=4)What is the most intuitive contract:
- user expects "listener will always get a=2, regardless of what sender sends": in this case both prints are "4 2 3": curried value always overrides sendMessage
- user expects "listener will only get this curried value if the user does not specify "a" in the sendMessage": then first sendMessage prints "4 2 3", but second prints "1 2 3"; curried value only overrides default
- user expects "listener will always get the topic message value, if there is one": then first sendMessage prints "0 2 3", second prints "1 2 3", and the curried value is never actually used if topic defines it; curred value only used if not defined by topic
Case 2 seems rather unlikely, but is still valid. Not sure what is more intuitive, case 1 or case 3. Any opinion? I was thinking of using a "specially named kwarg" in sendMessage, say, "_when_" as inpub.subscribe(listener, 'topic', a=4, _when_=UseCurry.always) # case 1pub.subscribe(listener, 'topic', a=4, _when_=UseCurry.sendDefault) # case 2pub.subscribe(listener, 'topic', a=4, _when_=UseCurry.notInTopic) # case 3Let me know your thoughts ASAP. Thanks,Oliver
pub.subscribe(listener, 'topic', a=4) # case 1pub.subscribe(listener, 'topic', a=4, _overrideSent_=False) # case 3
Le 23 déc. 2015 à 19:38, Oliver <Oliver.S...@gmail.com> a écrit :IMO the listener should exactly behave as if the curried argument disappeared from its signature, so this would raise a TypeError.Best regardsJérôme LaheurteMakes sense too. So this would be the default.
Le 30 déc. 2015 à 06:58, Oliver <Oliver.S...@gmail.com> a écrit :Jerome,Code committed. You can now writepub.subscribe(listener, topic, **curriedArgs)Let me know if it all works for you.
Le 30 déc. 2015 à 06:58, Oliver <Oliver.S...@gmail.com> a écrit :
Jerome,Code committed. You can now writepub.subscribe(listener, topic, **curriedArgs)Let me know if it all works for you.
class MyListeners:def __init__(self, ...): ...def listener_choose_dir(arg): ...my_listeners = Nonedef main(page: Page):global my_listenersmy_listeners = MyListeners(page, ...)pub.subscribe(listener_chose_dir, 'chose_dir')flet.app(target=main)