4.5. Saving and Loading
Let us create a simple transition that counts how many times the user visits:
#4: calls the macro #VISITS defined in #7.
MacroVisits can be defined as follow:
#10-13: uses the statements to return the appropriate literal.
The challenge is that we must save the number of visits to make this macro effective (and correct). This can be achieved by saving the variable dictionary into a binary file using the built-in Python object serialization called :
#1: takes a dialogue flow df and a file path varfile for saving the variable dictionary to.
#3: creates a dictionary by copying only user-generated variables.
After running this code, you will see the visits.pkl file saved under the resources directory.
The following code shows how to load the saved dictionary to a new dialogue flow:
#1: takes a dialogue flow df and a file path varfile for loading the variable dictionary from.
#2: opens a readable (r) and binary (b) file and loads the object as a dictionary.