<jupyter_start><jupyter_text>Using the subject/observer pattern<jupyter_code>import sys
sys.path.append('../..')<jupyter_output><empty_output><jupyter_text>The subject/observer pattern requires in this library to import following (as needed):<jupyter_code>from responsive.subject import Subject
from responsive.observer import DefaultObserver<jupyter_output><empty_output><jupyter_text>Testing the subject/observer feature. The notification allows specifying no parameters, positional parameter or keyword arguments or both. It is in the responsiblity of the concrete subject to provide sufficient information to the observers. We are using a default server implementation which is - to be clear - mainly for testing and demonstration purpose.<jupyter_code>observer = DefaultObserver()
subject = Subject()
subject.add_observer(observer)
subject.notify('just a test', reason="test")

for subject, args, kwargs in observer:
    print(f"subject has notified with {args} and {kwargs}")<jupyter_output>subject has notified with ('just a test',) and {'reason': 'test'}<jupyter_text>Now we change the **interest** of the observer and we register for the concrete interest.<jupyter_code>observer.set_interests({"reason": lambda value: value == "test2"})
observer.clear()

subject.notify(reason="test1", message="it's a test")
print(list(kwargs for _, _, kwargs in observer))
subject.notify(reason="test2", message="it's another test")
print(list(kwargs for _, _, kwargs in observer))<jupyter_output>[]
[{'reason': 'test2', 'message': "it's another test"}]
{'fraction_non_alphanumeric': 0.10989010989010989, 'fraction_numerical': 0.002585649644473174, 'mean_word_length': 7.278074866310161, 'pattern_counts': {'":': 1, '<': 15, '<?xml version=': 0, '>': 15, '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}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '610499', 'license': 'MIT', 'n_tokens_mistral': 440, 'n_tokens_neox': 422, 'n_words': 147, 'path': 'docs/notebooks/subject-observer.ipynb', 'repo_name': 'thomas-lehmann-private/responsive-data', 'size': '1547'}