Python save program state


















Put all of your "state" data in one place and use a pickle. The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.

If you want to save everything, including the entire namespace and the line of code currently executing to be restarted at any time, there is not a standard library module to do that. As another poster said, the pickle module can save pretty much everything into a file and then load it again, but you would have to specifically design your program around the pickle module i. If you ok with OOP, consider creating a method for each class that output a serialised version using pickle to file.

Then add a second method to load in the instance the data, and if the pickled file is there you call the load method instead of the processing one. I use this approach for ML and it really seed up my workflow. In the traditional programming approach the obvious way to save a state of variables or objects after some point of execution is serialization. So if you want to execute the program after some heavy already computed state we need to start only from the deserialization part.

These steps will be mostly needed mostly in data science modelling where we load a huge CSV or some other data and compute it and we do not want to recompute every time we run a program.

All you need to do is do execute a selected portion of your python code let us say line , which are dependent on pervious lines Jupyter saves the state of for you. Explore a tutorial it and give it a try. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? I'm developing an adventure game in Python which of course is lots of fun.

One of the features is the ability to save games and restore the saves later. I'm using the pickle module to implement this. Capturing current program state and neatly replacing it later is proving to be trickier than I first imagined, so I'm here to ask for a little direction from wiser minds than mine! When my program initializes, each game object is stored in two places -- the defining module, and in a list in another module.

The following example is not from my actual code, but what happens is the same. When I update the instance attributes in one place, the changes are reflected in the other place. The instances in the globalstate. I started to write code to get around this.

I figured that with each loadgame call, I could reset all the lists in globalstate to empty, then reappend each game object to the appropriate list. But this possibility got complicated fast, because all game objects belong to more than one list. My apple instance alone would belong to globalstate. Some of the game objects contained in these lists don't need to be a part of capturing program state in the first place! But I'm stuck, because unpickling so far as I understand it creates a brand new instance that doesn't know it used to have references to itself in the globalstate lists.

Any advice out there? I'm looking for a clean, elegant way to overwrite the same class instance in two arbitrary places at once. Perhaps the example code I've provided isn't even the best way of saving and restoring program state. Perhaps I can easily update my globalstate lists and I'm just overlooking the simple way. Or perhaps the solution lies in abandoning the concepts of referencing my game objects through module attributes and lists. I'm open to any suggestions. Thanks in advance for any help!

Follow Post Reply. You can navigate into the python stack using module inspect and generate the context for all the functions in the stack trace. This is probably no more than 50 lines of code, maybe 20 :- You can find sample of how to get these info and use them in the sample I was reffering before.

I don't think it will work with objects defined by extension modules, except if they somehow support serialization, will it? I guess I can dump core for debugging these objects, and serialize the Python context for debugging the rest. And then there are the lovely cases when CPython crashes, like "deletion of interned string failed. Abort core dumped ", where you get to shovel through CPython state with a native debugger.

Maybe the best way around this is to spawn sub-processes for running native code This discussion thread is closed Start new discussion. Similar topics Python. Python syntax in Lisp and Scheme. Waterbury last post: by. Help with saving and restoring program state. Running autogenerated code in another python instance. Accessors in Python getters and setters.

Mobile Development.



0コメント

  • 1000 / 1000