Welcome to responsive-data¶
The basic idea of this library is to change a data to be responsive on change. The next code should demonstrate the basic functionality:
demo1.py
The output looks like following (you have to scroll a bit to see whole output).
"""Demo code."""
from responsive.data import make_responsive
from responsive.observer import OutputObserver
if __name__ == "__main__":
subject = make_responsive(
{
"some_str": "string 1",
"some_int": 1234567890,
"some_list": [1, 2, 3, 4, 5, {"inner_str": "string 2"}],
"some_dict": {"some_other_str": "string 3"},
}
)
subject.add_observer(OutputObserver())
# changing string field
subject.some_str = "another string"
# changing integer field
subject.some_int = 9876543210
# changing dictionary in a list
subject.some_list[-1].inner_str = "just another string"
# appending a value to a list field
subject.some_list.append(6)
# removing a value from a list field
subject.some_list.remove(3)
# change value by index
subject.some_list[2] = 7
# changing list field to another list
subject.some_list = [5, 4, 3, 2, 1, 0]
# changing string field of a nested dictionary
subject.some_dict.some_other_str = "yet another string"
# changing dictionary in total
subject.some_dict = {"some_other_str": "string 4"}
# changing string field of a nested dictionary (after replacement)
subject.some_dict.some_other_str = "string 5"
subject with id 4309460944 has notified with () and {'id': 4309460944, 'context': <Context.DICTIONARY: 2>, 'name': 'some_str', 'old': 'string 1', 'new': 'another string', 'operation': <Operation.VALUE_CHANGED: 1>}
subject with id 4309460944 has notified with () and {'id': 4309460944, 'context': <Context.DICTIONARY: 2>, 'name': 'some_int', 'old': 1234567890, 'new': 9876543210, 'operation': <Operation.VALUE_CHANGED: 1>}
subject with id 4309460944 has notified with () and {'id': 4309463248, 'context': <Context.DICTIONARY: 2>, 'name': 'inner_str', 'old': 'string 2', 'new': 'just another string', 'operation': <Operation.VALUE_CHANGED: 1>}
subject with id 4309460944 has notified with () and {'id': 4309461616, 'context': <Context.LIST: 3>, 'new': 6, 'operation': <Operation.VALUE_ADDED: 2>}
subject with id 4309460944 has notified with () and {'id': 4309461616, 'context': <Context.LIST: 3>, 'old': 3, 'operation': <Operation.VALUE_REMOVED: 3>}
subject with id 4309460944 has notified with () and {'id': 4309461616, 'context': <Context.LIST: 3>, 'index': 2, 'old': 4, 'new': 7, 'operation': <Operation.VALUE_CHANGED: 1>}
subject with id 4309460944 has notified with () and {'id': 4309461616, 'context': <Context.LIST: 3>, 'operation': <Operation.VALUE_CHANGED: 1>}
subject with id 4309460944 has notified with () and {'id': 4309453936, 'context': <Context.DICTIONARY: 2>, 'name': 'some_other_str', 'old': 'string 3', 'new': 'yet another string', 'operation': <Operation.VALUE_CHANGED: 1>}
For detailed documentations please follow the links: