Package: managedstate
managedstateState management inspired by ReduxQuickstartSetupfrommanagedstateimportStateinitial_state={"first_key":[{"id":1,"second_key":True},{"id":2,"second_key":False}]}state=State(initial_state=initial_state)Getting the stateThe full state object>>>state.get(){'first_key':[{'id':1,'second_key':True},{'id':2,'second_key':False}]}A sub-state object>>>state.get(["first_key",0,"second_key"],defaults=[[],{},False])TrueA sub-state object, using a query functiondefid_is_1_query(first_key_list):forindex,objinenumerate(first_key_list):ifobj["id"]==1:returnindex>>>state.get(["first_key",KeyQuery(id_is_1_query),"second_key"],defaults=[[],{},False])TrueSetting the stateThe full state object>>>state.set({'first_key':[{'id':3,'second_key':True},{'id':4,'second_key':False}]})>>>state.get(){'first_key':[{'id':3,'second_key':True},{'id':4,'second_key':False}]}A sub-state object, using a query functiondefget_id_key_query(target_id):# This will dynamically create the query we need, when we need itdefid_query(substate):forindex,objinenumerate(substate):ifobj["id"]==target_id:returnindexreturnKeyQuery(id_query)>>>state.set(False,['first_key',get_id_key_query(3),'second_key'],defaults=[[],{}])>>>state.get(){'first_key':[{'id':3,'second_key':False},{'id':4,'second_key':False}]}FunctionalityDependenciesThe State class and the extensions in this package implement Extendable and Extension respectively, fromobjectextensions.
As such, applying extensions is done by calling the class methodState.with_extensions()and passing in the extension classes to be applied.Example code:frommanagedstateimportStatefrommanagedstate.extensionsimportRegistrarstate=State.with_extensions(Registrar)()Extensionsextensions.RegistrarAllows specific get and set operations to be registered under a shorthand label for ease of use later.extensions.ListenersProvides an easy way to attach observer methods that will be called immediately afterset()and/orget().Data ClassesAttributeName(self, attribute_name: str)An instance of this class should be provided as a path key when getting or setting the state,to indicate that the next nesting level of the state should be accessed via an object attribute.Note: As this class is used indirectly to determine the method of access into the state,it should never be stored directly as a key within that state.KeyQuery(self, path_key_getter: Callable[[Any], Any])Instances of this class can be provided as path keys when getting or setting the state,to indicate that the next nesting level of the state should be accessed via the path key returnedfrom its stored function.The function will receive a copy of the state object at the current level of nestingin order to determine what key to return.extensions.PartialQuery(self, path_key_getter: Callable[[Any], Any])Instances of this class can be provided as path keys only inRegistrar.register_path().Whenregistered_get()/registered_set()is called with the relevant path label, the function provided belowwill be called and passed one value from the custom query args list;a valid path key or KeyQuery should be returned.Propertiesextensions.Registrar.registered_pathsReturns a copy of the current path registry.MethodsState.get(self, path_keys: Iterable[Any] = (), defaults: Iterable[Any] = ())Drills into the state object using the provided path keys in sequence.Any time progressing further into the state object fails, a copy of the default value at the relevant indexof defaults is substituted in.Returns a copy of the drilled-down state object.Thedefaultsparam may be provided any number of default values, and they will only be used as necessary.State.set(self, value: Any, path_keys: Iterable[Any] = (), defaults: Iterable[Any] = ())Drills into the state object using the provided path keys in sequence.Any time progressing further into the state object fails, a copy of the default value at the relevant indexof defaults is substituted in.The final path key is used as the index to store a copy of the provided value atinside the drilled-down state object.Thedefaultsparam may be provided any number of default values, and they will only be used as necessary.extensions.Registrar.register_path(self, registered_path_label: str, path_keys: Iterable[Any], defaults: Iterable[Any] = ())Saves the provided path keys and defaults under the provided label, so that a custom get or set can becarried out at later times simply by providing the label again in a call toregistered_get()orregistered_set().extensions.Registrar.get_shape(self, initial_state: Any = None)Generates a default shape for the state, using the current registered paths.Any registered paths containing PartialQuery objects are truncated for this purpose, as it is not possibleto determine what kind of value a PartialQuery object would provide to drill further into the state.extensions.Registrar.registered_get(self, registered_path_label: str, custom_query_args: Iterable[Any] = ())Callsget(), passing in the path keys and defaults previously provided inregister().If any of these path keys are instances of PartialQuery, each will be called and passed one value fromthe custom query args list and is expected to return a valid path key or KeyQuery.extensions.Registrar.registered_set(self, value: Any, registered_path_label: str, custom_query_args: Iterable[Any] = ())Callsset(), passing in the path keys and defaults previously provided inregister().If any of these path keys are instances of PartialQuery, each will be called and passed one value fromthe custom query args list and is expected to return a valid path key or KeyQuery.extensions.Listeners.add_listener(self, method_name: str, listener: Callable[[dict], None])Adds the provided listener to a set of callbacks for the specified method.These callbacks will receive copies of the method return value and its argumentsin the formresult, self, *args, **kwargs.extensions.Listeners.remove_listener(self, method_name: str, listener: Callable[[dict], None])Removes the provided listener from the set of callbacks for the specified method.Additional InfoKeyQuery instances provided as path keys can return any valid path key,exceptanother KeyQuery or a PartialQuerySimilarly, PartialQuery instances can return any valid path key except for another PartialQuery (they can however return a KeyQuery)The data classes provided in this package are not designed to be stored inside the state object themselves. Doing so may result in unintended behaviour