Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using gossip for objectmethods #22

Open
eplaut opened this issue Dec 11, 2016 · 2 comments
Open

Using gossip for objectmethods #22

eplaut opened this issue Dec 11, 2016 · 2 comments

Comments

@eplaut
Copy link

eplaut commented Dec 11, 2016

how can I use gossip for objectmethods

>>> import gossip
>>> class MyClass(object):
...     def trigger(self):
...         gossip.trigger('my_hook')
...     @gossip.register('my_hook')
...     def my_func(self):
...         print 42
... 
>>> my_object = MyClass()
>>> my_object.trigger()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in trigger
  File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 189, in trigger
    trigger_with_tags(hook_name, kwargs, None)
  File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 199, in trigger_with_tags
    hook.trigger(kwargs or {}, tags)
  File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 152, in trigger
    exception_policy.handle_exception(ctx, exc_info)
  File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/exception_policy.py", line 29, in handle_exception
    reraise(exc_info[0], exc_info[1], exc_info[2])
  File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/hooks.py", line 169, in _call_registration
    registration(**kwargs)  # pylint: disable=star-args
  File "/home/eli/qa/.venv/local/lib/python2.7/site-packages/gossip/registration.py", line 70, in __call__
    return self.func(*args, **kwargs)
TypeError: my_func() takes exactly 1 argument (0 given)

since the self parameter is not sent

@edouardpoitras
Copy link

edouardpoitras commented Dec 11, 2016

If you're just using static methods and aren't making use of the instance variable 'self', then you can try removing it as a parameter entirely:

import gossip
class MyClass(object):
    def trigger(self):
        gossip.trigger('my_hook')
    @gossip.register('my_hook')
    def my_func():
        print 42

Although presumably your my_func needs access to the object instance or you wouldn't be using object oriented code :)
The only way I got that working was by re-naming the self parameter:

import gossip
class Test:
    def hello(self):
        self.message = 'Hello World!'
        gossip.trigger('hello_world', itself=self)
    @gossip.register('hello_world')
    def world(itself):
        print(itself.message)
test = Test()
test.hello()
# Prints 'Hello World!'

However, I may be missing something. I'm not terribly familiar with the code.

Edit: You of course would need to pass an object instance when triggering that hook from outside the class.

@vmalloc
Copy link
Member

vmalloc commented Dec 11, 2016

Registering class methods isn't supported because gossip doesn't know which instance to call...

You can easily achieve a similar result by:

class Blap(object):
    def callback(self, a, b, c):
        pass

obj = Blap()
gossip.register(obj.callback, 'some_hook')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants