from __future__ import print_function
import inspect
from weakref import WeakSet, WeakKeyDictionary


class Signal(object):
    def __init__(self):
        self._functions = WeakSet()
        self._methods = WeakKeyDictionary()

    def __call__(self, *args, **kargs):
        res = []
        # Call handler functions
        for func in self._functions:
            res.append (func(*args, **kargs))

        # Call handler methods
        for obj, funcs in self._methods.items():
            for func in funcs:
                res.append (func(obj, *args, **kargs))
        return res

    def connect(self, slot):
        if inspect.ismethod(slot):
            if slot.__self__ not in self._methods:
                self._methods[slot.__self__] = set()

            self._methods[slot.__self__].add(slot.__func__)

        else:
            self._functions.add(slot)

    def disconnect(self, slot):
        if inspect.ismethod(slot):
            if slot.__self__ in self._methods:
                self._methods[slot.__self__].remove(slot.__func__)
        else:
            if slot in self._functions:
                self._functions.remove(slot)

    def clear(self):
        self._functions.clear()
        self._methods.clear()

@@ -1,12 +1,45 @@
+%22%22%22%0ASimple signalling system%0A%22%22%22%0A
 from __futur
@@ -146,24 +146,65 @@
 al(object):%0A
+    %22%22%22%0A    The Signalling class%0A    %22%22%22%0A
     def __in
@@ -251,16 +251,58 @@
 akSet()%0A
+        self._after_functions = WeakSet()%0A
         
@@ -336,16 +336,66 @@
 ionary()
+%0A        self._after_methods = WeakKeyDictionary()
 %0A%0A    de
@@ -533,25 +533,24 @@
   res.append
- 
 (func(*args,
@@ -702,687 +702,1985 @@
 pend
- (func(obj, *args, **kargs))%0A        return res%0A%0A    def connect(self, slot):%0A        if inspect.ismethod(slot):%0A            if slot.__self__ not in self._methods:%0A                self._methods%5Bslot.__self__%5D = set()%0A%0A            self._methods%5Bslot.__self__%5D.add(slot.__func__)%0A%0A        else:%0A            self._functions.add(slot)%0A%0A    def disconnect(self, slot):%0A        if inspect.ismethod(slot):%0A            if slot.__self__ in self._methods:%0A                self._methods%5Bslot.__self__%5D.remove(slot.__func__)%0A        else:%0A            if slot in self._functions:%0A                self._functions.remove(slot)%0A%0A    def clear(self):%0A        self._functions.clear()%0A        self.
+(func(obj, *args, **kargs))%0A%0A        for func in self._after_functions:%0A            res.append(func(*args, **kargs))%0A%0A        # Call handler methods%0A        for obj, funcs in self._after_methods.items():%0A            for func in funcs:%0A                res.append(func(obj, *args, **kargs))%0A        return res%0A%0A    def connect(self, slot):%0A        %22%22%22%0A        @slot: The method to be called on signal emission%0A%0A        Connects to @slot%0A        %22%22%22%0A        if inspect.ismethod(slot):%0A            if slot.__self__ not in self._methods:%0A                self._methods%5Bslot.__self__%5D = set()%0A%0A            self._methods%5Bslot.__self__%5D.add(slot.__func__)%0A%0A        else:%0A            self._functions.add(slot)%0A%0A    def connect_after(self, slot):%0A        %22%22%22%0A        @slot: The method to be called at last stage of signal emission%0A%0A        Connects to the signal after the signals has been handled by other%0A        connect callbacks.%0A        %22%22%22%0A        if inspect.ismethod(slot):%0A            if slot.__self__ not in self._after_methods:%0A                self._after_methods%5Bslot.__self__%5D = set()%0A%0A            self._after_methods%5Bslot.__self__%5D.add(slot.__func__)%0A%0A        else:%0A            self._after_functions.add(slot)%0A%0A    def disconnect(self, slot):%0A        %22%22%22%0A        Disconnect @slot from the signal%0A        %22%22%22%0A        if inspect.ismethod(slot):%0A            if slot.__self__ in self._methods:%0A                self._methods%5Bslot.__self__%5D.remove(slot.__func__)%0A            elif slot.__self__ in self._after_methods:%0A                self._after_methods%5Bslot.__self__%5D.remove(slot.__func__)%0A        else:%0A            if slot in self._functions:%0A                self._functions.remove(slot)%0A            elif slot in self._after_functions:%0A                self._after_functions.remove(slot)%0A%0A    def clear(self):%0A        %22%22%22%0A        Cleanup the signal%0A        %22%22%22%0A        self._functions.clear()%0A        self._methods.clear()%0A        self._after_functions.clear()%0A        self._after
 _met

Implement connect_after