<gh_stars>0
"""
    Question: Write a class contains three functions that will
    register the callback with its id, unregister specific callback from
    a signal id,and runs all the callback associated with specific signal id
   
    Hint: Think about how to store(using data structure)
    the signal id and callback
"""


class SignalRegister:
    # to keep the persistence of the callbacks we declare
    # global dictionary for signal id and set of callbacks
    # { signal_id: {callback1}}
    registered_callbacks = {}

    def register(self, signal_id, callback):
        """
            Takes two arguments signal id and callback. Registers the signal id
            with its associated callback
        """
        # check if signal_id is registered before
        if signal_id not in self.registered_callbacks:
            # add initialize the set with new callback
            callback = set([callback])
            # if not then initialize a set with a single callback
            self.registered_callbacks[signal_id] = callback
        else:  # if signal id already registered
            # simply add new callback to its set of values
            self.registered_callbacks[signal_id].add(callback)

    def unregister(self, signal_id, callback):
        """
            Takes two arguments signal id and callback. And unregisters the specific 
            callback from its signal id
        """
        # if the callback was registered the remove it 
        if signal_id in self.registered_callbacks:
            self.registered_callbacks[signal_id].remove(callback)
        

    def signal(self, signal_id):
        """
            Takes signal id and runs all the callbacks are associated with
        """
        # if signal id is registered runs all its callbacks
        if signal_id in self.registered_callbacks:
            for callback in self.registered_callbacks[signal_id]:
                callback()


def a():
    print("It is a!")


def b():
    print("It is b!")

def c():
    print("It is c!")
def d():
    print("It is d!")


sr = SignalRegister()
sr.register(2, a)
sr.register(2, b)
sr.register(3, c)
sr.register(2, d)
sr.register(3, d)
print(f"Registered signal 2 and 3")
sr.signal(2) 
# sr.signal(3)


# sr.unregister(2, a)
# print("After unregister callback: ")
# sr.signal(2)
# sr.signal(3)

"""
    Question: Write a class contains three functions that will
    register the callback with its id, unregister specific callback from
    a signal id,and runs all the callback associated with specific signal id
   
    Hint: Think about how to store(using data structure)
    the signal id and callback
"""


class SignalRegister:
    # to keep the persistence of the callbacks we declare
    # global dictionary for signal id and set of callbacks
    # { signal_id: {callback1}}
    registered_callbacks = {}

    def register(self, signal_id, callback):
        """
            Takes two arguments signal id and callback. Registers the signal id
            with its associated callback
        """
        # check if signal_id is registered before
        if signal_id not in self.registered_callbacks:
            # add initialize the set with new callback
            callback = set([callback])
            # if not then initialize a set with a single callback
            self.registered_callbacks[signal_id] = callback
        else:  # if signal id already registered
            # simply add new callback to its set of values
            self.registered_callbacks[signal_id].add(callback)

    def unregister(self, signal_id, callback):
        """
            Takes two arguments signal id and callback. And unregisters the specific 
            callback from its signal id
        """
        # if the callback was registered the remove it 
        if signal_id in self.registered_callbacks:
            self.registered_callbacks[signal_id].remove(callback)
        

    def signal(self, signal_id):
        """
            Takes signal id and runs all the callbacks are associated with
        """
        # if signal id is registered runs all its callbacks
        if signal_id in self.registered_callbacks:
            for callback in self.registered_callbacks[signal_id]:
                callback()


def a():
    print("It is a!")


def b():
    print("It is b!")

def c():
    print("It is c!")
def d():
    print("It is d!")


sr = SignalRegister()
sr.register(2, a)
sr.register(2, b)
sr.register(3, c)
sr.register(2, d)
sr.register(3, d)
print(f"Registered signal 2 and 3")
sr.signal(2) 
# sr.signal(3)


# sr.unregister(2, a)
# print("After unregister callback: ")
# sr.signal(2)
# sr.signal(3)
Question: Write a class contains three functions that will     register the callback with its id, unregister specific callback from     a signal id,and runs all the callback associated with specific signal id         Hint: Think about how to store(using data structure)     the signal id and callback # to keep the persistence of the callbacks we declare # global dictionary for signal id and set of callbacks # { signal_id: {callback1}} Takes two arguments signal id and callback. Registers the signal id             with its associated callback # check if signal_id is registered before # add initialize the set with new callback # if not then initialize a set with a single callback # if signal id already registered # simply add new callback to its set of values Takes two arguments signal id and callback. And unregisters the specific              callback from its signal id # if the callback was registered the remove it Takes signal id and runs all the callbacks are associated with # if signal id is registered runs all its callbacks # sr.signal(3) # sr.unregister(2, a) # print("After unregister callback: ") # sr.signal(2) # sr.signal(3)