<commit_before>"""
===============
Observer (old)
===============

::author::
    <NAME>
::license::
    MIT License
"""


class Observable(object):

    events = {}  # categorizes observers by event

    def on(self, event, observer):  # observer must be a callable object
        if event not in self.events:
            self.events[event] = []
            setattr(self, event, lambda: self.notify(event))  # self.event()
        self.events[event].append(observer)

    def notify(self, event):  # notifies observers by event
        for observer in self.events[event]:
            observer(self)  # observer.__call__(self)

if __name__ == '__main__':
    class HelloObserver:

        def __init__(self):
            self.msg = 'Papai noel'

        def __call__(self, source):  # observer handler
            print(source.count, self.msg)
            source.count += 1

    def another_observer(source):  # funcitons are callable objects
        print(source.count, "Ai dentro")
        source.count += 1

    subject = Observable()
    # Attaching observers to hello event of subject
    subject.on('hello', HelloObserver())
    subject.on('hello', another_observer)
    # changing subject state
    subject.count = 0
    # notifing observers
    subject.hello()  # obj.notify('hello')
    # Result:
    #   0 Ai dentro
    #   1 Papai Noel<commit_msg>Refatorando codigo antigo.<commit_after>"""
===============
Observer (old)
===============

::author::
    <NAME>
::license::
    MIT License
"""


class Observable(object):

    events = {}  # categorizes observers by event

    def on(self, event, observer):  # observer must be a callable object
        if event not in self.events:
            self.events[event] = []
            setattr(self, event, lambda: self.notify(event))  # self.event()
        self.events[event].append(observer)

    def notify(self, event):  # notifies observers by event
        for observer in self.events[event]:
            observer(self)  # observer.__call__(self)

if __name__ == '__main__':
    class Observer:

        def __init__(self):
            self.msg = 'I saw it.'

        def __call__(self, source):  # observer handler
            print(source.count, self.msg)

    def another_observer(source):  # funcitons are callable objects
        print(source.count, "Me too!")

    subject = Observable()
    subject.count = 0
    print('Attaching a observer to the event')
    subject.on('hello', Observer())
    subject.count = 1  # changing subject state
    subject.notify('hello')  # notifying observers of hello event
    print('Attaching another observer...')
    subject.on('hello', another_observer)
    subject.count = 2
    subject.hello()

    # OUTPUT #
    # Attaching a observer to hello event
    # 1 I saw it.
    # Attaching another observer ...
    # 2 I saw it.
    # 2 Me too!
{'fraction_non_alphanumeric': 0.13232713232713234, 'fraction_numerical': 0.003861003861003861, 'mean_word_length': 2.616751269035533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1625979', 'n_tokens_mistral': 845, 'n_tokens_neox': 795, 'n_words': 239}