Save and load game minesweeper [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
I need help. The task is to add to this game (minesweeper), save the game and download from a file (if there is no file, you need to create one). Also add a check box counter.
My code

You could use pickle for serializing the current instance of the class that is executing the game like:
import pickle
def save(self, filename):
with open(filename, mode) as f:
pickle.#dump, load(self, f)
for mode insert either wb or rb for writing and reading, and then one line below .dump for saving and .load for loading.
Then whenever needed you just call the functions.
I suggest you just read the pickle docs to implement it, so you can acutally understand the logic behind it, instead of blind-copying.
Edit: As Bryan Oakley correctly mentioned, you cant pickle tkinter objects, so for them I would recommend storing the state of the tkinter objects in a dictionary in the class, and pickle that dictionary instead of the tkinter objects.

Related

Is there a way to make a textbox save by itself? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
So let's say I wanted to make a code editor and I want the contents in a text box to save by itself.
How would I do that, and where would I start?
Here is my thought:
I would create a function that saves the content and run it in a forever loop. But it won't work, so how would I do it.
Step one: make a function that saves the data:
def save():
data = the_text_widget.get("1.0", "end-1c")
with open("the_filename.txt", "w") as f:
f.write(data)
Next, write a function that calls this function over some interval, like every 10 seconds:
def autosave():
save()
the_text_widget.after(10000, autosave)
And finally, call this function once and it will run every 10 seconds:
autosave()
This isn't the only way, but it's arguably the simplest.

deleting results after completion of code's execution PYTHON [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Currently I am working on a project in which I need to save n number of images (to be used in the program's scope). Since the number of images to be saved is dynamic, it may end up exhausting the whole space which i have for my project.
I wanted to know that can there be something added to my code so that after 100% completion of my code the images get automatically deleted as I do not need them after the code's execution.
How can this be done?
I need to save images as they are passed as an argument to one of my functions inside my code. If you know how can I pass image without saving it to my function then please comment here
might be an idea to delete the files immediately after you've done the code you need to do i.e
import os
# Open image
# Manipulate image
os.remove(path_to_image)
Keep track of all the image files you're creating, then delete them in a finally block to ensure they'll be deleted even if an exception is raised.
import os
temp_images = []
try:
# ...do stuff
# ...create image at path_to_file
temp_images.append(path_to_file) # called multiple times
# ...other stuff
finally:
for image in temp_images:
os.remove(image)

Saving variable value outside of python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to python, been coding in school for about a year now, but I like to code when I get bored. I have made two programs but both are useless as I have to input each value of the variables every time I start it up. Is there anyway I can save the value of a variable externally so when it loads it will open up the file and assign each variable?
You should use the pickle module for that purpose:
l =[1,2,3,4,5]
import pickle
pickle.dump(l,open("mydata","wb"))
and for getting your variable back:
import pickle
l = pickle.load(open("mydata","rb"))
If you have many variables to save, consider embedding them in a dictionary for instance.
Yo can use the shelve module its pretty simple it puts all variables into a dictionary then when your file reopens you can make shelve set the variables back. Here is a good example of using the shelve module.
To save data to a file, you could use
filehandle = open(filename, 'w')
filehandle.write(string)
filehandle.close()
Preferred in Python is
with open(filename, 'w') as filehandle:
filehandle.write(string)
because the file will be closed upon exiting the with block even if the block exits with an error, and without requiring the programmer to remember to close the file.
Load the values back in with filehandle.readline() or readlines().
You can also use the Python libraries json or csv to facilitate moving data into and out of files. If you have no need to inspect or modify the data in the file using another program (e.g. Notepad++ or MS Excel), you might prefer pickle or shelve.

Python memory leak: do I need to delete? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got a python script that is slowly consuming all of my memory (48GB). If I recall, python will perform garbage collection so there is no need for me to cleanup after myself?
for example:
class data_store:
dat1={}
dat2={}
dat3={}
class myclass ():
def mem_func(self):
self.x = data_store()
self.x.dat1 = (lots of data)
self.x.dat2 = (lots of data)
y = x.dat1 + 1
...
Most of my data is stored in data_store() temporarily before it is written out to files. I would think that this would be the source of the leak. Everytime mem_func() is called a new data_store() object is created and assigned to self.x. I assume that the old data_store() object would now be a candidate for the GC to delete. In addition, I would assume that y also be able to be deleted after mem_func completes.
The only other thing I can think of is that I am creating figures with matplotlib and saving them to a file. That is all done in one function but perhaps I need to delete the figure properly. Also, I have a sqlite db that is open the whole time where I am writing data but that is not alot of data. The image is much bigger.
You need to remember that GC only collects data that no pointer (variable) is pointing at it. In other words, as long as the memory is accessible via your variables, it won't be collected/freed.
So you need to assing None to the variables you don't need any more, or assign new data to the same variable names, if you don't need them any more.

Using objects other class created [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have python file that has a user interface, that mainly create objects of some class.
That file will be used by my colleagues, on their own computers.
In another file, from my computer, I'm willing to reach those objects that the first file generated.
What will be the best way to "save" the objects of the class, and then reach them from
my computer?
Thanks
What you want to do is have the script serialize the objects, and send them to your computer over the network.
As inspectorG4dget has said, you can use the pickle module to serialize your objects, and the requests library should be good for sending the objects from the client side.
On your machine, you would need a web-server/socket-listener, listening for the sent messages. You would deserialize them, and use them in some way after that.
Pickle or cPickle nicely handles saving object instances (as well as anything else); documentation here.
Two notes from when I fumbled through the a similar problem:
When you load a pickled object instance, you must have the object's class definition present in the namespace of the script/environment where you load.
Not everything can be pickled; I ran into this when saving objects that contained scipy spline instances. In your class definition, you can override the default behavior when pickling and unpickling in order to safely save and restore such attributes.

Categories

Resources