Package: observedstruct
observedstructObserved structures that calls your functions on changes.Installobservedstructsupports Python 3.7, 3.8, and 3.9.pip install observedstructAPIobservedstruct.ObservedOperationThese are the operations tracked by the structures:ObservedOperation.AccessObservedOperation.AddObservedOperation.UpdateObservedOperation.RemoveThese values are sent to the callbacks.get_item_for_reference(struct, reference)Find an item inside a structure based on a reference list.observedstruct.ObservedDict__init__(initial, *, pre_callbacks=None, post_callbacks=None)ObservedDictcan be instantiated with anotherdict, and it will recursively turn all nested dictionaries and lists into observed structs as well (by usingObservedDictandObservedList).pre_callbacksare functions to be called before an operation takes place, andpost_callbacksare functions to be called after an operation takes place.fromobservedstructimportObservedDictdefpre_callback(struct,operation,reference,old_value,new_value):passdefpost_callback(struct,operation,reference,old_value,new_value):passd=ObservedDict({'a':1},pre_callbacks=[pre_callback],post_callbacks=[post_callback],)d['b']=2Callbacks are called with:struct: The structure in which the event is taking place. You can find the affected object in the structure by navigating to the reference list inreferencestarting from thestruct.operation: One ofObservedOperation.reference: A list of references inside the structure that can be used to find the affected object. If the structure is a list, it will be an integer, and if it's a dictionary, it will the object used as key. It can be something like['a', 0]for a dictionary that contains keyawhich is a list, and the operation takes place at the first position in the list. You can use the utility functionget_item_for_referenceto find the object the reference refers to from inside callback functions.old_value: The value that will be removed from the structure.new_value: The value that will be added to the structure.This class supports all operations a regular dictionary supports.observedstruct.ObservedListSame as theObservedDictclass, but you instantiate this class with alist.LimitationsAn observed structure can only belong inside one other observed structure. To allow for the callback functions to be found and to be called with the correct references, each nested observed structure stores a reference to its parent and where it can be found in the parent. If an observed structure belongs to more than one parent, then it will have those attributes replaced and one of the parents won't be reachable from the child.Callbacks registered in nested structures will not be called. Once a structure belongs in another structure, only the callbacks from the root structure in the hierarchy are called. Because of this, the multiplication of lists does not work inObservedList, since that would make several instances of a nested structure happen inside a list, and the reference would be wrong for at least one of the occurrences.DevelopmentClone the source code from GitHub, have all supported Python interpreters in yourPATH(we recommend that you usepyenvto manage different Python interpreters and environments) and create a new virtual environment withpoetry install.Run the tests with:./run all-testsTo run a specific test:./run test <test-address>To make sure your code complies with our quality standards, run:./run quality