Python Forum
Saving the state of a program...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving the state of a program...
#1
Hello,

I would like to make sure that I have the correct understanding of what saving the state of a program means. Say we write a Python script with several statements that generate some lists, some dictionaries, etc. We then save the file as newfile.py. We can re-run the script as many times we like and the data we created (the lists, dictionaries, etc.) will be the same.

However, there are situations in which the script uses external data, does computations, and generates some output. If we run the script again, the outputs may be different so, if we are interested in saving that specific output data, we must save the data into an external file and we will be able to upload again without it being changed later. There are different methods using the modules pickle, json, etc.

Saving the state essentially means saving some of the script data (like freezing it) into an external file which can be a pickle file, json, txt, csv, excel, etc.
because re-running the code would generate brand new data...

Is that correct?

Any basic example? What kind of simple Python code would show that? Would the code need to be using a data contained in an external file and when the file changes the output changes?

Thanks!
bytecrunch
Reply
#2
(Mar-07-2022, 12:57 AM)bytecrunch Wrote: Is that correct?
Yes, I agree. I can add to this you might also save the state do a database.
bytecrunch likes this post
Reply
#3
(Mar-07-2022, 09:17 AM)ibreeden Wrote:
(Mar-07-2022, 12:57 AM)bytecrunch Wrote: Is that correct?
Yes, I agree. I can add to this you might also save the state do a database.

So saving the state is really just about saving the value of specific variables present in the script....
Reply
#4
If you write your code "correctly" the "state" is completely defined by the values of variables. However, it is very easy to write code where this is not so. Any kind of recursion is almost impossible to restore. Code that has pointers to enclosed functions can cause problems. Code that interacts with external systems that have "states" must take care to return the external system to the desired state. Code that depends on some sort of sequence to arrive at a "state" may have to duplicate or somehow mimic the sequence. Code that depends on external resources (files, folders, databases) may need to recover from the resource no longer being available

What kind of "state" are you trying to restore?
bytecrunch likes this post
Reply
#5
Hello and thanks.

Those creating ML models, train the models and generate the optimal set of weights and biases which gets saved. The entire model is saved along with its biases and weights. When using the trained model, the same parameters get used. In general, I have seen the model saved as json or as ymal... This is an example of saving the state....

Python can save the state (of the variables) into a json, a txt, ymal files...Or even pickle the file....

In one example I have seen, data was saved with json format using the dump() method and the json module, inside a txt file instead of inside a file with .json extension...Would that be a problem, i.e. a txt file whose content is json formatted?

Thanks!
Reply
#6
JSON is just plain text anyway. Probably one of the only reasons to, say, save it with an extension of ".json" instead is that your editor may use the extension to turn on syntax highlighting and other useful things.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving progress in a Python program to use later Led_Zeppelin 9 2,216 Sep-11-2022, 01:32 PM
Last Post: snippsat
  saving and loading text from the clipboard with python program MaartenRo 2 1,682 Jan-22-2022, 05:04 AM
Last Post: MaartenRo
  State graph Kaluss 1 2,265 Mar-18-2019, 05:29 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020