Package: dicta
dictaA dict subclass that observes a nested dict and listens for changes in its data structure. If a data change is registered, Dicta reacts with a callback or a data-export to a JSON file.Core FunctionalityDetect data changes in a nested dictThrow a callback, when the nested data structure changesWrite data to a JSON file, when the nested data structure changesEasily import & export JSON files to/from a nested dictFeaturesBehaves like a regulardictand supports alldict,list,tupleandsetmethods.Supports nesting of all possible datatypes likedict,list,tuple,setand other objects like custom classes.Encodes non-serializable objects to a binary-string when writing data to a file (Optional / deactivated by default).Reading data from a file will decode a binary-string back to a non-serializable object (Optional / deactivated by default).Import/Insert additional data from json files.Export data to json files.Installpip3 install dictaHow to useimportdicta# Declare the 'Dicta' class.dicta=dicta.Dicta()# Activate binary serializationdicta.setBinarySerializer(True)# Set a synch file path.dicta.synchFile("data.json")# Define the callback methoddefcallback():print("Data changed!")print(dicta)# Bind the callback method to dictadicta.bind(callback)# Add datadicta.importData({"key":"value"})dicta.importData(key2=value2,key3=value3)dicta["entities"]={}dicta["entities"]["persons"]=[]dicta["entities"]["persons"].append({"name":"john","age":23})dicta["entities"]["persons"].append({"name":"peter","age":13})# Update a key in a dictdicta["entities"]["persons"][1]["age"]=42# Add another nested list to the dictdicta["entities"]["animals"]=[]dicta["entities"]["animals"].append("lion")dicta["entities"]["animals"].append("elephant")# Slice item from listdeldicta["entities"]["animals"][0:1]# Remove item from dictdicta["entities"].pop("persons")# and so forth…# Should support all regular dict behaviours and# list methods (pop(), append(), slice(), insert() …)# Import additional data from another file.# (New data will be added. Old data remains but will# be overwritten if dict keys match.)dicta.importFile("additional_data_file.json")# Export the data to another filedicta.exportFile("data_backup.json")# Get string representation of the Dictadicta.stringify()ReferenceDicta()Dicta(*args,**kwargs)Dicta(dict)Dicta(key=value,key2=value)A dict subclass.Parameter*args(Optional)**kwargs(Optional)ReturnDicta ClassMethodsDicta MethodsDicta.bind()Dicta.bind(callback,response=False,*args,*kwargs)Sets the callback method for the Dicta Class. Ifresponse=False(default) the callback method only gets the*args, *kwargsas parameters you define. Ifresponse=Truethe callback method gets response from the Dicta Class. You should define your callback function with a*kwargsparameter or with three positional parameters:def my_callback(**kwargs)ordef my_callback(modifed_object, modify_info, modify_trace)Parametercallback(method)default_response(bool) (optional / default = False)Callbackargsas defined in setCallback(optional / default: None)kwargsas defined in setCallback(optional / default: None)modifed_object(object)modify_info(json_string): Contains info about the data modmodify_trace(list): Contains the dict-tree-path to the modified object as a list starting from root*Dicta.syncFile()Dicta.syncFile(path,reset=False)Sets the sync file to automatically store the data on data change. Ifreset=False(default) old data will remain and will be updated with new data . Ifreset=Truethe data wil be cleared whensyncFile()is called.This will fail if your dict contains non-serializable objects and binary serialization is not activated.For security reasons this is deactivated by default. You can activate binary serialization manually withDicta.useBinarySerializer(True).If you activate the binary-serializer all non-serializable objects will be encoded to a binary string and packed into adictlabeled with the key'<serialized-object>'. See the reference forDicta.useBinarySerializer().Parameterpath(string)reset(bool) (optional / default = False)Dicta.importFile()Dicta.importFile(path)Import data from a file. New data will be added to the DictObsercer, old data remains but will be overwritten if dict keys match.Parameterpath(string)Dicta.exportFile()Dicta.exportFile(path,reset=True)Export data to a file. Ifreset=Truethe data wil be cleared whenexportFile()(default) is called . Ifreset=Falsethe data will be updated.This will fail if your dict contains non-serializable objects and binary serialization is not activated.For security reasons this is deactivated by default. You can activate binary serialization by callingDicta.useBinarySerializer(True)before.If you activate the binary-serializer all non-serializable objects will be encoded to a binary string and packed into adictlabeled with the key'<serialized-object>'. See the reference forDicta.useBinarySerializer().Parameterpath(string)reset(bool) (optional / default = True)Dicta.clearFile()Dicta.clearFile(path)Clear a file.Parameterpath(string)Dicta.removeFile()Dicta.removeFile(path)Remove a data file.Parameterpath(string)Dicta.importData(*args,**kwargs)Dicta.importData(dict)Dicta.importData(key=value,key2=value2…)Import data as dict or key/value pairs.Dicta.dictify()Dicta.dictify()Returns a plain dict representation of the data without Dicta functionality.ParameterNoneReturndictDicta.stringify()Dicta.stringify(returnBinaries=False)Returns a string representation of the data in Dicta.This will fail if your dict contains non-serializable objects and binary serialization is not activated.For security reasons this is deactivated by default. You can activate binary serialization by callingDicta.useBinarySerializer(True)before.If you activate the binary-serializer all non-serializable objects will be encoded to a binary string and packed into adictlabeled with the key'<serialized-object>'. See the reference forDicta.useBinarySerializer().For better readability serialized objects won´t be returned by default and are replaced by a the'<serialized-object>'hook. If you want to return the binaries set thereturn_binariesparameter toTrue.Parameterreturn_binaries(bool) (default = False)ReturnstringDicta.setBinarySerializer()Dicta.setBinarySerializer(binary_serializer=False,serializer_hook='<serialized-object>')For security reasons binary serialization of non-serializable objects is deactivated by default. You can activate or deactivate binary serialization with this method (default=False).If you activate the binary-serializer all non-serializable objects will be encoded to a binary string and packed into a dict labeled with the key'<serialized-object>'. In case you need this key for your data structure, define a custom serializer-hook by using theserializer_hookparameter (optional). If you don´t use theserializer_hookparameter the default hook'<serialized-object>'will be used.Parameterbinary_serializer(bool) (default = False)serializer_hook(string) (optional / default = '<serialized-object>')ExamplemyDicta.setBinarySerializer(True)myDicta.setBinarySerializer(True,'<my_serialzer_hook>')Data Type MethodsBehaves like a regular nested dict and supports all data type methods. Adding, removing, modifiying and accessing of nested elements should work out of the box. For example:NestedDict.update()NestedDict.update(*args,*kwargs)NestedDict.clear()NestedDict.clear()NestedDict.pop()NestedDict.pop(key)NestedDict.popitem()NestedDict.popitem(key)NestedDict.setdefault()NestedDict.setdefault(key,default=None)and so forth: keys(), iter() …NestedList.append()NestedList.append(item)and so forth: pop()…Dependenciesosrejsonpickle